Objective-C 中的 IBAction 方法内部不能引用类实例

发布于 12-16 20:19 字数 923 浏览 6 评论 0原文

我正在为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

节枝2024-12-23 20:19:26

尝试为 myCard 显式设置 retain 属性。默认情况下使用分配属性时可能会出现泄漏。

@property (nonatomic, retain) NSMutableArray *myCard;

Try to make explicit retain property for myCard. It may be a leak when using assign property by default.

@property (nonatomic, retain) NSMutableArray *myCard;
小ぇ时光︴2024-12-23 20:19:26

设置器解决了问题

我终于通过更改 @property 属性和 myCard player.h

@property (nonatomic,retain) NSMutableArray *myCard;

player.m

- (void)setMyCard:(NSMutableArray *)aCardArr
{
    [myCard autorelease];
    myCard = [[NSMutableArray arrayWithArray:aCardArr] retain];
}

的自定义 。

感谢您的帮助

i finally solved the problem by changeing the @property attribut and a custom setter for myCard

player.h

@property (nonatomic,retain) NSMutableArray *myCard;

player.m

- (void)setMyCard:(NSMutableArray *)aCardArr
{
    [myCard autorelease];
    myCard = [[NSMutableArray arrayWithArray:aCardArr] retain];
}

Maybe this is just basic stuff, but as a newer to objective-c, just post back here for someone like me as a reference.

Thanks for all of your help

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文