繁体   English   中英

如何在 Objective C 中以编程方式设置根视图控制器?

[英]How to set Root View Controller programatically in Objective C?

我是 iOS 开发的新手,试图学习如何以编程方式创建和设置视图。

我正在尝试在 Obj-C 中快速声明

window?.rootViewController = UINavigationController(rootViewController : ViewController()) 

项目:单视图应用程序。 尝试链接默认创建的 ViewController.h

根据 Krunals 的回答,我更新了代码,但模拟器中未显示导航控制器

Cmd+单击控制器不会导航到 ViewController 文件

#import "AppDelegate.h"
#import "ViewController.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIScreen *screen=[[UIScreen alloc]init];
    UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.window.makeKeyAndVisible;




    ViewController *controller = [[ViewController alloc] init];



    window.rootViewController = [[UINavigationController alloc] initWithRootViewController:controller] ;

在添加(用作导航的根控制器)到导航控制器堆栈之前初始化您的视图控制器ViewController

这是初始化简单视图控制器的示例代码

UIViewController *controller = [[UIViewController alloc] init];

这是使用故事板初始化的示例代码

ViewController *controller = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"<ViewController - string identifier of your view controller>"];

这是使用 NIB/Bundle 初始化的示例代码

ViewController *controller = [[ViewController alloc] initWithNibName:@"<ViewController - string NIB name>>" bundle:nil];

根据您的代码和以下评论,仅尝试使用此代码(从您的应用程序委托启动中删除其他代码):

// make sure your NIB name is 'ViewController' 

ViewController *controller = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
if (controller != nil) {
    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController: controller];
    self.window.makeKeyAndVisible;
} else {
   //print - your view controller is nil
}

感谢 Krunal 的详细回答。

感谢 dan 的支持

我发现问题而不是 self.window.rootViewController ,我输入了 window.rootViewController 。

设置 self.window.rootViewController 解决了问题。

我不知道 self.window.rootViewController 和 window.rootViewController 之间的区别以及问题的原因。

如果有人知道答案,请在评论中提供答案

#import "AppDelegate.h"
#import "ViewController.h"


@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    UIScreen *screen=[[UIScreen alloc]init];
    UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];


    self.window.makeKeyAndVisible;




    ViewController *controller = [[ViewController alloc] init];





    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:controller] ;

删除文件Main.storyboard并在执行此操作之前让Main interface选项为空。 在此处输入图片说明

并添加以下代码:

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
ViewController *vc = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];

更新:如果您想将故事板与UINavigationController一起使用,请尝试以下操作:

在此处输入图片说明

添加 UIViewController 并使用 UINavigationController 添加

      UIStoryboard *storyboard = self.window.rootViewController.storyboard;
           UIViewController *rootViewController= [storyboard instantiateViewControllerWithIdentifier:kIdentifier];
            [self setRootViewController:rootViewController];


        #pragma mark - Set RootView Controller
        -(void)setRootViewController:(UIViewController *)rootViewController {
            self.window.rootViewController = rootViewController;
            [self.window makeKeyAndVisible];
        }


         UIStoryboard *storyboard = self.window.rootViewController.storyboard;
           UIViewController *rootViewController= [storyboard instantiateViewControllerWithIdentifier:kIdentifier];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
    [self setRootViewController:navController];

-(void)setRootViewController:(UINavigationController *)rootViewController {
                self.window.rootViewController = rootViewController;
                [self.window makeKeyAndVisible];
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM