presentsViewController更改KVO

时间:2013-10-24 15:03:56

标签: ios uiviewcontroller

我正在尝试收听presentViewController的更改,但它看起来不像该属性符合KVO(或者至少我无法从中获取更改)。 UIViewController是否有一种方法可以在UIViewController被主动呈现时监听更改?

2 个答案:

答案 0 :(得分:4)

presentedViewController似乎不符合KVO标准,但可以通过覆盖UIViewController的相关演示/解雇方法来通知更改:

override func presentViewController(viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)?) {
    // will present view controller
    super.presentViewController(viewControllerToPresent, animated: flag, completion: completion)
}

override func dismissViewControllerAnimated(flag: Bool, completion: (() -> Void)?) {
    super.dismissViewControllerAnimated(flag, completion: completion)
    // did dismiss view controller
}

答案 1 :(得分:-7)

我不明白为什么你想知道UIViewController的实例是否被主动呈现,使用:

@property(nonatomic,readonly) UIViewController *presentedViewController;
但是,嘿,你是老板。我在一个独特的视图控制器中思考,并且只应该呈现一次,但我不知道这是否符合您的标准。

我认为最好的方法是,使用单词实例是我的小费:),例如

在头文件中:

+ (MyViewController *)instance;

在源文件中:

static MyViewController *instance = nil;

@implementation MyViewController

+ (MyViewController *)instance
{
    return instance;
}


- (void)viewDidLoad
{
   instance = self;
   [super viewDidLoad];
   // ...
}

现在,您可以从应用程序的其他部分:

if ([[MyViewController instance] presentedViewController]) 
{
    // here I'm been presented
}
相关问题