Prevent changing Tabbar viewcontroller with UIAlertView

I'm trying to make that when the user press a tabbar item an UIAlertView gets called, asking if really wants to change the actual tab, the problem is that the UIAlertView doesn't stop the code until getting the answer, the code keeps running and depending on the previous value change the viewcontroller or not, not the actual.

I've tried to wait to the answer with a while, but the screen only gets darker and the alert didn't popup. I also read this post pause code execution until UIAlertview, I tried but i was unable to make it work, can someone help, thanks!

- (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{

if (([self Myfunction]) && (viewController != [tabBarController.viewControllers objectAtIndex:0])){
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"text1" message:@"text2" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    [alert show];
    [alert release];

    return boolean_var;
}

return YES;}

- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) [self setBoolean_var:NO];
else [self setBoolean_var:YES];}

Answers


- (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    if ( !([self Myfunction]) || (viewController == [tabBarController.viewControllers objectAtIndex:0])) {
        return YES;
    }

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"text1" message:@"text2" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    [alert show];
    [alert release];

    candidateViewController = viewController; // `candidateViewController` must be declared as an instance variable.

    return NO;
}

Identify which view controller that you need to show alert for and save it in candidateViewController and return NO to delay the switch. Based on the response on the alert view, you should change it.

- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != 0)
        self.tabBarController.selectedViewController = candidateViewController;
}

The last method assumes few things. Your tab bar controller is referenced by self.tabBarController and that you were setting boolean_var to return it to the earlier method. Alert view is non blocking in that method so using boolean_var is pointless.


Need Your Help

Xcode constraints for specific layout

ios xcode interface-builder constraints autolayout

I'm trying to get a specific layout in Interface Build using constraints, but can't figure out the constraints I need. I'll try and demonstrate what I'm after using a diagram. I've got a header wit...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.