2012-10-24 126 views
1

我想知道在xcode中創建新的「單一視圖」項目時需要執行哪些步驟,以實現: 1. viewController在沒有NIB的情況下初始化,而是以編程方式在其視圖中加載它自己的控件。
2.如何讓該視圖控制器的視圖爲load並致電viewDidLoad
3.使該控制器的視圖在所有控件的屏幕上可見。以編程方式查看控制器?

我如何去了解這個從這個函數:

-(BOOL)application:(UIApplication*)application didFinishLoadingWithOptions:(NSDictionary *)launchOptions 

我想修改一個新的Xcode項目,但我得到的是一個黑色的screeen,viewDidLoad中不會被調用

回答

1

這是您的應用程序委託的應用程序加載方法。

在那裏,您可能需要創建自定義視圖控制器的實例,並將其作爲rootViewController分配給您的應用程序委託didFinishLoading。應該有這樣一行:

// app delegate .h file 

#import "CustomViewController.h" 

@interface 
{ 
    ... 
    CustomViewController *myCustomVC; 
    ... 
} 

@property (nonatomic, retain) CustomViewController *myCustomVC; 


// app delegate .m file 
@implementation AppDelegate 

@synthesize myCustomVC; 

-(BOOL)application:(UIApplication*)application didFinishLoadingWithOptions:(NSDictionary *)launchOptions 
{ 
    ... 
    myCustomerVC = [[CustomViewController alloc] init]; 

    [self.window setRootViewController:myCustomVC]; 
    ... 
} 

然後自定義視圖控制器的viewDidLoad方法裏,你可以做到這一點作爲一個測試:

// custom view controller .m file 
-(void)viewDidLoad 
{ 
    self.view.backgroundColor = [UIColor redColor]; 
} 
1
UIViewController *myViewController = [[UIViewController alloc] init]; 
[myViewController.view setFrame:self.view.bounds]; 
[self.view addSubview:myViewController.view]; // if you want to add it in another viewcontroller 

// For testing, set the background color to something other than white (default) 

[myViewController.view setBackgroundColor:[UIColor greenColor]]; 

而你離開!

+0

誰是自己在self.view(3號線)? – Ted

+0

要在其中添加新視圖的視圖控制器。在一個新的單視圖項目中,這將在「ViewController.m」中,例如在viewDidLoad中。 –

+0

這是應該喚醒viewDidLoad? – Ted

1

您需要創建的UIViewController一個子類,並設置您的在loadViewviewDidLoad(取決於定製級別)的視圖層次結構

通過子類化UIViewController加載方法調用將爲您做出,因此您不必擔心關於得到越來越viewDidLoad

爲了讓屏幕的最簡單的方法是在你的應用程序委託,以將其設置爲應用程序窗口

rootViewControllerdidFinishLaunchingWithOptions:

self.window.rootViewController = [[MyViewControllerSubclass alloc] init]; 
0

試試這個上可見:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    HomeViewController *homeVC = [[HomeViewController alloc]init]; 
    [self.window setRootViewController:homeVC]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

從通用主界面刪除主(故事板參考)設置: enter image description here

添加啓動圖像: 而選擇的iOS-7在你隨後的左上角設置 enter image description here

相關問題