Xcode學習筆記之WindowBase程序添加View
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的內容,請參考編輯推薦。