Ios 在UIView中未调用自定义setter
我正在使用bool属性的自定义setter,因为当这个值更改时,我需要做一些额外的工作。以下是我的实现: MyView.hIos 在UIView中未调用自定义setter,ios,iphone,objective-c,ios5,getter-setter,Ios,Iphone,Objective C,Ios5,Getter Setter,我正在使用bool属性的自定义setter,因为当这个值更改时,我需要做一些额外的工作。以下是我的实现: MyView.h @interface MyView : UIView @property (nonatomic) BOOL isSelected; @end MyView.m @implementation MyView @synthesize isSelected; -(void)setIsSelected:(BOOL)_isSelected { self.isSel
@interface MyView : UIView
@property (nonatomic) BOOL isSelected;
@end
MyView.m
@implementation MyView
@synthesize isSelected;
-(void)setIsSelected:(BOOL)_isSelected
{
self.isSelected = _isSelected;
//Custom code that changes UI based on bool state
}
@end
然而,二传手并没有被召唤!有人能告诉我为什么吗?尝试将
-(void)setIsSelected:
方法的参数\u isSelected
更改为其他变量,例如selected
,并使用它将属性指定为\u isSelected=selected方法中的code>。尝试将-(void)setIsSelected:
方法的参数\u isSelected
更改为其他变量,例如selected
,并使用它将属性指定为\u isSelected=selected方法中的code>。调用两种设置属性和setter方法的方法
1、 点语法:self.isSelected=YES;
2、 直接打电话。[set-setIsSelected:是]
@interface MyView : UIView
@property (nonatomic) BOOL isSelected;
@end
@implementation MyView
@synthesize isSelected;
-(void)setIsSelected:(BOOL)_isSelected
{
isSelected = _isSelected;
//Custom code that changes UI based on bool state
}
@end
你的应用程序崩溃,因为你在setter方法中调用self.isSelected=\u isSelected
,它将递归地、无休止地调用你的setter方法,直到堆栈溢出 调用两种设置属性和setter方法的方法
1、 点语法:self.isSelected=YES;
2、 直接打电话。[set-setIsSelected:是]
@interface MyView : UIView
@property (nonatomic) BOOL isSelected;
@end
@implementation MyView
@synthesize isSelected;
-(void)setIsSelected:(BOOL)_isSelected
{
isSelected = _isSelected;
//Custom code that changes UI based on bool state
}
@end
你的应用程序崩溃,因为你在setter方法中调用self.isSelected=\u isSelected
,它将递归地、无休止地调用你的setter方法,直到堆栈溢出 例如,在viewDidLoad中,加载设置值self.isSelected=YES代码>,或要设置此值的位置
您的setter方法应该如下所示:
-(void)setIsSelected:(BOOL)seleted
{
_isSelected = seleted;
//Custom code that changes UI based on bool state
}
如果您在setter中使用该代码,这就是“setter循环”:self.isSelected=\u isSelected代码>例如,在viewDidLoad set valueself.isSelected=YES代码>,或要设置此值的位置
您的setter方法应该如下所示:
-(void)setIsSelected:(BOOL)seleted
{
_isSelected = seleted;
//Custom code that changes UI based on bool state
}
如果您在setter中使用该代码,这就是“setter循环”:self.isSelected=\u isSelected代码>试试这个
//in myView.h
@interface customView : UIView
{
}
@property(nonatomic)BOOL *isSelected;
@end
@implementation customView
@synthesize isSelected = _isSelected;
- (void)setIsSelected:(BOOL *)inSelected //inSelected is the in coming value for bool
{
_isSelected = inSelected;
NSLog(@"setter is called");
}
//in myView.h
@interface customView : UIView
{
}
@property(nonatomic)BOOL *isSelected;
@end
@implementation customView
@synthesize isSelected = _isSelected;
- (void)setIsSelected:(BOOL *)inSelected //inSelected is the in coming value for bool
{
_isSelected = inSelected;
NSLog(@"setter is called");
}
在myView.m文件中
//in myView.h
@interface customView : UIView
{
}
@property(nonatomic)BOOL *isSelected;
@end
@implementation customView
@synthesize isSelected = _isSelected;
- (void)setIsSelected:(BOOL *)inSelected //inSelected is the in coming value for bool
{
_isSelected = inSelected;
NSLog(@"setter is called");
}
试试这个
//in myView.h
@interface customView : UIView
{
}
@property(nonatomic)BOOL *isSelected;
@end
@implementation customView
@synthesize isSelected = _isSelected;
- (void)setIsSelected:(BOOL *)inSelected //inSelected is the in coming value for bool
{
_isSelected = inSelected;
NSLog(@"setter is called");
}
//in myView.h
@interface customView : UIView
{
}
@property(nonatomic)BOOL *isSelected;
@end
@implementation customView
@synthesize isSelected = _isSelected;
- (void)setIsSelected:(BOOL *)inSelected //inSelected is the in coming value for bool
{
_isSelected = inSelected;
NSLog(@"setter is called");
}
//in myView.h
@interface customView : UIView
{
}
@property(nonatomic)BOOL *isSelected;
@end
@implementation customView
@synthesize isSelected = _isSelected;
- (void)setIsSelected:(BOOL *)inSelected //inSelected is the in coming value for bool
{
_isSelected = inSelected;
NSLog(@"setter is called");
}
在myView.m文件中
//in myView.h
@interface customView : UIView
{
}
@property(nonatomic)BOOL *isSelected;
@end
@implementation customView
@synthesize isSelected = _isSelected;
- (void)setIsSelected:(BOOL *)inSelected //inSelected is the in coming value for bool
{
_isSelected = inSelected;
NSLog(@"setter is called");
}
在某些情况下,当对象为nil时,它会给您一种不调用setter和getter的感觉。希望您能解决这个问题。在某些情况下,当您的对象为nil时,它会让您感觉到setter和getter没有被调用。希望您能解决这个问题。当您希望调用setter方法时,如何设置属性?更多不能在setter方法中使用点符号设置属性。在同一个类中,我。。。isSelected=是;您应该执行self.isSelected=YES代码>当您希望调用setter方法时,如何设置属性?更多不能在setter方法中使用点符号设置属性。在同一个类中,我。。。isSelected=是;您应该执行self.isSelected=YES代码>我这样做了。它会导致应用程序在具有EXC_BAD_access的setter中崩溃,这是我做的。它会导致应用程序在setter中崩溃,并带有EXC_BAD_access谢谢,这可以修复它!我想我可以在下一次iOS面试中问这个问题!:-)谢谢,这就解决了!我想我可以在下一次iOS面试中问这个问题!:-)谢谢,这就解决了!我想我可以在下一次iOS面试中问这个问题!:-)谢谢,这就解决了!我想我可以在下一次iOS面试中问这个问题!:-)亚伦最好把你的答案写出来。作为对用户问题的评论。Aaron最好给出你的答案。作为对用户问题的评论。
//in myView.h
@interface customView : UIView
{
}
@property(nonatomic)BOOL *isSelected;
@end
@implementation customView
@synthesize isSelected = _isSelected;
- (void)setIsSelected:(BOOL *)inSelected //inSelected is the in coming value for bool
{
_isSelected = inSelected;
NSLog(@"setter is called");
}