標籤:
---恢複內容開始---
由於Xcode6之後,預設建立storyboard而非xib檔案,而作為初學,瞭解xib的載入原理很重要,所以,需要建立一個沒有storyboard的項目
1. 建立一個新的工程
2. 選擇僅一個視圖的模板
選擇 Single View Application , 點擊Next
3. 填寫項目資訊
不需要選擇core data,填好資訊後,點擊next,然後點擊create
4. 刪除storyboard和launchscreen.xib檔案
將storyboard和launchscreen扔進廢紙簍
5. 修改info.plist檔案
刪除info.plist檔案中Main storyboard file base name和Launch screen interface file base name兩個屬性
6. 建立user interface的視圖xib檔案
點擊next,然後Save as “HelloWorldView”點擊Create
從object library中拖出一個Lable,再拖出一個button形成下面圖即可
7. 修改視圖控制器檔案
建立視圖和控制器的關聯,Xcode預設建立了ViewController.h和ViewController.m檔案,所以就不用自己建立了
1)修改xib視圖檔案的File‘s Owner
點擊列表中的File‘s Owner,按command+option+3 開啟 Identity Inspector,修改Custom Class中的Class為ViewController
2)建立輸出口和動作方法的關聯
點擊中間的兩個圈,開啟輔助編輯器
選定Lable視圖同時按住control鍵拖到ViewController.h的@interface與@end中間會快顯功能表,按照填寫內容,然後斷行符號建立輸出口
建立button的動作方法也是選定視圖並按住controll鍵拖到輔助編輯區
建立好關聯後,ViewController.h的代碼變為:
1 #import <UIKit/UIKit.h>2 3 @interface ViewController : UIViewController4 @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屬性進行關聯
關聯好的串連檢查器如下所示
3) 修改ViewController.m的代碼
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 //建立window13 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 }9. 運行程式
command+R
初學iOS很多內容不正確的,請批評指出,謝謝
iOS學習筆記(2)--Xcode6.1建立僅xib檔案無storyboard的hello world應用