2013-07-25 30 views
26

Tôi có một số UIViewController được gọi là RootViewController và A UIViewController được gọi là NewsViewController. Tôi muốn hiển thị NewsViewController bên trong một UIView (UIView chỉ là một phần của màn hình) mà tôi đã tạo bên trong RootViewController. Tôi đang sử dụng kịch bản và ứng dụng của tôi cần hỗ trợ iOS 5 (vì vậy tôi không thể sử dụng segues nhúng aka Container từ IB) mãTải UIViewController bên trong Chế độ xem vùng chứa bằng StoryBoard

RootViewController:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NewsViewController *news = [[NewsViewController alloc]init]; 
    news.view.frame = self.newsSection.bounds; 
    [self.newsSection addSubview:news.view]; 
    [self addChildViewController:news]; 
    // Do any additional setup after loading the view. 
} 

Tôi cũng kết nối cả UIViewControllers với một segue. Bản tin UIView sẽ vẫn trống. Tôi đang làm gì sai?

Edit:

này làm việc cho tôi, đó là cách tiếp cận đúng không?

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
    NewsViewController *news = [storyboard instantiateViewControllerWithIdentifier:@"NewsViewControllerID"]; 
    news.view.frame = self.newsSection.bounds; 
    [self.newsSection addSubview:news.view]; 
    [self addChildViewController:news]; 
    [news didMoveToParentViewController:self]; 
} 
+0

bạn đã cố gắng của tôi câu trả lời? – stosha

+0

Có. Nó không hoạt động. – Segev

Trả lời

41
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
    NewsViewController *news = [storyboard instantiateViewControllerWithIdentifier:@"NewsViewControllerID"]; 
    news.view.frame = self.newsSection.bounds; 
    [self.newsSection addSubview:news.view]; 
    [self addChildViewController:news]; 
    [news didMoveToParentViewController:self]; 
} 

Swift

override func viewDidLoad() { 
    super.viewDidLoad() 
    let news = self.storyboard?.instantiateViewControllerWithIdentifier("NewsViewControllerID") as! NewsViewController 
    news.view.frame = newsSection.bounds 
    newsSection.addSubview(news.view) 
    addChildViewController(news) 
    news.didMoveToParentViewController(self) 
} 

Swift> = 3:

override func viewDidLoad() { 
    super.viewDidLoad() 
    let news = self.storyboard?.instantiateViewController(withIdentifier: "NewsViewControllerID") as! NewsViewController 
    news.view.frame = newsSection.bounds 
    newsSection.addSubview(news.view) 
    addChildViewController(news) 
    news.didMove(toParentViewController: self) 
} 
+0

Câu trả lời hay và lưu ý ... http://stackoverflow.com/a/13867671/294884 – Fattie

+1

Sử dụng self.storyboard trong bộ điều khiển chế độ xem thay vì tải lại. –

+0

Tôi đã đưa ra một đi không thực sự mong đợi nó để làm chính xác như tôi muốn ... nhưng nó đã làm! Tôi đã có một vòng lặp vô hạn lúc đầu tiên bởi vì tôi đã tải trình điều khiển xem trong chính lớp đó. Khi tôi đã chuyển mã sang 'initWithNibName', mọi thứ hoạt động tốt. Điều đó đã sửa nó vì '[instantboard instantiate ..]' gọi 'initWithCoder', do đó tránh vòng lặp vô hạn đệ quy. – Matthew

10

Tạo đứa trẻ, khi tạo gốc VC:

-(void)awakeFromNib 
{ 
    [super awakeFromNib]; 
    // Create news vc from storyboard 
    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"YourStoryboard" bundle:nil]; 
    NewsViewController *news = [storyboard instantiateViewControllerWithIdentifier:@"News VC ID, you should set it in IB"]; 
    // Add it as a child to root 
    [self addChildViewController:news]; 
    [news didMoveToParentViewController:self]; 
    // Save it for future use 
    self.news = news; 
} 

Thêm quan điểm của trẻ khi xem gốc được tạo ra:

-(void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.news.view.frame = self.newsSection.bounds; 
    [self.newsSection addSubview:self.news.view]; 
} 
+0

