UIBarButtonItem not reacting after click

2019-09-02 11:35发布

问题:

From the rootViewController I navigate to a UIViewController

if (self.contr == nil) {
    ExampleViewController *controller = [[ExampleViewController alloc] 
                                   initWithNibName:@"Example" 
                                   bundle:[NSBundle mainBundle]];
    self.contr = controller;
    [controller release];
}
[self.navigationController presentModalViewController:self.contr animated:YES];

In the UIViewController I have the method

-(IBAction) goBack:(id)sender {
    [self.navigationController dismissModalViewControllerAnimated:YES];
}

I added the signature to the .h file. In the .xib file, I have a UIToolbar with a UIBarButtonItem. I connected the button to the File's Owner - goBack:

Everything appears in the screen, but when I click on the button, goBack isn't called. I also tried to do this programatically instead, but I got the same result - everything appears, but no reaction to the click.

Any ideas why it isn't working?

Edit: I just found out something invisible is over the toolbar. If I click on a specific point (over the toolbar), then goBack: is called. Since I navigated to this screen using presentModelViewController, the navigation bar isn't appearing... but probably it's there and that's what is hiding the tool bar.

回答1:

Have bind your Toolbar with File Owner?
As your UIBarButton is subview of UIToolbar so you have to bind Toolbar with File Owner.



回答2:

Presenting a modal view controller do not require you to pass through a UINavigationController. I suggest you to change this:

[self.navigationController presentModalViewController:self.contr animated:YES];
[self.navigationController dismissModalViewControllerAnimated:YES];

to this:

[self presentModalViewController:self.contr animated:YES];
[self dismissModalViewControllerAnimated:YES];

Let me know if this helps.



回答3:

Try this in the goBack method :

  [self.navigationController popToRootViewControllerAnimated:YES];


回答4:

If you are not hitting the breakpoint that means you did not connect them properly in the xib.