我在iphone应用程序中有两个视图控制器。
1.)FirstVC
2.)SecondVC
在我的FirstVC中,我有一个按钮。通过点击该按钮,我在presentModalViewController中打开了SecondVC。看看下面的代码。
- (IBAction)buttonClicked:(id)sender
{
SecondVC *secondVC = [SecondVC alloc] initWithNibName:@"SecondVC" bundle:nil];
[self.navigationController presentModalViewController:secondVC animated:YES];
}现在转移到SecondVC。在SecondVC上,我有创建导航栏以及作为leftBarButtonItem的“取消”按钮。我在取消按钮上设置了一个按钮点击事件。在这种方法中,我想忽略SecondVC。bellow方法是在SecondVC中实现的。看看下面的代码。
- (void)cancelButtonClicked
{
[self dismissModalViewControllerAnimated:YES];
}这个代码不起作用。我不能因为这个代码而忽略SecondVC。请推荐另一种技巧。
发布于 2012-10-30 20:17:15
将按钮代码更改为以下代码。
- (IBAction)buttonClicked:(id)sender
{
SecondVC *secondVC = [SecondVC alloc] initWithNibName:@"SecondVC" bundle:nil];
[self presentViewController:secondVC animated:YES completion:NULL];
}在cancelButtonClick上
-(void)cancelButtonClicked {
[self.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
}发布于 2012-10-30 20:16:29
您正在将dismissModalViewControllerAnimated:消息发送到错误的对象。
由于您通过导航控制器呈现了模式视图控制器,因此您应该调用:
[self.presentingViewController dismissModalViewControllerAnimated:YES];这将在iOS 5和更高版本上运行。如果您只针对iOS 5和更高版本,您还可以考虑使用它上可用的新方法来管理模式视图控制器:
– presentViewController:animated:completion:
– dismissViewControllerAnimated:completion:但我不认为这是强制性的。
如果你想支持iOS 4和更早的版本,你应该在你的模式视图控制器中添加一个属性:
@interface SecondVC : UIViewController
@property (nonatomic, weak/assign) UIViewController* presentingController;
...
@end并在以模式显示控制器之前对其进行设置:
- (IBAction)buttonClicked:(id)sender
{
SecondVC *secondVC = [SecondVC alloc] initWithNibName:@"SecondVC" bundle:nil];
secondVC.presentingController = self.navigationController;
[self.navigationController presentModalViewController:secondVC animated:YES];
}然后,您将使用:
[self.presentingController dismissModalViewControllerAnimated:YES];发布于 2012-10-30 20:16:40
尝试将此用于present secondVC...
[self presentModalViewController:secondVC animated:YES];https://stackoverflow.com/questions/13138706
复制相似问题