这两个方法常常会困惑我们,它们有什么区别呢?
定义首先,我们来看看它们的定义。
isKindOfClass:
官方解释:Returns a Boolean value that indicates whether the receiver is an instance of given class or an instance of any class that inherits from that class.
isMemberOfClass:官方解释:Returns a Boolean value that indicates whether the receiver is an instance of a given class.
使用 关于使用,在网上看到一个很形象的例子,这里就直接拿来用了。这里有两个类,分别是继承于NSObject的Person,Person的Teacher
#import <Foundation/Foundation.h> @interface Person : NSObject { NSString *name; } -(void)setName:(NSString*)n; @end #import "Person.h" @implementation Person -(void)setName:(NSString *)n { name = n; } @end
#import "Person.h" @interface Teacher : Person -(void)teach; @end #import "Teacher.h" @implementation Teacher -(void)teach { NSLog(@"我教数学"); } @end
使用isKindOfClass的例子:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; Person *person = [[Person alloc] init]; Teacher *teacher = [[Teacher alloc] init]; //YES if ([teacher isKindOfClass:[Teacher class]]) { NSLog(@"teacher 是 Teacher类或Teacher的子类"); } //YES if ([teacher isKindOfClass:[Person class]]) { NSLog(@"teacher 是 Person类或Person的子类"); } //YES if ([teacher isKindOfClass:[NSObject class]]) { NSLog(@"teacher 是 NSObject类或NSObject的子类"); } [person release]; [teacher release]; [pool release];
2012-07-04 14:34:17.315 ObjectiveCTest[2595:f803] teacher 是 Teacher类或Teacher的子类 2012-07-04 14:34:17.316 ObjectiveCTest[2595:f803] teacher 是 Person类或Person的子类 2012-07-04 14:34:17.316 ObjectiveCTest[2595:f803] teacher 是 NSObject类或NSObject的子类
使用isMemberOfClass的例子:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; Person *person = [[Person alloc] init]; Teacher *teacher = [[Teacher alloc] init]; //YES if ([teacher isMemberOfClass:[Teacher class]]) { NSLog(@"teacher Teacher类的成员"); } //NO if ([teacher isMemberOfClass:[Person class]]) { NSLog(@"teacher Person类的成员"); } //NO if ([teacher isMemberOfClass:[NSObject class]]) { NSLog(@"teacher NSObject类的成员"); } [person release]; [teacher release]; [pool release];
输出结果:
2012-07-04 14:23:07.965 ObjectiveCTest[2460:f803] teacher Teacher类的成员
看了这两个例子,应该就会明白了。
IOS7一出来,对应的xcode版本变成了5了,这次xcode升级比较大,特别是在源代码编译方面,苹果下足了功夫,编译时间不到原来的一半,忽然强烈觉得android在这方面需要加强啊;
其他不多说,XIB在XCODE5上使用的最新编译,只能在5上面修改和查看,然后4.6上面是打不开的;
解决办法:兼容低版本,跟我们兼容ios5一样,选择的tagart是5.0的开发环境
在xcode5中的 Interface Builder Document 下的Opens in 改为4.6点的就行
RelativeLayout 布局的运用
LinearLayout 布局的多重嵌套会导致 程序执行效率的低下,因此我们最好用RelativeLayout 来实现布局的效果,当然五大布局是结合使用才会出现美好的效果;
看效果图:
代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_main" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:src="/blog_article/@drawable/index_icon/index.html" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView1"
android:layout_below="@+id/imageView1"
android:background="#50000000"
android:drawableTop="@drawable/icon_mine"
android:gravity="center"
android:paddingBottom="8dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="5dp"
android:text="我的文档"
android:textColor="@color/white"
android:textSize="12sp" />
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/textView1"
android:background="#50000000"
android:drawableTop="@drawable/icon_local"
android:gravity="center"
android:paddingBottom="8dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="5dp"
android:text="本地管理"
android:textColor="@color/white"
android:textSize="12sp" />
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="5dp"
android:background="#50000000" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/icon_files"
android:gravity="center"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="5dp"
android:text="新闻编审"
android:textColor="@color/white"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginRight="3dp"
android:background="@color/black"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:text="9"
android:textColor="@color/white"
android:textSize="8dp" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/TextView01"
android:layout_alignTop="@+id/relativeLayout1"
android:background="#50000000" >
<TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/icon_prog"
android:gravity="center"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="5dp"
android:text="流程管理"
android:textColor="@color/white"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/TextView02"
android:layout_below="@+id/TextView02"
android:layout_marginRight="3dp"
android:background="@color/black"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="12"
android:textColor="@color/white"
android:textSize="8dp" />
</RelativeLayout>
<TextView
android:id="@+id/TextView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/TextView03"
android:layout_alignBottom="@+id/TextView03"
android:layout_alignLeft="@+id/relativeLayout2"
android:background="#50000000"
android:drawableTop="@drawable/icon_mine"
android:gravity="center"
android:paddingBottom="8dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="5dp"
android:text="录音"
android:textColor="@color/white"
android:textSize="12sp" />
<TextView
android:id="@+id/TextView06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/relativeLayout2"
android:layout_alignTop="@+id/TextView05"
android:background="#50000000"
android:drawableTop="@drawable/icon_mine"
android:gravity="center"
android:paddingBottom="8dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="5dp"
android:text="拍照"
android:textColor="@color/white"
android:textSize="12sp" />
<TextView
android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/TextView05"
android:layout_below="@+id/relativeLayout1"
android:layout_marginTop="5dp"
android:background="#50000000"
android:drawableTop="@drawable/icon_mine"
android:gravity="center"
android:paddingBottom="8dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="5dp"
android:text="撰稿"
android:textColor="@color/white"
android:textSize="12sp" />
<TextView
android:id="@+id/TextView05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/relativeLayout1"
android:layout_below="@+id/TextView03"
android:layout_marginTop="5dp"
android:background="#50000000"
android:drawableTop="@drawable/icon_mine"
android:gravity="center"
android:paddingBottom="8dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="5dp"
android:text="摄像"
android:textColor="@color/white"
android:textSize="12sp" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/lay1"
android:layout_alignTop="@+id/relativeLayout2"
android:layout_marginTop="13dp"
android:src="/blog_article/@drawable/icon_help/index.html" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/TextView01"
android:layout_alignLeft="@+id/lay1"
android:src="/blog_article/@drawable/icon_setting/index.html" />
<RelativeLayout
android:id="@+id/lay1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/TextView01"
android:layout_marginRight="14dp" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:src="/blog_article/@drawable/icon_bulletin/index.html" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView2"
android:layout_toRightOf="@+id/imageView2"
android:text="3"
android:textColor="@color/white"
android:textSize="8sp" />
</RelativeLayout>
</RelativeLayout>
比较简单,如果你有好的想法,或好的布局,请留言,或发到我的邮箱jrhhybh@163.com谢谢。