ios loadView使用
1,下面这个例子,既会加载nib(前提是Test1ViewController初始化时加载nib),也会加载自定义的控件。
如果初始化时没有加载nib,如:Test1ViewController *viewController = [[Test1ViewController alloc] init];则只会加载自定义控件。
// // Test1ViewController.m // TestLoadView // // Created by dongway on 14-7-31. // Copyright (c) 2014年 dongway. All rights reserved. // #import "Test1ViewController.h" #include "stdio.h" @interface Test1ViewController () @end @implementation Test1ViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } -(void)loadView{ [super loadView];//初始化self.view,如果关联nib则初始化nib的信息到self.view;如果ViewController初始化时没有关联nib,则初始化一个空白UIView UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 70, 60, 30)]; button.titleLabel.text = @"jump"; button.backgroundColor = [UIColor redColor]; [button addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; printf("loadview\n"); } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. printf("test c"); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)jump{ [self dismissViewControllerAnimated:YES completion:nil]; } @end