开发者

Do I have to allocate view when I make app without IB?

I want to make window based app without using IB. I created MyViewController by allocating it. Do I have to create view inside MyViewController also? Without allocating it, app crashes. For UITableViewController, UINavigationController,... I don't have to allocate view. What is difference?

- (B开发者_如何学编程OOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    viewController = [[MyViewController alloc]init];
    viewController.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    [self.window addSubview:viewController.view]; // app crashes here without allocating view explicitly
}   


you should either allocate the view explicitly just as you shown in your code or implement "loadView" in your controller so that the view is created when it is first shown on screen.


MyViewController *_myViewController = [[MyViewController alloc]init];
UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

_myViewController.view = myView
[myView release];
[self.window addSubview:myViewController.view];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