ios - 如果更改了呈现 View Controller ,如何关闭模态视图 Controller ?

标签 ios objective-c uiviewcontroller

我在 iPad 上展示模态视图 Controller ,它会在展示时更改展示 View Controller 。例如:

  1. 当用户在 TableView 中选择一个单元格时, View Controller VC 会呈现模态视图 Controller 。
  2. 用户在模态视图 Controller 上选择一个项目,然后打开另一个 VC 实例来代替第一个。重要的是,替换第一个的 View Controller 实例属于同一类型。
  3. 模态视图 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/

相关文章:

html - 在父窗口中打开 iFrame 中的链接(无法控制子站点)

ios - 如何返回由一堆异步 HTTP 请求组合而成的对象数组?

objective-c - 如何以编程方式触发方法 "tabBarController:didSelectViewController:"?

iphone - 当有多个 NSURLConnection 时,如何确定哪个 NSURLConnection 完成了加载?

ios - 直接在自定义 Getter/Setter 中设置 NSUserDefaults

ios - SDK 'iOS 8.4' 不允许临时代码签名

iphone - 不让 UIButton 拖到 Circle 外

ios - 在后台创建 session 下载任务

objective-c - GCD 在 iOS5 中崩溃

适用于hidesBottomBarWhenPushed的iOS自定义动画