iOS Swift3检查ViewController是否已初始化

时间:2017-03-03 10:04:02

标签: ios swift uiviewcontroller swift3

有没有办法检查ViewController是否已在iOS中初始化?我有一个放松Segue,它不起作用我某个ViewController以前没有被初始化。

3 个答案:

答案 0 :(得分:0)

您可以使用isKindOf类检查它,

if view.isKind(of:UIViewcontroller.self) {
   //do your code while you know the view is initialised.
}

答案 1 :(得分:0)

查找根视图控制器然后使用它。

if self == UIApplication.shared.keyWindow?.rootViewController {
    /*do stuff*/
}

或者你也可以用它

if self.window.rootViewController is MyViewController {
//do something if it's an instance of that class
}

答案 2 :(得分:0)

UINavigationController管理在该实例调用的所有viewControllers的数组(UIViewControllers),如下所示

对于swift 3

let viewControllers: [UIViewController] = self.navigationController!.viewControllers
for aViewController in viewControllers {
if aViewController is YourViewController {

   // get vc here

    break
  }
}

对于目标 - c

for (UIViewController *controller in self.navigationController.viewControllers) {

        //Do not forget to import AnOldViewController.h
        if ([controller isKindOfClass:[YourViewController class]]) {

                   // get vc here
            break;
        }
    }