---恢復內容開始---app
因爲Xcode6以後,默認建立storyboard而非xib文件,而做爲初學,瞭解xib的加載原理很重要,因此,須要建立一個沒有storyboard的項目編輯器
選擇 Single View Application , 點擊Nextide
不須要選擇core data,填好信息後,點擊next,而後點擊createatom
將storyboard和launchscreen扔進廢紙簍spa
刪除info.plist文件中Main storyboard file base name和Launch screen interface file base name兩個屬性3d
點擊next,而後Save as 「HelloWorldView」點擊Createcode
從object library中拖出一個Lable,再拖出一個button造成下面圖便可blog
建立視圖和控制器的關聯,Xcode默認建立了ViewController.h和ViewController.m文件,因此就不用本身建立了繼承
點擊列表中的File's Owner,按command+option+3 打開 Identity Inspector,修改Custom Class中的Class爲ViewControllerit
點擊中間的兩個圈,打開輔助編輯器
選定Lable視圖同時按住control鍵拖到ViewController.h的@interface與@end中間會彈出菜單,按照下圖填寫內容,而後回車建立輸出口
建立button的動做方法也是選定視圖並按住controll鍵拖到輔助編輯區
建立好關聯後,ViewController.h的代碼變爲:
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 @property (weak, nonatomic) IBOutlet UILabel *helloLable; 5 - (IBAction)helloButton:(UIButton *)sender; 6 7 @end
點擊xib列表中的File's Owner,而後按command+option+6 打開Connection Inspector查看輸出口和動做的關聯,將View與ViewController從UIViewController中繼承的view屬性進行關聯
關聯好的鏈接檢查器以下所示
1 #import "ViewController.h" 2 3 @interface ViewController () 4 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 // Do any additional setup after loading the view, typically from a nib. 12 13 } 14 15 - (void)didReceiveMemoryWarning { 16 [super didReceiveMemoryWarning]; 17 // Dispose of any resources that can be recreated. 18 } 19 20 - (IBAction)helloButton:(UIButton *)senhder { 21 self.helloLable.frame = CGRectMake(10, 50, 300, 40); 22 self.helloLable.text = @"Hello World!"; 23 } 24 @end
8. 修改AppDelegate.m 文件
在Xcode默認建立的AppDelegate.h文件中已存在如下代碼:
1 #import <UIKit/UIKit.h> 2 3 @interface AppDelegate : UIResponder <UIApplicationDelegate> 4 5 @property (strong, nonatomic) UIWindow *window; 6 7 @end
Xcode默認爲應用委託建立了window的屬性,打開AppDlegate.m文件,引入ViewController.h
重寫AppDlegate.m文件的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法
其餘代碼不做修改,代碼以下
1 #import "AppDelegate.h" 2 #import "ViewController.h" 3 @interface AppDelegate () 4 5 @end 6 7 @implementation AppDelegate 8 9 10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 11 // Override point for customization after application launch. 12 //建立window 13 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 14 //建立ViewController實例 15 ViewController *viewController = [[ViewController alloc] initWithNibName:@"HelloWorldView" bundle:nil]; 16 //設置window根視圖控制器 17 self.window.rootViewController = viewController; 18 self.window.backgroundColor = [UIColor whiteColor]; 19 [self.window makeKeyAndVisible]; 20 return YES; 21 }
command+R
初學iOS不少內容不正確的,請批評指出,謝謝