在應用中有一個uialertview
,有兩個按鈕,第一個是取消按鈕,另一個是OK按鈕,在按下OK按鈕時,應該跳到一個帶有storyboard標志符的視圖控制器RootView。應該怎麼實現?
- (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];
}
}
可嘗試在當前控制器中,彈出identifier為navigationController的控制器,在navigationController控制器中再push到rootView.改成這樣試試
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
UINavigationController *navigationController = [storyboard instantiateViewControllerWithIdentifier:@"navigationController"];
if (navigationController) {
[self presentViewController:navigationController animated:YES completion:nil];
}
接著在identifier 為navigationcontroller的控制器的viewwillappear中push到rootview
-(void)viewWillAppear {
RootViewController *rootController=[storyboard instantiateViewControllerWithIdentifier:@"RootView"];
if (rootController) {
[self.navigationController pushViewController:rootController animation:NO];
}
}