6

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

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

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

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

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

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] ;
4

4 回答 4

1

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
}
于 2017-08-28T17:25:57.073 回答
1

感谢 Krunal 的详细回答。

感谢丹的支持

我发现问题而不是 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] ;
于 2017-08-28T18:33:38.587 回答
0

删除文件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,请尝试以下操作:

在此处输入图像描述

于 2017-08-29T09:30:59.343 回答
0

添加 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];
            }
于 2017-08-29T10:32:33.567 回答