OGeek|极客世界-中国程序员成长平台

标题: ios - 当 presentModalViewController 呈现任何 View Controller 时,我如何注意到? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 17:09
标题: ios - 当 presentModalViewController 呈现任何 View Controller 时,我如何注意到?

为了解决这个问题,我尝试使用 KVO(键值观察)来监控名为“presentedViewController”的属性。我制作了一个自定义 View Controller 作为基本 View Controller ,用于我的应用程序中所有 View Controller 的基本 View Controller 。然后,我将以下代码添加到 View Controller 的 initWithNibName:bundle:

- (id)initWithNibNameNSString *)nibNameOrNil bundleNSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self addObserver:self forKeyPath"presentedViewController" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}

并且,像这样覆盖观察方法:

- (void)observeValueForKeyPathNSString *)keyPath ofObjectid)object changeNSDictionary *)change contextvoid *)context {
    NSLog(@"object %@", object);
}

最后,我制作了一个从包含上述代码的基本 View Controller 派生的 View Controller 。 当我使用 presentModalViewController 方法使其成为模态视图 Controller 时。但是,观察我安装没有工作。我希望在调用该方法时会触发观察。不幸的是,什么也没发生。

简而言之,我有两个问题。首先是为什么那行不通。另一个是有什么好的方法可以实现这个目标。

编辑: 我能够通过@jailani habeeb 的方式获得成功。谢谢@jailani habeeb。 另外,我真的很想知道为什么KVO不起作用。像“title”这样的任何其他属性都可以正常工作..



Best Answer-推荐答案


presentModalViewController 在 iOS 6 中已被弃用。您可以使用 presentViewController:animated:completion: 代替。您可以在完成 block 中执行您的逻辑。

关于ios - 当 presentModalViewController 呈现任何 View Controller 时,我如何注意到?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20541834/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4