可以[self.window makeKeyAndVisible]; 在设置rootviewcontroller之前调用

S.J*_*S.J 5 objective-c uiviewcontroller uiwindow ios appdelegate

我的要求是UITabBarController是rootviewcontroller,并且在第一次启动应用程序时,我想要显示UINavCon内部的登录过程,我正在显示它presentViewController.

我不希望第一次看到UITabBarController并且不想如何登录UINavCon作为模态弹出.

我想让用户体验如果应用程序第一次启动登录UINavCon应该是可见的.所以这是我的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

[self.window makeKeyAndVisible];//is it correct to call it here?

LoginVC *loginObj = [[LoginVC alloc]init];

self.navigationController = [[UINavigationController alloc] initWithRootViewController:cellPhoneNumber];

self.tabBarController = [[UITabBarController alloc]init];

self.window.rootViewController = self.tabBarController;

[self.tabBarController presentViewController:self.navigationController animated:NO completion:^{}];

return YES;
}
Run Code Online (Sandbox Code Playgroud)

我正在呼叫[self.window makeKeyAndVisible];第二行uiwindow alloc init.这样做是否正确,或者我可能遇到像viewcontroller没有接收事件或方向通知的问题?

HRM*_*HRM 5

您没有提到使用您的实现是否使代码正常工作.无论如何我最近做了类似的实现,我们需要在登录后提供登录控制器然后tabBarController,所以只需共享我的实现.

  1. 创建登录控制器并将其显示在didFinishLaunching方法中.

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    LoginController *loginCObj= [[[MainScreenController alloc]init]autorelease];
    UINavigationController *navigationControllerObj = [[[UINavigationController alloc]initWithRootViewController:loginObj]autorelease];
    self.window.rootViewController = navigationControllerObj;
    [self.window makeKeyAndVisible];
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在登录视图控制器中成功登录之后,调用appDelegate公共方法

    在登录控制器中

    AppDelegate *appDel = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDel  applicationLoggedInSuccesfully];
    
    Run Code Online (Sandbox Code Playgroud)

    在appDelegate文件中,添加如下方法:

    -(void)applicationLoggedInSuccesfully{
        UINavigationController *nv1 = [[[UINavigationController alloc] initWithNibName:nil bundle:nil]autorelease];
        TabController1 *v1 = [[[TabController1 alloc] initWithNibName:nil bundle:nil]autorelease];
        [nv1 pushViewController:v1 animated:NO];
    
        UITabBarController *tabController = [[[UITabBarController alloc] init]autorelease];
        tabController.viewControllers = @[nv1];
        tabController.delegate = self;
        self.window.rootViewController = tabController;
        [self.window makeKeyAndVisible];
    }
    
    Run Code Online (Sandbox Code Playgroud)

希望它会对你有所帮助.


Dai*_*jan 5

你可以随时调用它。调用它会影响窗口的 z-index 和屏幕属性。它不依赖于设置的任何特定内容。