Giải pháp của bạn đã hoạt động. Cảm ơn. Tôi tự hỏi, tại sao tôi nên sử dụng awakeFromNib thay vì câu hỏi đã chỉnh sửa của mình (tôi đã thêm giải pháp tạm thời ở đó) – Segev

+0

Trên iOS <6 view có thể được tải/dỡ nhiều lần, vì vậy mỗi lần xemDidLoad được gọi là instance của NewsViewController sẽ được tạo và được thêm vào như một đứa trẻ. Tất nhiên bạn có thể xử lý viewDidUnload và loại bỏ nó ở đó, nhưng việc tạo con khi tạo bố mẹ tốt hơn theo ý kiến ​​của tôi. –

1

Hãy thử điều này:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" 
                bundle:nil]; 
NewsViewController *rootViewController = 
[storyboard instantiateViewControllerWithIdentifier:@"NewsViewControllerID"]; 
+0

Sẽ không hiển thị ViewController trên toàn bộ màn hình? Tôi muốn hiển thị nó chỉ trên một phần của màn hình điều khiển xem gốc – Segev

+1

Tại sao bạn không chỉ cần tạo một lớp UIView thay vì UIViewcontroller? Nó có thể jus được thêm vào với phương thức 'addsubview:' –

0

Bạn nên di chuyển mã của bạn đến - (void) viewDidApp tai: (BOOL) hoạt hình như thế này

- (void) viewDidAppear: (BOOL) animated 
{ 
    [super viewDidAppear: animated]; 

    NewsViewController *news = [[NewsViewController alloc]init]; 
    news.view.frame = self.newsSection.bounds; 
    [self.newsSection addSubview:news.view]; 
    [self addChildViewController:news]; 
    // Do any additional setup after loading the view. 
} 

vì khung vọt trong viewDidLoad là {0,0} {0,0}

+0

Không làm việc cho tôi. – Segev

+0

bạn có thể chèn NSLog (@ "% @", NSStringFromCGRect (self.newsSection.bounds)) hay không; sau [super viewDidAppear: ...? – stosha

+0

2013-07-25 12: 54: 04.470 containersTests [6683: 11303] {{0, 0}, {320, 146}} – Segev

1

Đây là cách tiếp cận theo cách nó làm việc cho tôi.

Tôi có 3 nút ở đầu Mua, Bán, Cho thuê. bây giờ tại khai thác tại bất kỳ nút cụ thể tôi đang tải tương ứng UIViewController và tôi đang sử dụng bảng phân cảnh.

Tôi đã chỉ định Storyboard Id cho mỗi UIViewController.

Tôi đã thêm hai UIView vào số Main ViewController có 3 nút này (Mua, Bán, Cho thuê).

một chế độ xem tôi đang sử dụng cho ba nút ở trên và khác tôi đang sử dụng để tải UIViewController.

Bây giờ, hãy nhấn vào nút cụ thể mà tôi đang sử dụng và nhấn vào một nút cụ thể mà tôi đang thực hiện theo mã sau.

- (IBAction)sellingClicked:(id)sender 
{   
    UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"Main" bundle:nil]; 

    TestViewController *tvc=[storyboard instantiateViewControllerWithIdentifier:@"test"]; 

    [self.viewContainer addSubview:tvc.view]; 
    [self addChildViewController:tvc];  
} 

và nó hoạt động hoàn hảo cho tôi hơn nữa, bạn có thể thêm cuộn Xem i bạn muốn cuộn ở đó.

+1

Sử dụng 'self.storyboard' thay vì tải toàn bộ bảng phân cảnh. Điều đó sẽ hút rất nhiều bộ nhớ và CPU. – Andy

1
 otherController!.view.frame = container.bounds; 
     self.addChildViewController(otherController!); 
     container.addSubview(otherController!.view); 
     container.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[view]|", options: .allZeros, metrics: nil, views: ["view": otherController!.view])) 
     container.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[view]|", options: .allZeros, metrics: nil, views: ["view": otherController!.view])) 
     otherController!.didMoveToParentViewController(self); 

Đừng quên để xác định hạn chế đặc biệt là nếu bạn muốn thực hiện hình ảnh động

Các vấn đề liên quan