我的应用程序中有一个uialertview,它有两个按钮,第一个是取消按钮,第二个是ok。当我按下OK按钮时,我想转到具有故事板标识符“RootView”的视图控制器,但它不起作用。 我把这个代码放在appdelegate.m方法中。
- (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];
}
}
答案 0 :(得分:3)
你想推动新的视图控制器吗?你以模态方式呈现它吗?假设最后一个,也许这可以帮助你:
RootView *viewController = [storyboard instantiateViewControllerWithIdentifier:@"RootView"];
UINavigationController *navigationController = [UINavigationController alloc] initWithRootViewController:viewController];
[self presentViewController:navigationController animated:YES completion:nil];
答案 1 :(得分:1)
请查看我的示例代码,它适用于我
- (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];
}
}