手动打开故事板视图
我有一个使用Storyboard的应用程序。 在视图上有一个AlertViewDialog。
当用户单击第一个按钮(“是”)时,如何在故事板上打开其他视图?
我可以帮忙:
- 拖动视图然后转到Identity Inspector(快捷方式:选项+ apple + 3)。
- 选择新拖动的视图,并从标题故事板ID中的标识检查器中提供唯一名称。 //查看图片以供参考
创建viewController的SecondViewController类(.h和.m)子类。
然后从警报视图代码(如你所说,单击是时)
粘贴下面提到的代码
SecondViewController *svc =[self.storyboard instantiateViewControllerWithIdentifier:@"vinay"]; [svc setModalTransitionStyle:UIModalTransitionStyleCoverVertical]; [self presentViewController:svc animated:YES completion:nil];
如果出现任何问题,请告诉我。
这有助于:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; ClassNameViewController *viewController = (ClassNameViewController *)[storyboard instantiateViewControllerWithIdentifier:@"viewIdentifierOnStoryboard"]; [self presentModalViewController:viewController animated:NO];
你需要做的第一件事就是设置UIAlertView
委托这样做,将UIAlertViewDelegate
添加到你的@interface
,看起来像
@interface myClass : super // super could be anything like `UIViewController`, etc @end
然后在@implementation
你可以添加类似的东西
@implementation myClass ........... Some code - (IBAction)someActionMethod:(id)sender { UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:nil message:@"Would you like to move on?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil]; [myAlertView show]; // [myAlertView release]; Only if you aren't using ARC } - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { switch(buttonIndex) { case 1: SecondViewController *svc =[self.storyboard instantiateViewControllerWithIdentifier:@"secondViewController"]; [svc setModalTransitionStyle:UIModalTransitionStyleCoverVertical]; // [self presentViewController:svc animated:YES]; // Deprecated in iOS 6.0 [self presentViewController:svc animated:YES completion:nil]; // Introduced in iOS 5.0 break; default: break; } } @end
请记住在故事板中设置唯一标识符。 您可以通过转到.storyboard
和Identity Inspector(选择第三个)来完成此操作,您可以设置Storyboard ID
这是您需要与instantiateViewControllerWithIdentifier
中的内容匹配的内容,因此在上面的情况下它将是"secondViewController"
。 就这么简单。
记得在完成后忽略此视图,您将需要使用
[self dismissModalViewControllerAnimated:YES];
以上实际上已在iOS 6.0中弃用,但您可以使用
[self dismissModalViewControllerAnimated:YES completion:nil];
除了它在最后增加一个完成块之外,它做同样的事情。
希望这可以帮助。