IOS: How To Get A View Controller instance
To obtain a View Controller instance, you can use the value of its public member variable instead of re-opening a piece of memory, and the original Instance does not just need a block of memory area
Instance method
-(RootViewController *) rootViewController {UIResponder * next = self. nextResponder; do {if ([next isKindOfClass: [RootViewController class]) {return (RootViewController *) next;} next = next. nextResponder;} while (next! = Nil); return nil ;}Class Method
+ (RootViewController *) rootViewControllerWithView :( UIView *) view {UIResponder * next = view. nextResponder; do {if ([next isKindOfClass: [RootViewController class]) {return (RootViewController *) next;} next = next. nextResponder;} while (next! = Nil); return nil ;}
I personally feel that the class method is better. It can be called only in the class of the instance you need. The VIew is generally your current VIew and only needs to be written once, reduces the number of demos to a certain extent