init初始化简单实用

##sinitWithFrame 和 initWithCoder

当我们所写的程序里没用用Nib文件(XIB)时,用代码控制视图内容,需要调用initWithFrame去初始化

1
2
3
4
5
6
7
- (id)initWithFrame:(CGRect)frame
{
if (self =[superinitWithFrame:frame]) {
// 初始化代码
}
return self;
}

用于视图加载nib文件,从nib中加载对象实例时,使用 initWithCoder初始化这些实例对象

1
2
3
4
5
6
7
- (id)initWithCoder:(NSCoder*)coder
{
if (self =[superinitWithcoder:coder]) {
// 初始化代码
}
return self;
}

Assuming you have storyboard, go to storyboard and give your VC an identifier (inspector), then do:

1
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"IDENTIFIER"]; [self.navigationController pushViewController:vc animated:YES];

Assuming you have a xib file you want to do:

1
UIViewController *vc = [[UIViewController alloc] initWithNibName:@"NIBNAME" bundle:nil]; [self.navigationController pushViewController:vc animated:YES];

Without a xib file:

1
UIViewController *vc = [[UIViewController alloc] init]; [self.navigationController pushViewController:vc animated:YES];

从xib中加载UIview

1
2
3
4
5
6
  NSArray *niblets = [[NSBundle mainBundle] loadNibNamed:@"sample" owner:self options:NULL];
for (id theObject in niblets)
{
if ([theObject isKindOfClass:[UIViewController class]])
[self.navigationController pushViewController:theObject animated:YES];
}