简体   繁体   English

创建没有笔尖的视图控制器

[英]Creating a view controller without a nib

In AppDelegate, I want to create a UIViewController subclass and add it's view. 在AppDelegate中,我想创建一个UIViewController子类并添加它的视图。 The viw itself will be specified in code - there is no nib. viw本身将在代码中指定 - 没有笔尖。

Based on the apple docs, I should use 根据苹果文档,我应该使用

initWithNibName:nil bundle:nil];

and then in loadView of the controller, I add my subviews etc. 然后在控制器的loadView中,我添加我的子视图等。

However, the follwing test code below does not work for me. 但是,下面的后续测试代码对我不起作用。 I modelled the AppDelegate code on Apple's PageControl demo , simply because my app will implement a similar structure (specifically a base controller to manage a paged scroll view, and an array of other controller's to build the pages). 我在Apple的PageControl演示中对AppDelegate代码进行了建模,仅仅因为我的应用程序将实现类似的结构(特别是用于管理分页滚动视图的基本控制器,以及用于构建页面的其他控制器阵列)。

But I suspect my AppDelegate code is the problem, since logging proves that initWithNibName:: and loadView both fire. 但我怀疑我的AppDelegate代码是问题,因为日志记录证明initWithNibName ::和loadView都会触发。 The app as below runs, but the screen is blank. 下面的应用程序运行,但屏幕为空白。 I am expecting a green view with a label. 我期待带有标签的绿色视图。

AppDelegate AppDelegate中

        - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        ScrollerController *controller = [[ScrollerController alloc] initWithNibName:nil bundle:nil];
        [self.window addSubview:controller.view];
        [self.window makeKeyAndVisible];
        return YES;
    }

ScrollerController (the UIViewController subclass) ScrollerController (UIViewController子类)

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)loadView{
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
    UIView *contentView = [[UIView alloc] initWithFrame:applicationFrame];
    contentView.backgroundColor = [UIColor greenColor];
    self.view = contentView;

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 100, 40)];
    [label setText:@"Label created in ScrollerController.loadView"];
    [self.view addSubview:label];
}

Try to use: self.window.rootViewController = controller; 尝试使用: self.window.rootViewController = controller; instead of [self.window addSubview:controller.view]; 而不是[self.window addSubview:controller.view];

Note, that you should also @synthesize window; 注意,你还应该@synthesize window; and create it self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 并创建它self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

Instead of initWithNibNamed:, just use alloc and init or any of the other designated initalizers for the view controller. 而不是initWithNibNamed:,只需使用alloc和init或任何其他指定的initalizer用于视图控制器。 Here is an example from a project 这是一个项目的例子

hoverViewController=[[BDHoverViewController alloc] initWithHoverStatusStyle:BDHoverViewStatusActivityProgressStyle];
self.window.rootViewController=hoverViewController;
[self.window makeKeyAndVisible];

also, the correct form( for now anyways) for adding the root view controller to the window in the app delegate is like this: 另外,将根视图控制器添加到app delegate中的窗口的正确形式(现在无论如何)如下所示:

self.window.rootViewcontroller=controller;
[self.window makeKeyAndVisible];

You don't need to add the view to the window. 您无需将视图添加到窗口。 The above code does it automatically. 上面的代码自动完成。

Good luck, 祝好运,

T Ť

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

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