一、在Storyboard中放置ViewController,然后可以用下面的方法加载Main.storyboard,ViewController要设置Identifier。
UIStoryboard *st = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [st instantiateViewControllerWithIdentifier:@"1”];
可以用下面的方法加载initialView.
UIStoryboard *st = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [st instantiateInitialViewController];
二、在xib中放置一个View,然后把file’s owner 设置为自己的ViewController类。不设置的话,没法连线,因为一定要保证file’s owner的View有值。但是设置file’s owner并且连线之后,把file’s owner去掉之后,之前的连线还是有用,控制器仍会响应之前的按钮事件,只要方法还在的话。用下面这句加载Xib。
ViewController *vc = [[ViewController alloc] initWithNibName:@"XibName" bundle:nil];
用 [[ViewController alloc] init] 的话,系统会自动去找和这个View同名的Xib文件,然后加载。没有的话,会生成一个没有View的ViewController。有的话,同样必须保证这个Xib的file’s owner 有View,不然会报'...the view outlet was not set.’,但是要连线的话,file’s owner还是得设置一个Viewcontroller.
三、同样在xib里面放置一个View,这时候可以自己自定义一个View类,然后可以设置xib中的View的class为自定义的View,然后可以连线到自定义的View。
//加载V1.xib,这里返回的是一个数组,因为一个xib中可以有多个VIew,并且如果你在xib中添加了手势的话,也会在这个array中,所以在有手势的时候要注意。
NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"V1" owner:nil options:nil] ;
ViewController *vc = [[ViewController alloc] init];
vc.view = [array lastObject];
这种情况相当于加载View,而不是Viewcontroller。
四、在Xib中放置一个Viewcontroller,这时候这个ViewController的class默认是UIViewController。然后用下面方式加载
NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"VC1" owner:nil options:nil];
UIViewController *vc = [array lastObject];
但是不能连线,所以必须在xib的ViewController的class设置自己的ViewController。然后可以连线了