2014-07-23 32 views
2

我在iOS中看起來像是典型的Container View問題。我有一個ViewController有兩個子視圖:一個UISegmentedControl和一個容器視圖。現在將我的Container View放置在故事板中,我不知道如何繼續。當然,我認爲我的下一步是將UIContainerView子類化,以完成我在iOS文檔中閱讀的所有內容。但是沒有UIContainerView這樣的類。所以現在,除了我能夠放置在故事板上之外,我被卡住了。希望有人可以幫助我,我會假設看起來像一個簡單的情況。如何設置容器視圖以便交換兩個孩子

試想:

  • 一個視圖控制器有兩個按鈕(貓,狗)和ContainerView。
  • 當用戶點擊catButton時,ContainerView應顯示CatViewController(並且對dogButton執行類似操作)
  • 已經有故事板設置的圖像。
  • 爲簡單起見,讓CatViewController包含一個帶有單詞CAT的單個UILabel(以及類似的DogViewController)。
  • 此外,在故事板中,我已經創建了CatViewController和DogViewController作爲兩個獨立的,無法訪問的視圖控制器。

那麼在這一點上,我該如何繼續?由於我不能像UIContainerView那樣繼承這樣的類,我該怎麼做?

我相信這個場景很簡單,可以讓某人提供一個例子,但是如果您認爲它太複雜了,請提供一個更簡單的例子。我只想看看如何做一個簡單的。

P.S.我已經在這裏採取了參觀在計算器上,如:

Swapping child views in a container view

,我已經在https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html#//apple_ref/doc/uid/TP40007457-CH18-SW6

+0

參考https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006926-CH3-SW86,我假設您的第一個ViewController應該使用addChildViewController添加Cat-或DogViewController:然後將它們的rootView添加到containerView –

回答

1

enter image description here

閱讀文檔,我認爲是更好的使用UISegmentedControl而不是兩個UIButtons 。 容器視圖子視圖(_vwContainer.subviews)最初包含自動實例化的CatViewController視圖。

// ViewController.m 
#import "ViewController.h" 

@interface ViewController() 
@property (weak, nonatomic) IBOutlet UIView *vwContainer; 
@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    _vwContainer.clipsToBounds = YES; 
} 

- (IBAction)onSegmentValueChanged:(UISegmentedControl *)sender { 
    NSLog(@"Value changed to: %zd",sender.selectedSegmentIndex); 
    NSLog(@"BEFORE: self.childViewControllers: %@",self.childViewControllers); 
    NSLog(@"BEFORE: _vwContainer.subviews: %@",_vwContainer.subviews); 

    // set oldVC & newVC 
    UIViewController *oldVC = self.childViewControllers.firstObject; 
    NSString *strIdNewVC; 
    switch (sender.selectedSegmentIndex) { 
     case 0: strIdNewVC = @"catVC"; break; 
     default: strIdNewVC = @"dogVC"; 
    } 
    UIViewController *newVC = [self.storyboard instantiateViewControllerWithIdentifier:strIdNewVC]; 

    // 
    [oldVC willMoveToParentViewController:nil]; 
    [self addChildViewController:newVC]; 

    // Prepare animation transition, for example left to right 
    newVC.view.frame = oldVC.view.frame; 
    CGPoint pntEnd = oldVC.view.center; 
    CGPoint pntInit = pntEnd; 
    pntInit.x += oldVC.view.frame.size.width; 
    newVC.view.center = pntInit; 

    [self transitionFromViewController:oldVC toViewController:newVC 
           duration:0.25 options:0 
          animations:^{ 

           newVC.view.center = pntEnd; 

           } completion:^(BOOL finished) { 
            [oldVC removeFromParentViewController]; 
            [newVC didMoveToParentViewController:self]; 
            NSLog(@"AFTER: self.childViewControllers: %@",self.childViewControllers); 
            NSLog(@"AFTER: _vwContainer.subviews: %@",_vwContainer.subviews); 
           }]; 
} 

@end 
相關問題