简体   繁体   中英

UIButton enable doesn't work

I have a UIViewController with a single button and an activity indicator. In the class for this VC MainViewController.m I do the following in viewDidAppear :

- (void)viewDidLoad
{
    [super viewDidLoad];
    _actLoadLoc.color = [UIColor blueColor];
    _startButton.enabled = NO;
    [_startButton setTitle:@"Fetching Location" forState:UIControlStateDisabled];
}

Another method in my MainViewController.m is called readyToGo and is implemented as follows:

-(void) readyToGo
{

    [NSThread sleepForTimeInterval:1.0f];
    NSLog(@"Done sleeping");
    _startButton.enabled = YES;
    [_startButton setTitle:@"Start" forState:UIControlStateNormal];
    _actLoadLoc.stopAnimating;

}

I have properties for both UIButton , UIActivityIndicatorView and a declaration of the readyToGo method in my MainViewController.h as follows:

@property (weak, nonatomic) IBOutlet UIButton *startButton;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *actLoadLoc;
-(void) readyToGo;

The readyToGo method is called from another class abc.[h/m] which imports MainViewController.h . The call happens after one of the functions in abc.m completes filling an array with calculated data.

The call works since Done Sleeping shows in the output, however the startButton is not enabled, its test does not change and the actLoadLoc does not stop animating... Any idea what's wrong with my code/method?

Thanks in Advance!

You are calling the readyToGo on the wrong instance of the view controller. You have an instance which is displaying content on the screen and you are, in some way, creating a new one to call the method on. You need to get the existing one instead.


It's not ideal, but you should be able to get the controller with:

UINavigationController *n = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
SDPPMainViewController *mvc = (SDPPMainViewController *)[n viewControllers][0];

(Will need to add some casts, and should probably break out to multiple lines)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM