//
// AppDelegate.h
//
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
//
// AppDelegate.m
//
#import "AppDelegate.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(@"application:%@",application);
NSLog(@"AppDelegete:%@",self);
NSLog(@"application.delegate:%@",application.delegate);
NSLog(@"AppDelegete.window(self.window):%@",self.window);
NSLog(@"application.windows:%@",application.windows);
NSLog(@"application.keyWindow:%@",application.keyWindow);
NSLog(@"AppDelegete.window.rootVC:%@",self.window.rootViewController);
return YES;
}
@end
//
// ViewController.m
//
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"ViewController:%@",self);
}
@end
2017-02-18 21:23:24.137 WZMainController[2680:211019] application:<UIApplication: 0x7fe209f00ed0>
2017-02-18 21:23:24.137 WZMainController[2680:211019] AppDelegete:<AppDelegate: 0x600000037da0>
2017-02-18 21:23:24.137 WZMainController[2680:211019] application.delegate:<AppDelegate: 0x600000037da0>
2017-02-18 21:23:24.137 WZMainController[2680:211019] AppDelegete.window(self.window):<UIWindow: 0x7fe209d124e0; frame = (0 0; 375 667); hidden = YES; gestureRecognizers = <NSArray: 0x60800005e450>; layer = <UIWindowLayer: 0x60800003d220>>
2017-02-18 21:23:24.138 WZMainController[2680:211019] application.windows:(
"<UIWindow: 0x7fe209d124e0; frame = (0 0; 375 667); hidden = YES; gestureRecognizers = <NSArray: 0x60800005e450>; layer = <UIWindowLayer: 0x60800003d220>>"
)
2017-02-18 21:23:24.138 WZMainController[2680:211019] application.keyWindow:(null)
2017-02-18 21:23:24.138 WZMainController[2680:211019] AppDelegete.window.rootVC:<ViewController: 0x7fe209d11dc0>
2017-02-18 21:23:24.144 WZMainController[2680:211019] ViewController:<ViewController: 0x7fe209d11dc0>
以上验证,通过sb启动,显示的window就是AppDelegate.h中的@property(strong)UIWindow *window; 而window.rootVC就是sb中绑定ViewController类的那个视图控制器。而application.keyWindow为null,因为UIApplication类的@property(nullable, nonatomic,readonly) UIWindow *keyWindow;可为空。故以后获取根控制器最可靠的方式是[UIApplication sharedApplication].windows[0];