界面使用XIB文件的幾種方法

注意:提示錯誤“loaded the "ViewController" nib but the view outlet was not set.”時,XIB文件中view的Referencing Outlets需連到File’s Owner上。

1. AppDelegate代理中initWithNibName方法

在AppDelegate代理中

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor grayColor];
self.window.rootViewController = [[SelectHeroViewController alloc] initWithNibName:@"SelectHeroViewController" bundle:nil];

即可

2.ViewController中initWithNibName方法

在ViewController中

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.view = [[[NSBundle mainBundle] loadNibNamed:@"SelectHero" owner:self options:nil] lastObject];
    }
    return self;
}

3.Identity inspector中設置

按1,AppDelegate代理中修改第3行

self.window.rootViewController = [[SelectHeroViewController alloc] initWithNibName:@"SelectHeroViewController" bundle:nil];
 改為
self.window.rootViewController = [[SelectHeroViewController alloc] init];

然后在xib文件Identity inspector中 選擇控制器類

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容