-(BOOL) isKindOfClass: classObj 用来判断是否是某个类或其子类的实例
-(BOOL) isMemberOfClass: classObj 用来判断是否是某个类的实例
-(BOOL) respondsToSelector: selector 用来判断是否有以某个名字命名的方法(被封装在一个selector的对象里传递)
+(BOOL) instancesRespondToSelector: selector 用来判断实例是否有以某个名字命名的方法. 和上面一个不同之处在于, 前面这个方法可以用在实例和类上,而此方法只能用在类上.
-(id) performSelector: selector
SEL sel = @selector (start:) ; // 指定action
if ([obj respondsToSelector:sel])
{ //判断该对象是否有相应的方法
[obj performSelector:sel withObject:self]; //调用选择器方法
}
使用[[UIApplication sharedApplication] keyWindow]查找应用程序的主窗口对象
respondsToSelector方法对不同ios版本有类似动态支持的功能,例如在ios4.0以后中eagleView中新增了对 iphone4 Retina高清屏幕支持的成员变量scaleContent,在4.1sdk下可以直接使用self.scaleContent = 2,但是如果换用3.2sdk,这样编译马上就会报错,提示eagleView中没有这样的成员变量,这里报错也是自然,因为3.2中根本没有对高清的支持,这是出了四代的新硬件后新增的。那有没有办法使用同一份代码在不同的sdk版本中编译而又能动态运行在不同的ios中呢,这里,可以使用成员变量的get和set方法,而避免使用 “ . ”属性,再配合上respondsToSelector方法,这个问题就迎刃而解了。具体解决思路如下:
if([self respondsToSelector:@selector(setScaleContent:)])
{
[self setScaleContent:2]; //ios4.0以上才会执行进来
}
else
{
}
if和else语句的大括号中还可以加入一些自定义的代码,这样使用4.1sdk编译而目标设定为ios3.1.2的程序包就可以在不同的ios上有区别的动态执行代码了。
-(BOOL) isMemberOfClass: classObj 用来判断是否是某个类的实例
-(BOOL) respondsToSelector: selector 用来判断是否有以某个名字命名的方法(被封装在一个selector的对象里传递)
+(BOOL) instancesRespondToSelector: selector 用来判断实例是否有以某个名字命名的方法. 和上面一个不同之处在于, 前面这个方法可以用在实例和类上,而此方法只能用在类上.
-(id) performSelector: selector
SEL sel = @selector (start:) ; // 指定action
if ([obj respondsToSelector:sel])
{ //判断该对象是否有相应的方法
[obj performSelector:sel withObject:self]; //调用选择器方法
}
使用[[UIApplication sharedApplication] keyWindow]查找应用程序的主窗口对象
respondsToSelector方法对不同ios版本有类似动态支持的功能,例如在ios4.0以后中eagleView中新增了对 iphone4 Retina高清屏幕支持的成员变量scaleContent,在4.1sdk下可以直接使用self.scaleContent = 2,但是如果换用3.2sdk,这样编译马上就会报错,提示eagleView中没有这样的成员变量,这里报错也是自然,因为3.2中根本没有对高清的支持,这是出了四代的新硬件后新增的。那有没有办法使用同一份代码在不同的sdk版本中编译而又能动态运行在不同的ios中呢,这里,可以使用成员变量的get和set方法,而避免使用 “ . ”属性,再配合上respondsToSelector方法,这个问题就迎刃而解了。具体解决思路如下:
if([self respondsToSelector:@selector(setScaleContent:)])
{
[self setScaleContent:2]; //ios4.0以上才会执行进来
}
else
{
}
if和else语句的大括号中还可以加入一些自定义的代码,这样使用4.1sdk编译而目标设定为ios3.1.2的程序包就可以在不同的ios上有区别的动态执行代码了。