首先,新建一個Window-based Application的Project,然後你會在左方看到如下圖所示的檔案清單。

接下來請毫不猶豫的砍掉Classes資料夾與Resources資料夾內的MainWindow.xib。刪除的同時會有視窗跳出,請選擇Also Move to Trash。

打開Resources資料夾中的plist,刪除下圖中紅色圈選列並存檔關閉。

然後我們在Other Sources內的main.m開始編寫程式碼,有兩個最重要的介面要實作,分別是UIViewController與UIApplicationDelegate。 UIViewController的功用為建立應用程式所需之視圖,當被載入時建立一個UIView,然後在view上加入一個UILabel顯示文字;UIApplicationDelegate則是產生UIWindow為應用程式外框,載入UIViewController並顯示。
接著修改main function中UIApplicationMain的最後一個参数delegateClassName為UIApplicationDelegate實作的名稱即可。
整體流程為main function->call delegate->產生UIWindow->建立ViewController->產生視圖->啟動完成。
接著修改main function中UIApplicationMain的最後一個参数delegateClassName為UIApplicationDelegate實作的名稱即可。
整體流程為main function->call delegate->產生UIWindow->建立ViewController->產生視圖->啟動完成。
#import <UIKit/UIKit.h> @interface UsingNoNibViewController : UIViewController @end @implementation UsingNoNibViewController -(void) loadView { // 建立一個UIView(applicationFrame為扣除StatusBar的範圍) UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; contentView.backgroundColor = [UIColor blackColor]; // 建立UILabel UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 30.0f)]; label.text = @"不使用NIB"; label.center = contentView.center; label.textAlignment = UITextAlignmentCenter; label.textColor = [UIColor whiteColor]; label.backgroundColor = [UIColor clearColor]; // 加入到Application Frame中 [contentView addSubview:label]; [label release]; self.view = contentView; [contentView release]; } @end @interface UsingNoNibAppDelegate : NSObject<UIApplicationDelegate> @end @implementation UsingNoNibAppDelegate -(void) applicationDidFinishLaunching:(UIApplication *)application { // 建立UIWindow(bounds為畫面全部範圍) UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // 建立ViewController UsingNoNibViewController *controller = [[UsingNoNibViewController alloc] init]; [window addSubview:controller.view]; [window makeKeyAndVisible]; } @end int main(int argc, char *argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; int retVal = UIApplicationMain(argc, argv, nil, @"UsingNoNibAppDelegate"); [pool release]; return retVal; }出來結果就像下圖,完工!!!

沒有留言:
張貼留言