Gajendrasinh Chauhan
Gajendrasinh Chauhan

Reputation: 3397

How to disable navigation bar back button when some activity is loading in iOS?

I have two ViewControllers.

In second view controller, When I click on search button action (activity started, like web service is executing).After getting data from web service, It will show UIAlertview.

My problem is, When I pressed search button(activity start), now I clicked on back button on Navigation Bar. Now, I am on previous view(First view controller). I have now response of search button activity with UIAlertview.

Obviously, My app will crash on OK button of alert view.

So, In that case

How can I disable navigation bar back button? (when I click on search)

OR

How to prevent UIAlerview to display when I click on back button?

Update :

I tried all methods to hide back bar button. :-)

Upvotes: 0

Views: 1702

Answers (2)

AntonijoDev
AntonijoDev

Reputation: 1315

Only hiding button or disabling user interaction is not the best practice cause the user won't know what's happening with the app, in case of longer process the user will thing that the app is frozen and will terminate it. Good practice in these cases are HUDs, progress indicators that block user interactions. In this case the user will see that something is happening in background and will wait for it to finish, and if he tires to go back the HUD wont let him. I usually use this one: https://github.com/jdg/MBProgressHUD , the implementation is very easy and it looks quite nice, but you can always program one of your own.

Upvotes: 1

Armand DOHM
Armand DOHM

Reputation: 1131

save reference of your UIAlertView as property

@property (strong, nonatomic) UIAlertView *alert;

show alert

  self.alert = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles: nil];
    [self.alert show];

in case of "risk", remove the delegate

self.alert.delegate = nil;

When risk finished, reset delegate

self.alert.delegate = self;

Upvotes: 0

Related Questions