视图控制器引用的获取

*keyWindow指向的视图才可以被看到,如果指向其他的视图,那么原来的视图会被释放掉

1.获取到 Storyboard

1.1通过名字

UIStoryboard*storyboard = [UIStoryboardstoryboardWithName:@"Main"bundle:nil];

1.2通过 self.storyboard

UIStoryboard*storyboard =self.storyboard;

2.获取到 storyboard 的视图控制器

2.1通过 Storyboard ID

self.view.window.rootViewController= [storyboardinstantiateInitialViewController];

self.view.window.rootViewController= [storyboardinstantiateViewControllerWithIdentifier:@"viewController"];

3.获取到使用 xib 创建视图控制器

MyViewController*vc = [[MyViewControlleralloc]initWithNibName:@"MyViewController"bundle:nil];

4.代码设置导航栏的根视图控制器

MyNavigationController*nv = [[MyNavigationControlleralloc]initWithRootViewController:vc];

5.通过 keyWindow 来获取到视图控制器

_playingVC= [[UIApplicationsharedApplication].keyWindow.rootViewController.storyboardinstantiateViewControllerWithIdentifier:@"ViewController"];

6.keyWindow 来设置根视图控制器,可以直接跳转页面

[UIApplicationsharedApplication].keyWindow.rootViewController= [storyboardinstantiateInitialViewController];

7.自己的window

WelComeViewController*vc = [self.window.rootViewController.storyboardinstantiateViewControllerWithIdentifier:@"WelComeViewController"];

8.拿到XIB创建的UIView的引用

SendMessageView*vc = [[NSBundlemainBundle]loadNibNamed:@"SendMessageView"owner:selfoptions:nil][0];

你可能感兴趣的:(视图控制器引用的获取)