我在 iPad 上展示模态视图 Controller ,它会在展示时更改展示 View Controller 。例如:
- 当用户在 TableView 中选择一个单元格时, View Controller VC 会呈现模态视图 Controller 。
- 用户在模态视图 Controller 上选择一个项目,然后打开另一个 VC 实例来代替第一个。重要的是,替换第一个的 View Controller 实例属于同一类型。
- 模态视图 Controller 无法关闭或发生
EXC_BAD_ACCESS
异常。
失败的解除是可以理解的:呈现 View Controller 不再可用。基本上,我如何从不同的呈现 View Controller 中关闭这个呈现的模态视图 Controller ?
我已有的代码是:
ViewController1.m
- (void)showModalViewController:(UIViewController *)viewController
{
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
viewController.navigationItem.rightBarButton = [[UIBarButton alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissModalViewController)];
[self presentViewController:navigationController animated:YES completion:nil];
}
- (void)dismissModalViewController
{
[self dismissViewControllerAnimated:YES completion:nil];
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}
最佳答案
感谢您的建议,但我通过使用委托(delegate)解决了这个问题。呈现的 View Controller 定义了一个委托(delegate),用于在发生操作时通知呈现器。
ChildViewControler.h:
@protocol ChildViewControllerDelegate <NSObject>
- (void) childView:(ChildViewController *)childView didSelectItem:(Item *)item;
@end
ChildViewController.m:
// in interface
@property (nonatomic, weak) id <ChildViewControllerDelegate> delegate;
// in implementation
- (void)closeView:(Item *)anItem
{
[self.delegate childView:self didSelectItem:anItem];
}
ViewController1.m:
- (void)showModalViewController:(UIViewController *)viewController
{
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
viewController.navigationItem.rightBarButton = [[UIBarButton alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissModalViewController)];
// Different view controller types may be passed here so check is required...
if (viewController.class == [ChildViewController class]) {
((ChildViewController *)viewController).delegate = self;
[self presentViewController:navigationController animated:YES completion:nil];
}
- (void)childView:(ChildViewController *)childView didSelectItem:(Item *)item
{
[self dismissViewControllerAnimated:YES completion:nil];
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
// Perform action required with 'item'
}
关于ios - 如果更改了呈现 View Controller ,如何关闭模态视图 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21826215/