简体   繁体   中英

uibarbuttonitem not working

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithTitle:@"Refresh" style:UIBarButtonItemStylePlain target:self action:@selector(refreshButtonClicked:)];
    [self.navigationItem setRightBarButtonItem:refreshButton animated:YES];
}
- (void)refreshButtonClicked:(id)sender {
    NSLog(@"Refreshed.");
}

I added a navigation controller programmatically in AppDelegate and set this view as the root.

When I was trying to add a navigationItem programmatically as shown above, the refreshButton showed in the navigation bar but refreshButtonClicked: was not invoked.

What's the problem? Any suggestions and comments are welcome.

Make a call to the super class in your viewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithTitle:@"Refresh" style:UIBarButtonItemStylePlain target:self action:@selector(refreshButtonClicked:)];
    [self.navigationItem setRightBarButtonItem:refreshButton animated:YES];
}

It will ensure that the root class setup is done

Problem solved. Weird. After I clean the builds, delete the derived data and restart Xcode, it goes runs properly.

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