Is there a way to check if a ViewController has been initialized in iOS? I have an unwind Segue which does not work i a certain ViewController has not been initialized before.
Asked
Active
Viewed 1,693 times
-3
-
if using navigation try this http://stackoverflow.com/a/42523549/5461400 – Harshal Valanda Mar 03 '17 at 10:07
-
What do you mean by "has been initialized"? Please post the code that's causing you problems. You'll get more and better answers If you show what you've tried. See [Ask] – Ashley Mills Mar 03 '17 at 10:28
-
Please post code and let us know your issue ("does not work"). – shallowThought Mar 03 '17 at 10:56
-
I perform a Segue, which is an unwind segue. If the destination was never called at runtime, it can not unwind segue to the destination. For example, i have a StartVC, which decides if the user is already logged in, thus sending the user to the contentVC, or send him to the loginVC. Now if i logout in an app session where the app never called the LoginVC, the app wont perform the unwind segue. That is why i need a way to catch this case and perform a manual segue. Thank you guys for helping! – Jochen Österreicher Mar 03 '17 at 11:05
-
1_Please_ , show some code! – Ashley Mills Mar 03 '17 at 11:15
-
The viewControllers are set up in the storyboard, so are the segues, they work if you open that loginVC in runtime, they do not work if loginVC is never accessed. I can not show you any code, i want you to show me code for how to check if my login was created! – Jochen Österreicher Mar 03 '17 at 11:21
3 Answers
0
You can check it using isKindOf class,
if view.isKind(of:UIViewcontroller.self) {
//do your code while you know the view is initialised.
}

Aashish
- 2,532
- 2
- 23
- 28
0
Find root view-controller then used it.
if self == UIApplication.shared.keyWindow?.rootViewController {
/*do stuff*/
}
or u can used it also
if self.window.rootViewController is MyViewController {
//do something if it's an instance of that class
}

Nishant Gupta
- 457
- 3
- 16
0
UINavigationController
manages the array(viewControllers
) of all UIViewControllers
which are invoked at that instance so do as below
For swift 3
let viewControllers: [UIViewController] = self.navigationController!.viewControllers
for aViewController in viewControllers {
if aViewController is YourViewController {
// get vc here
break
}
}
For objective - c
for (UIViewController *controller in self.navigationController.viewControllers) {
//Do not forget to import AnOldViewController.h
if ([controller isKindOfClass:[YourViewController class]]) {
// get vc here
break;
}
}

dahiya_boy
- 9,298
- 1
- 30
- 51
-
Thank you, but this only works if my view is insie a navigationController. The view i try to check for is a normal viewController. – Jochen Österreicher Mar 03 '17 at 11:19
-
@JochenÖsterreicher Please clear it either you wanted to check `UIView` or `UIViewController` – dahiya_boy Mar 03 '17 at 11:21
-
-
@JochenÖsterreicher If your required `UIViewController` is not in `NavController` that means that vc is not in memory so that `UIViewController` is not initialised at that instant. – dahiya_boy Mar 03 '17 at 11:29
-