我在iPhone应用程序中有两个视图控制器。
1。)firstvc
2。)secondvc
在我的第一个VC中,我有一个按钮。通过敲击该按钮,我在presentModalViewController
中打开了第二vc。看波纹码。
- (IBAction)buttonClicked:(id)sender
{
SecondVC *secondVC = [SecondVC alloc] initWithNibName:@"SecondVC" bundle:nil];
[self.navigationController presentModalViewController:secondVC animated:YES];
}
现在搬到了secondvc。在SecondVC上,我有创建导航栏以及"取消"按钮作为左barbuttonItem。我在取消按钮上设置一个按钮单击事件。在这种方法中,我想删除第二vc。Bellow方法在第二vc中。看波纹码。
- (void)cancelButtonClicked
{
[self dismissModalViewControllerAnimated:YES];
}
此代码不起作用。我不能通过此代码解雇第二vc。请提出另一个技巧。
将按钮代码更改为此。
- (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];
}
您将dismissModalViewControllerAnimated:
消息发送到错误的对象。
由于您通过导航控制器介绍了模态视图控制器,因此您应该致电:
[self.presentingViewController dismissModalViewControllerAnimated:YES];
这将适用于iOS 5和更新。如果您仅针对iOS 5和较新的目标,您还可以考虑使用它在其上可用的较新方法来管理模态视图控制器:
– presentViewController:animated:completion:
– dismissViewControllerAnimated:completion:
,但我不认为这是强制性的。
如果要支持iOS 4及以上的iOS,则应在模态视图控制器中添加属性:
@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];
尝试当前的secondvc ...
[self presentModalViewController:secondVC animated:YES];
请用以下...代码
替换您的代码- (IBAction)buttonClicked:(id)sender
{
SecondVC *secondVC = [SecondVC alloc] initWithNibName:@"SecondVC" bundle:nil];
[self presentModalViewController:secondVC animated:YES];
}
- (void)cancelButtonClicked
{
[self dismissModalViewControllerAnimated:YES];
}