Objective-C 中的 IBAction 方法内部不能引用类实例
我正在为iPhone编写一个小型纸牌游戏,
比如说,我有一个player.h/.m类,其中有一个可变数组myCard
我
@interface Player : NSObject{
NSMutableArray *myCard;
}
@property (nonatomic) NSMutableArray *myCard;
@end
在视图控制器中编写,controller.h
#import "Player.h"
@interface controller : UIViewController {
Player *playerMe;
}
在视图控制器中编写,controller.m
- (Player *)playerMe
{
if (!playerMe) playerMe = [[Player alloc] init];
return playerMe;
}
然后我有在playerMe.myCard中添加一些实例方法,到目前为止一切都很好。
我有一个在 IB 中添加的按钮,并且有一个 IB 操作:
- (IBAction)btnSort:(id)sender {
//do something with self.playerMe.myCard
}
然后出现问题,我查看调试窗口并查找所有变量中的值。在单击按钮之前, self.playMe.myCard 与那里的某个对象很好。一旦单击按钮, self.playerMe.myCard 为零,里面什么也没有
所以,我想知道为什么 self.playerMe.myCard 不能被引用,而 self.playMe 没问题,这与 @property 定义有关吗?
谢谢!
I am writing a small card game for iphone,
say, i have a player.h/.m class, which have a Mutable Array myCard
I write
@interface Player : NSObject{
NSMutableArray *myCard;
}
@property (nonatomic) NSMutableArray *myCard;
@end
in a view controller, controller.h
#import "Player.h"
@interface controller : UIViewController {
Player *playerMe;
}
in a view controller, controller.m
- (Player *)playerMe
{
if (!playerMe) playerMe = [[Player alloc] init];
return playerMe;
}
then i have some instance method to addObject in playerMe.myCard, everything is fine till now.
I have a button which was added in IB, and have a IB action:
- (IBAction)btnSort:(id)sender {
//do something with self.playerMe.myCard
}
Problem then appears, I look at the debug window and look up the value in all variables. Before the button was clicked, the self.playMe.myCard is fine with a certain object there. once the button was clicked, the self.playerMe.myCard is nil and have nothing inside
So, i would like to know why the self.playerMe.myCard cannot be reference while self.playMe is alright, is that about the @property definition?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
尝试为 myCard 显式设置
retain
属性。默认情况下使用分配属性时可能会出现泄漏。Try to make explicit
retain
property for myCard. It may be a leak when using assign property by default.