2011-09-09 81 views
0

我需要在沒有Interface Builder幫助的情況下使用空模板上的控制器創建自己的視圖。我需要在AppDelegate中編寫什麼?在空模板中查看

回答

2

XCode - >新建項目 - >選擇基於窗口的模板。然後從資源中刪除MainWindow.xib,從info.plist中刪除這個鍵「Main Nib file base name」。在main.m文件中 int retVal = UIApplicationMain(argc,argv,nil,@「AppDelegate」); //第四個參數是你的AppDelegate的名字。在AppDelegate.h中刪除IBOutlet UIWindow的屬性/合成,因爲您根本沒有使用IB ..

// In your AppDelegate.m  
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

     window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
     [window makeKeyAndVisible]; 

      // In your AppDelegate.h AnotherViewController* mainViewController; 
      mainViewController = [[AnotherViewController alloc] init]; 
      [[mainViewController view] setFrame:[UIScreen mainScreen].applicationFrame]; 
      [window addSubView:[mainViewController view]]; 
    } 

// In your AnotherViewController implement 
- (void)loadView 
{ 
    // Lets say you have a UITableView 
    UITableView* tableView = [[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain] autorelease]; 
    [self setView:tableView]; 
} 
+0

它的變體不起作用...您能測試它嗎? – OdNairy

+0

什麼不起作用。我更新了我的代碼。 AnotherViewController * mainViewController聲明爲iVar。並向您展示了該viewController的loadView方法 – 0x8badf00d