gpt4 book ai didi

ios5 - dismissModalViewControllerAnimated 适用于 self 但不适用于 parentViewController

转载 作者:行者123 更新时间:2023-12-03 18:23:45 25 4
gpt4 key购买 nike

我让dismissModalViewControllerAnimated 在以下设置中正常工作,但对为什么它在self(modalViewController)而不是parentViewController 上工作感到困惑。

这是设置:

  • 我有一个带有导航按钮的 UITableViewController,它可以调用模态视图:


  • - (void)viewDidLoad
    {
    [super viewDidLoad];
    self.title = @"Root";

    _data = [NSArray arrayWithObjects:@"One", @"Two", nil];
    _detailController = [[DetailViewController alloc] init];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showAbout)];
    }

    - (void)showAbout
    {
    AboutViewController *abv = [[AboutViewController alloc] init];
    abv.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:abv animated:YES];
    }


    这是模态视图 Controller AboutViewController ,带有一个工具栏按钮,可触发关闭操作以关闭模态:


    - (IBAction)dismissAction:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
    }


    我的问题是为什么 [selfdismissModalViewControllerAnimated] 工作而不是 [self.parentViewControllerdismissModalViewControllerAnimated] ?这是 iOS 5 中的新功能吗?我认为只有 parentViewController 能够关闭子模态视图?

    谢谢!

    最佳答案

    [self dismissModalViewControllerAnimated:YES];已经工作了一段时间。如果你问我,这是 iOS 开发中最保守的 secret 之一。

    然而self.parentViewController not working 实际上是 iOS 5 的新功能。它已被“替换”为 self.presentingViewController .

    对于试图兼容 iOS 5 之前的代码,这会导致一个有趣的问题。因为你已经找到了self.parentViewController在 iOS 5 上返回 nil。和 UIViewControllers不回复presentingViewController iOS 5 之前的版本。

    它让我们做这样的事情:

    if ([self respondsToSelector:@selector(presentingViewController)]){
    [self.presentingViewController dismissModalViewControllerAnimated:YES];
    }
    else {
    [self.parentViewController dismissModalViewControllerAnimated:YES];
    }

    关于ios5 - dismissModalViewControllerAnimated 适用于 self 但不适用于 parentViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8831011/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com