简体   繁体   中英

UIAlertView button to another view controller

I have in my application a uialertview, that has two buttons, the first one is cancel button , and the second one is ok. when i press on OK button, I want to go to the view controller which has a storyboard identifier "RootView", but it doesn't work. I put this below code in the appdelegate.m method.

- (void)showAlarm:(NSString *)text {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"show alarm"
                                                            message:text delegate:self
                                                  cancelButtonTitle:@"cancel"
                                                  otherButtonTitles:@"ok", nil];


        [alertView show];
    }

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

        if (buttonIndex == 0){
             NSLog(@"cancel"); 

        }else if (buttonIndex == 1){
             NSLog(@"ok");
           UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];


           UINavigationController *navigationController = [storyboard instantiateViewControllerWithIdentifier:@"navigationController"];


            [navigationController presentViewController:[storyboard instantiateViewControllerWithIdentifier:@"RootView"] animated:YES completion:nil];


        }
    }

Do you wanna push the new view controller ou present it modally? Assuming the last, maybe this helps you:

RootView *viewController = [storyboard instantiateViewControllerWithIdentifier:@"RootView"];
UINavigationController *navigationController = [UINavigationController alloc] initWithRootViewController:viewController];
[self presentViewController:navigationController animated:YES completion:nil];

Please see my sample code it works for me

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    [self showAlert];
    return YES;
}
-(void)showAlert
{
    UIAlertView *alertForCall = [[UIAlertView alloc]initWithTitle:@"" message:@"time to choose" delegate:self   cancelButtonTitle:@"YES" otherButtonTitles:@"NO", nil];
    [alertForCall show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1)
    {
        self.viewController = [[NewsTestViewController alloc] initWithNibName:@"NewsTestViewController" bundle:nil];
        UINavigationController *testNavi = [[UINavigationController alloc]initWithRootViewController:self.viewController];
        self.window.rootViewController = testNavi;
        [self.window makeKeyAndVisible];

    }
}

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