从另一个类无法访问UIButton?

时间:2011-11-21 04:49:26

标签: ios class variables uibutton

我正试图从我的CCLayerClass访问我的Play类中的UIButton。

问题是它无法正常工作!

以下是我在Play类中声明的方式: .H

IBOutlet UIButton *pauseButton;
@property(nonatomic, retain) IBOutlet UIButton *pauseButton;

的.m

@synthesize pauseButton;

然后在dealloc中:

[pauseButton release];

当然我也是在Interface builder中连接它。

然后在我的其他课程(My CCLayer)中。我试着这样做:

Play *play = [[[Play alloc] init] autorelease];
[play.pauseButton setHidden:YES];

问题是,它只是不隐藏按钮。这有什么理由吗?

谢谢!

EDIT1 : 我的Play.h

IBOutlet UIButton *pauseButton;
BOOL pauseButtonVisible;

@property(nonatomic, retain) IBOutlet UIButton *pauseButton;
@property(readwrite) BOOL pauseButtonVisible;

的.m

@synthesize pauseButton;

- (void)setPauseButtonVisible: (BOOL) variableToSet {
    pauseButtonVisible = variableToSet;
    if(pauseButton)
        [pauseButton setHidden: !pauseButtonVisible];
}
- (BOOL) pauseButtonVisible
{
    return(pauseButtonVisible);
}

viewWillAppear中:

[pauseButton setHidden: !pauseButtonVisible];

我还确保确保我在Interface Builder中连接它

然后在CCLayerClass中我这样做:

Play *play = [[[Play alloc] init] autorelease];
    if(play.pauseButton == NULL) {  
        NSLog( @"pause button is NULL");
    }

但是那个NSLog被调用了!为什么我的pauseButton为NULL?我只需要分配它以使它保持活力,这可能吗?

谢谢!         play.pauseButtonVisible = YES;

1 个答案:

答案 0 :(得分:1)

好。希望第三次是魅力(之后,我放弃了,因为我该上床睡觉了)。

在.h文件中,我保留了新的pauseButtonVisible BOOL属性。

@interface Play : UIViewController
{
    BOOL pauseButtonVisible;
    IBOutlet UIButton *pauseButton;
}

@property(nonatomic, retain) IBOutlet UIButton *pauseButton;
@property(readwrite) BOOL pauseButtonVisible;

@end    

但是在.m文件中,我们做了一些不同的事情:

@interface Play

// here we are rolling our own setters and getters
// instead of @synthesizing...
- (void)setPauseButtonVisible: (BOOL) variableToSet
{
    pauseButtonVisible = variableToSet;
    if(pauseButton)
        [pauseButton setHidden: !pauseButtonVisible];
}

- (BOOL) pauseButtonVisible
{
    return(pauseButtonVisible);
}

- (void) viewWillAppear: (BOOL) animated
{
    [pauseButton setHidden: !pauseButtonVisible];
    [super viewWillAppear: animated];
}

Play *play = [[[Play alloc] init] autorelease]; // you should really be using initWithNibName, but anyways
play.pauseButtonVisible = YES;

现在,希望暂停按钮在适当的时间可见或隐藏,以便代码运行。