2015-04-14 46 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

哦,从这个角度来看可能你是对的:) –

相关问题