Xcode學習筆記之WindowBase程式添加View是本文要介紹的內容,主要講解了在xcode中實現WindowBase程式添加View的案例,不多說,我們先來看詳細內容。
1、建立一個基於windowbase的程式。你會發現它只有一個MainWindow.xib,開啟這個檔案,拖拽一個View Controller控制項到的MainWindow.xib視窗。
2、按右鍵Classes檔案夾,為項目添加新的檔案。如:
選擇檔案類型為Cocoa Touch Class,注意勾選上Targeted for iPad以及With XIB for user interface產生xib檔案)
點擊確定並給類起個名字,我起的是TestViewController,回到工程會發現多了3個檔案:TestViewController.h, TestViewController.m,TestViewController.xib
最好將這個xib檔案拖入到Resources檔案夾裡。
3、雙擊在interface builder中開啟MainWindow.xib,在右側的懸浮視窗裡面的最上面的3個標籤,分別選中第一個標籤(屬性標籤)並在nib name那一欄點擊三角表徵圖在彈出的選項中選擇TestViewController,這樣就將MainWindow.xib和TestViewController.xib關聯起來了。再選擇第4個標籤(ID標籤)並點擊Class的三角表徵圖在彈出的類裡面選中TestViewController,這樣就將TestViewController.xib和TestViewController類關聯起來了。
4、在XXXAppDelegate.h中添加如下代碼,藍色字型為新增代碼
- #import <UIKit/UIKit.h>
- @class TestViewController;
- @interface WindowBaseTestAppDelegate : NSObject <UIApplicationDelegate> {
- UIWindow *window;
- TestViewController *viewController;
- }
- @property (nonatomic, retain) IBOutlet UIWindow *window;
- @property (nonatomic, retain) IBOutlet TestViewController *viewController;
- @end
在XXXAppDelegate.m中添加如下代碼,
- #import "WindowBaseTestAppDelegate.h"
- #import "TestViewController.h"
- @implementation WindowBaseTestAppDelegate
- @synthesize window;
- @synthesize viewController;
- #pragma mark -
- #pragma mark Application lifecycle
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- // Override point for customization after application launch.
- [window addSubview:viewController.view];
- [self.window makeKeyAndVisible];
- return YES;
- }
- ...
- - (void)dealloc {
- [viewController release];
- [window release];
- [super dealloc];
- }
5、開啟MainWindow.xib檔案,滑鼠左鍵單擊WindowBase ..之後滑鼠右鍵按住它拖拽到View Con..在彈出的視窗中選中viewController,儲存之。
到這裡算是大功告成了。ps:為了使得效果明顯一點,你最好給TestViewController.xib檔案添加一個控制項什麼的。
小結:Xcode學習筆記之WindowBase程式添加View的內容介紹完了,希望本文對你有所協助。更多相關xcode的內容,請參考編輯精選。