2015-04-14 47 views
0

我是Swift的新手。我已經用Swift開始了一個新項目。我正在嘗試將SecondViewcontroller視圖作爲子視圖添加到FirstViewController。我在看的是爲FirstViewController聲明SecondViewController屬性。任何人都可以請建議下面的代碼的雨燕版本用Swift聲明另一個ViewController的UIViewController變量屬性

FirstViewController.h

@interface FirstViewController : UIViewController { 
    IBOutlet UIView *listContainerView; 
} 

@property (nonatomic, strong) SecondViewController *secondVC; 

@end 

FirstViewController.m

#import "FirstViewController.h" 

@interface FirstViewController() 

@end 

@implementation FirstViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 

    self.secondVC = (SecondViewController *)[mainStoryBoard instantiateViewControllerWithIdentifier:@"SecondViewController"]; 

    if (![self.secondVC.view isDescendantOfView:self.view]) { 
     [self addChildViewController:self.secondVC]; 
     self.secondVC.view.frame = CGRectMake(0, 0, listContainerView.frame.size.width, listContainerView.frame.size.height); 
     [listContainerView addSubview:self.secondVC.view]; 
     [self.secondVC didMoveToParentViewController:self]; 
    } 
} 

@end 

回答

1

你應該做的是讓一個子視圖,而不是製作新的UIViewController。只有在非常特殊的情況下,你會把一個UIViewController放在另一個裏面。基本上每個'屏幕'應該有一個UIViewController,但你不應該把一個放在另一個。

+0

沒必要,那裏有很多使用childViewControllers的架構(只保留在Apple框架中,導航控制器也是這樣)。 我認爲他只是要求從他的objective-c代碼轉換到swift版本。 –

+0

@MarcoPace但作爲初學者,嵌套視圖控制器很少被使用,他試圖完成的工作可能會延續到另一個視圖。不要試圖假設,但通過兒童風險投資家在不同的應用程序模型上互相提供視圖並不一定有利於OP。 – Schemetrical

+0

哦,從這個角度來看可能你是對的:) –

相關問題