nickivey
nickivey

Reputation: 193

Disabling Button from different class

I can not figure out what I am doing wrong nothing happens when the methods are called.

Declared in the interface of the class

ViewController* mainMenu;

- (id)initWithSize:(CGSize)size {

    if (self = [super initWithSize:size]) {
        mainMenu = [[ViewController alloc] init];

        ....
    }
}

- (void)pauseEnabled {
    //  mainMenu.pauseButton.hidden = NO;
    //  mainMenu.pauseButton.enabled = YES;
    [mainMenu.pauseButton setHidden:NO];
    [mainMenu.pauseButton setEnabled:YES];
}

- (void)pauseDisabled {
    mainMenu.pauseButton.hidden = YES;
    mainMenu.pauseButton.enabled = NO;
}

- (void)startGame {
    [self pauseEnabled];
    ......
}

Upvotes: 0

Views: 34

Answers (2)

Wain
Wain

Reputation: 119031

When you run:

mainMenu = [[ViewController alloc] init];

you're creating a new view controller whose view isn't loaded and which is never shown on screen. You need to get an instance of ViewController which is actually presented on screen so that you can send it messages. How you do that depends very much on the structure of your app and what creates the ViewController.

Your usage of initWithSize: indicates that this object isn't a controller itself. In this case you may want to rethink what you're doing and, rather than have this class maintain a reference to a view controller, instead pass it a reference to the button that it should interact with when it is created.

Upvotes: 1

Rafał Sroka
Rafał Sroka

Reputation: 40030

Make sure that the pauseButton is wired to the outlet in the storyboard / xib file. Basically, make sure it is not nil. Also verify if you are adding the mainMenu's view to the view hierarchy.

Upvotes: 0

Related Questions