2013-11-02 55 views
0

我想用下面的代碼在我使用故事板的地方看到視圖,但是我沒有製作uiviewcontroller的場景初始視圖控制器。 這是我在我的AppDelegate的didFinishLaunchingWithOption中編寫的代碼。將UIViewcontroller的視圖添加到UIWindow中並不顯示

UIWindow* window = [UIApplication sharedApplication].keyWindow; 
abcViewController *controller = [[abcViewController alloc]init]; 
UIView *redView = [[UIView alloc] initWithFrame: CGRectMake ([[UIScreen mainScreen] bounds].origin.x+30, [[UIScreen mainScreen] bounds].origin.y+30, 260, 400)]; 
[redView setBackgroundColor:[UIColor redColor]]; 
UIView *greenView = [[UIView alloc] initWithFrame: CGRectMake (redView.frame.origin.x + 10.0f, redView.frame.origin.y + 10.0f, 180, 320)]; 
[greenView setBackgroundColor:[UIColor greenColor]]; 
[redView addSubview:greenView]; 
[controller.view addSubview:redView]; 
window.rootViewController = controller; 
[window makeKeyAndVisible]; 
return YES; 

回答

1
  1. 首先分配storyboard id到abcViewController在故事板例如 「的firstView」。
  2. 在app委託中導入viewController
  3. 在故事板中,取消選中第一個視圖控制器中的「初始視圖控制器」屬性。
  4. 在應用程序的info.plist中,刪除「主要故事板文件基本名稱」的值。
  5. 實例化故事板,創建窗口對象並設置初始視圖控制器在應用程序委託的application:didFinishLaunchingWithOptions:方法

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

    { //改寫後的應用程序啓動點定製。

    self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds]; 
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
    
    TestViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"firstView"]; 
    
    UIView *redView = [[UIView alloc] initWithFrame: CGRectMake ([[UIScreen mainScreen] bounds].origin.x+30, [[UIScreen mainScreen] bounds].origin.y+30, 260, 400)]; 
    [redView setBackgroundColor:[UIColor redColor]]; 
    UIView *greenView = [[UIView alloc] initWithFrame: CGRectMake (redView.frame.origin.x + 10.0f, redView.frame.origin.y + 10.0f, 180, 320)]; 
    [greenView setBackgroundColor:[UIColor greenColor]]; 
    [redView addSubview:greenView]; 
    [controller.view addSubview:redView]; 
    
    self.window.rootViewController = controller; 
    [self.window makeKeyAndVisible]; 
    
    
    return YES; 
    

    }

+1

大感謝你.... !!!!!!!!!!!!!!!!! – Saty

相關問題