Iphone - 类属性导致崩溃
所以我对整个目标 C 认为仍然很陌生,我遇到了一个我不确定根本原因的问题。
我的 h 文件基本上如下所示:
@interface DrinkDetailViewController : UIViewController<UITextFieldDelegate>
{
UITextField* nameTextField;
UITextField* activeView;
}
@property (nonatomic,retain) IBOutlet UITextField* nameTextField;
在我的 m 文件中,我正在实现委托函数:
-(BOOL) textFieldShouldBeginEditing:(UITextField*) textField
{
activeView = textField;
return YES;
}
问题是,如果我也将 activeView 声明为属性(意味着添加属性、合成和所有处理),那么当我要离开视图(这是一个基于导航的项目),我的应用程序崩溃了。然而,如果我把它当作非财产来生活,一切似乎都运转良好。这是为什么 ???
So I'm still very new to this whole objective C think and I ran into a problem I'm not sure the root cause for.
My h file looks basically like this :
@interface DrinkDetailViewController : UIViewController<UITextFieldDelegate>
{
UITextField* nameTextField;
UITextField* activeView;
}
@property (nonatomic,retain) IBOutlet UITextField* nameTextField;
In my m file i'm implementing the delegate function :
-(BOOL) textFieldShouldBeginEditing:(UITextField*) textField
{
activeView = textField;
return YES;
}
The thing is that if i'm declaring activeView to be a property as well (meaning adding property, synthesize and the all deal), then when i'm leaving the view (it's a navigation based project) my app crashes. However, if I live it as a non property everything seems to work fine. Why is that ???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为它是一个属性,所以您需要这样调用它:
这样将应用正确的内存管理规则,并且将为您完成 KVO 通知。
because it's a property you need to call it this way:
That way the correct memory management rules will be applied and also the KVO notifications will be done for you.
您是否在实现文件中合成了 activeView:
are you synthesing activeView in your implementation file: