Exception: Cannot Manually Set the Delegate on a Uinavigationbar Managed by a Controller

Trying to handle back navigation button action in iOS

Try this code using VIewWillDisappear method to detect the press of The back button of NavigationItem:

-(void) viewWillDisappear:(BOOL)animated
{
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound)
{
// Navigation button was pressed. Do some stuff
[self.navigationController popViewControllerAnimated:NO];
}
[super viewWillDisappear:animated];
}

OR There is another way to get Action of the Navigation BAck button.

Create Custom button for UINavigationItem of back button .

For Ex:

In ViewDidLoad :

- (void)viewDidLoad 
{
[super viewDidLoad];
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Home" style:UIBarButtonItemStyleBordered target:self action:@selector(home:)];
self.navigationItem.leftBarButtonItem=newBackButton;
}

-(void)home:(UIBarButtonItem *)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
}

Swift :

override func willMoveToParentViewController(parent: UIViewController?) 
{
if parent == nil
{
// Back btn Event handler
}
}

How to set delegate on CustomToolbar in rootViewController using a NavigationController setup with initWithNavigationBarClass:toolbarClass:

My guess is that in order to work, the navigationController needs to be the navigationBar delegate. That's why you can not set the navigationBar delegate when it is used in a navigationController.

As you use a subclass of navigationBar, you can try to have a subprotocol of the UINavigationBarDelegate in which you add a navigationBarWasSelected method.
In a new UINavigationController subclass, you implement this method to let the rootViewController now when the navigation is clicked.

Or, in your custom navigation bar you can send a NSNotification in the NSNotificationCenter, and catch it in your RootViewController.

Draw custom Back button on iPhone Navigation Bar

You need to set up a custom stack of UINavigationItem objects and push them on to the UINavigationBar. This is the only way I know of to get a true back button. I haven't tested this code, but you should do something like this:

UINavigationItem *previousItem =
[[[UINavigationItem alloc] initWithTitle:@"Back title"] autorelease];

UINavigationItem *currentItem =
[[[UINavigationItem alloc] initWithTitle:@"Main Title"] autorelease];

[navigationBar setItems:[NSArray arrayWithObjects:previousItem, currentItem, nil]
animated:YES];

To handle when the buttons are pressed you should set yourself as the navigation bar's delegate and implement the UINavigationBarDelegate delegates.

UINavigationController intercepting – popViewControllerAnimated:

Add your refresh code to the viewWillAppear:(BOOL)animated method on the view controller that is about to be displayed. In your case, that is the view controller that's already on the navigation stack.

How to actually catch when back button is pressed?

Try with following UINavigationBarDelegate.

 func navigationBar(navigationBar: UINavigationBar, didPopItem item: UINavigationItem)

UINavigationController: How to cancel the back button event?

viewWillDisappear is a delegate method for the event that the view is going to disappear - and there's nothing the developer can do about that! If you could, it would be a viewShouldDisappear delegate method.

So I guess the only way is as you suggest, to use a custom leftBarButtonItem.



Related Topics



Leave a reply



Submit