1. 用XIB來布局并顯示到模擬器
file--> new --> file --> Cocoa Touch Class --> name : VCRoot ,勾選also create xib file.
生成文件:
點(diǎn)開VCRoot.xib, 在右下角第三個(gè)選項(xiàng)添加控件唉俗。得到如圖結(jié)果:
讓xib視圖顯示到模擬控制器:
- 首先在info.plist中刪除main storyboard文件
- 在AppDelegate.m中添加代碼
頭文件 #import "VCRoot.h"
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1d81}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #bb2ca2}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s6 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s7 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s8 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//創(chuàng)建一個(gè)窗口對(duì)象
self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
//創(chuàng)建根視圖控制器對(duì)象
//參數(shù)一:創(chuàng)建時(shí)加載的XIB資源文件名。加載XIB作為視圖控制器視圖
//參數(shù)二:指主文件包拜姿,XIB所在的位置
VCRoot* root=[[VCRoot alloc]initWithNibName:@"VCRoot" bundle:[NSBundle mainBundle]];
self.window.rootViewController=root;
[self.window makeKeyAndVisible];
return YES;
}
即可顯示:
2. XIB控件的創(chuàng)建
先創(chuàng)建一個(gè)project,之后
file--> new --> file --> Cocoa Touch Class --> name : VCRoot 冯遂,這次不勾選 also create xib file.
然后再添加一個(gè)新文件:
file--> new --> file --> ios/Userinterface/empty
得到一個(gè)自己添加的xib文件。
- xib文件與VCRoot的綁定:
點(diǎn)擊左側(cè)圖標(biāo) file owner, 右上角class信息顯示的是NSObject谒获,自己輸入VCRoot蛤肌,實(shí)現(xiàn)xib文件與VCRoot的綁定。
在右下角控件找到View控件批狱,添加上去裸准。
-
file owner和新添加的view的綁定:
點(diǎn)擊file owner --> 右上角第六個(gè)選項(xiàng) --> 拖住view加號(hào),和顯示的視圖相連:
右上角變?yōu)椋?/p>
修改代理設(shè)置
重復(fù)第一部分步驟:
-----首先在info.plist中刪除main storyboard文件
-----在AppDelegate.m中添加代碼
頭文件 #import "VCRoot.h"
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1d81}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #bb2ca2}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s6 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s7 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s8 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//創(chuàng)建一個(gè)窗口對(duì)象
self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
//創(chuàng)建根視圖控制器對(duì)象
//參數(shù)一:創(chuàng)建時(shí)加載的XIB資源文件名赔硫。加載XIB作為視圖控制器視圖
//參數(shù)二:指主文件包炒俱,XIB所在的位置
VCRoot* root=[[VCRoot alloc]initWithNibName:@"VCRoot" bundle:[NSBundle mainBundle]];
self.window.rootViewController=root;
[self.window makeKeyAndVisible];
return YES;
}
修改xib中的背景顏色后,運(yùn)行爪膊,可以看到xib和虛擬機(jī)運(yùn)行結(jié)果一致:
3. 設(shè)置控件屬性
新添加一個(gè)控件权悟,點(diǎn)擊控件,看右上角第四選項(xiàng)推盛,可以更改控件屬性峦阁。
4. 控件屬性和事件的同步
選中視圖,點(diǎn)擊右上方中間圖標(biāo):
顯示出代碼:
要將左側(cè)視圖與右側(cè)代碼綁定:
- 找到對(duì)應(yīng)的頭文件耘成,進(jìn)入榔昔,點(diǎn)擊左側(cè)某個(gè)控件,按住ctrl瘪菌,拖拽到右側(cè)位置:
- 對(duì)于按鈕來說撒会,還需要同步事件
同樣,點(diǎn)擊左側(cè)某個(gè)控件师妙,按住ctrl诵肛,拖拽到右側(cè)位置:選擇最上面 action:
最后生成結(jié)果:
同時(shí)在相應(yīng)的VCRoot.m文件中也創(chuàng)建出事件函數(shù):
注意:如果想刪除某段代碼,要點(diǎn)擊對(duì)應(yīng)控件疆栏,在右上角:
刪除Owner曾掂,再刪除代碼,否則會(huì)導(dǎo)致程序崩潰壁顶。也就是說珠洗,要?jiǎng)h除的屬性前面一定是空心小圓圈。
之后再去VCRoot.m中實(shí)現(xiàn)事件函數(shù):
最終結(jié)果: