我有一个使用Storyboard的应用程序。在一个视图上有一个AlertViewDialog。
当用户点击第一个按钮(“是”)时,我如何在Storyboard上打开另一个视图?
我有一个使用Storyboard的应用程序。在一个视图上有一个AlertViewDialog。
当用户点击第一个按钮(“是”)时,我如何在Storyboard上打开另一个视图?
也许这可以帮助:
创建SecondViewController类(.h和.m),它是viewController的子类。
然后从警报视图代码中(正如您所说的当YES被点击时),粘贴以下代码:
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 <UIAlertViewDelegate>
// 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中设置唯一标识符。您可以通过进入您的.storyboard
并在Identity Inspector(选择第三个)中设置Storyboard ID
来完成此操作,这是您需要与instantiateViewControllerWithIdentifier
中的内容匹配的内容,因此在上面的情况下,它将是"secondViewController"
。就是这么简单。
完成后,请记得关闭此视图,您需要使用
[self dismissModalViewControllerAnimated:YES];
上述方法在iOS 6.0中已被弃用,但您可以使用
[self dismissModalViewControllerAnimated:YES completion:nil];
这个做的事情和之前一样,只不过在末尾添加了一个完成块。
希望能有所帮助。