新建了一个工程使用下面获取rootVC用来弹窗时,弹出不来,发现RootViewControlle是nil
UIViewController *RootViewController()
{
id<UIApplicationDelegate> delegate= [UIApplication sharedApplication].delegate;
if([delegate respondsToSelector:@selector(window)]){
UIWindow *window = [(NSObject *)delegate valueForKey:@"window"];
UIViewController *controller = window.rootViewController;
while (controller.presentedViewController) {
controller = controller.presentedViewController;
}
return controller;
}
return nil;
}
需要删除 SceneDelegate 使用 AppDelegate
Xcode11之后新创建的工程会多出两个文件SceneDelegate。那么我们如何让它变回之前的那样的工程呢。
一、将这两个文件删除。
会报错There is no scene delegate set. A scene delegate class must be specified to use a main storyboard file.
二、将Info.plis 的 UIApplicationSceneManifest 删除
三、将AppDelegate.m中的UISceneSession lifecycle注释掉或者删掉。
四、在AppDelegate.h加入window。在didFinishLaunchingWithOptions中加入UIWindow。
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (nonatomic, strong) UIWindow *window;
@end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = [[ViewController alloc] init];;
[self.window makeKeyAndVisible];
return YES;
}
删除后继续报错 whose view is not in the window hierarchy .我是demo里面遇到的,在viewDidLoad弹一个alertViewController,所以移到外面就好了
- 在 一个 ViewController 里面调用另外一个 ViewController 是出现这个错误:
该错误一般是由于在 viewDidLoad 里面调用引起的,解决办法是转移到 viewDidAppear 方法里面
- 在 AppDelegate.m 中调用遇到这个错误
解决办法1:
UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topRootViewController.presentedViewController)
{
topRootViewController = topRootViewController.presentedViewController;
}
//[topRootViewController presentViewController:yourController animated:YES completion:nil];
//or
[topRootViewController myMethod];
解决办法2:
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
LoginViewController* loginViewController = [mainstoryboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
[self.window makeKeyAndVisible];
//[LoginViewController presentViewController:yourController animated:YES completion:nil];
//or
[LoginViewController myMethod];
网友评论