XIB使用介绍:
经过上面的几步,就可以轻松的玩Xib了,然后就可以拖控件到Xib的UI中了
1. 首先我们删除一些不需要的东西:
删除info.plist 中的 main Storyboard file base name main 这一行
2. 然后我们创建一个新的视图控制器
红色箭头Also create XIB file要勾选上
这个时候,三个文件创建成功了,其中有一个RootViewController.xib文件,在这个里面就看到了一个像手机一样的视图,我们可以在右边进行拖拽一些控件在上面
3. 下面启动这个视图代码要在代理AppDelegate.m书写:
要引入#import “RootViewController.h”
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//创建一个窗口对象
self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
//方法一:显式加载xib文件
//创建根视图控制器对象
//参数一:创建时加载xib资源文件名,加载xib作为视图控制器视图
//参数二:是指主文件包,xib所在的位置,mainBundle是主资源文件包。如果传nil,函数会自动到mainBundle中查找
// RootController * root = [[RootController alloc]initWithNibName:@"RootController" bundle:[NSBundle mainBundle]];
//RootController * root = [[RootController alloc]initWithNibName:@"RootController" bundle:nil];
//方法二:隐式加载xib文件
//如果系统中有xib的名字(RootController.xib)和类名字相同(RootController),
//init函数会自动去加载RootController.xib文件
RootViewController *root = [[RootViewController alloc]init];
UINavigationController *_nav = [[UINavigationController alloc]initWithRootViewController:root];
self.window.rootViewController = _nav;
[self.window makeKeyAndVisible];
return YES;
}
看看工程的目录
经过上面的几步,就可以轻松的玩Xib了,然后就可以拖控件到Xib的UI中了