2013-04-17 24 views
21

Tôi đang sử dụng chế độ xem vùng chứa bảng phân cảnh. Vì vậy, tôi có một bộ điều khiển xem có chứa một cái nhìn container. Tôi có chế độ xem thứ hai, nhỏ hơn trong bảng phân cảnh, được nhúng trong vùng chứa đó thông qua phân đoạn nhúng. Càng xa càng tốt.Chuyển chế độ xem vùng chứa động theo số

Bây giờ tôi muốn có thể tự động chuyển nội dung của chế độ xem vùng chứa đó khi người dùng nhấn vào nút. Nút sẽ ở chế độ xem chính, lớn hơn. Tôi đã thực hiện một bộ điều khiển chế độ xem khác có cùng kích thước nhỏ hơn và đặt cho nó một ID bảng phân cảnh khác.

Tuy nhiên, tôi không thể tìm ra cách mã hóa công tắc. Tôi chỉ có thể tạo một phân đoạn nhúng duy nhất cho vùng chứa đó.

Bất kỳ trợ giúp nào được biết ơn.

Trả lời

39

Bạn cần sử dụng api bộ điều khiển chế độ xem bộ chứa tùy chỉnh để thực hiện chuyển đổi bộ điều khiển. Đoạn mã sau cho thấy một cách để làm điều đó. Trong dự án này, tôi đã có một điều khiển phân đoạn trong bộ điều khiển khung nhìn chính (cái có khung nhìn container) chuyển đổi giữa hai bộ điều khiển. initialVC là bộ điều khiển được nhúng trong IB và thay thếVC là bộ điều khiển tôi đang chuyển sang. Thuộc tính, vùng chứa, là một IBOutlet cho khung nhìn vùng chứa.

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.initialVC = self.childViewControllers.lastObject; 
    self.substituteVC = [self.storyboard instantiateViewControllerWithIdentifier:@"Substitute"]; 
    self.currentVC = self.initialVC; 
} 

-(IBAction)switchControllers:(UISegmentedControl *)sender { 

    switch (sender.selectedSegmentIndex) { 
     case 0: 
      if (self.currentVC == self.substituteVC) { 
       [self addChildViewController:self.initialVC]; 
       self.initialVC.view.frame = self.container.bounds; 
       [self moveToNewController:self.initialVC]; 
      } 
      break; 
     case 1: 
      if (self.currentVC == self.initialVC) { 
       [self addChildViewController:self.substituteVC]; 
       self.substituteVC.view.frame = self.container.bounds; 
       [self moveToNewController:self.substituteVC]; 
      } 
      break; 
     default: 
      break; 
    } 
} 


-(void)moveToNewController:(UIViewController *) newController { 
    [self.currentVC willMoveToParentViewController:nil]; 
    [self transitionFromViewController:self.currentVC toViewController:newController duration:.6 options:UIViewAnimationOptionTransitionFlipFromLeft animations:nil 
          completion:^(BOOL finished) { 
           [self.currentVC removeFromParentViewController]; 
           [newController didMoveToParentViewController:self]; 
           self.currentVC = newController; 
          }]; 
} 

Sau Edit:

Nếu bạn muốn đi với bộ điều khiển mới không có hình ảnh động, bạn có thể làm điều đó như thế này (điều này thay thế cho mã mà tôi đã thuộc trường hợp 1).

[self addChildViewController:self.substituteVC]; 
[self.substituteVC didMoveToParentViewController:self]; 
self.substituteVC.view.frame = self.container.bounds; 
[self.container addSubview:self.substituteVC.view]; 
[self.currentVC removeFromParentViewController]; 
+0

Xin chào, cảm ơn, điều đó thật tuyệt vời. Một điều - để có được kết quả chính xác mà tôi muốn (một trao đổi thẳng - không có hoạt ảnh), tôi đã thay đổi phương thức moveToNewController của bạn, thành duration: 0 và các tùy chọn: UIViewAnimationOptionTransitionNone. Nhưng một số mã, bao gồm cả cấu trúc khối hoàn thành, có vẻ thừa. Có cách nào đơn giản hơn để viết phương thức cuối cùng cho một trao đổi thẳng (không động) không? –

+0

@JamesWhite, tôi đã chỉnh sửa câu trả lời của mình để hiển thị cách thực hiện nó mà không có hoạt ảnh. – rdelmar

+0

Bạn là một nhà vô địch. –

4

rdelmar's answer chuyển đổi sang Swift cú pháp

override func viewDidLoad() { 
     super.viewDidLoad() 

     self.initialVC = self.childViewControllers.last 
     self.substituteVC = self.storyboard!.instantiateViewControllerWithIdentifier("Substitute") 
     self.currentVC = self.initialVC; 

} 
    @IBAction func switchControllers(sender: UISegmentedControl) { 
     switch sender.selectedSegmentIndex{ 
      case 0: 
       if (self.currentVC == self.substituteVC) { 
        self.addChildViewController(self.initialVC) 
        //self.initialVC.view.frame = self.containerView.bounds 
        moveToNewController(self.initialVC) 
       } 
      case 1: 
       if (self.currentVC == self.initialVC) { 
        self.addChildViewController(self.substituteVC) 
        //self.substituteVC.view.frame = self.containerView.bounds 
        moveToNewController(self.substituteVC) 
       } 
      default: 
      break; 
     } 
    } 

    func moveToNewController(newController : UIViewController){ 
     self.currentVC.willMoveToParentViewController(nil) 
     self.transitionFromViewController(
      self.currentVC!, 
      toViewController: newController, 
      duration: 0.2, 
      options: UIViewAnimationOptions.TransitionCrossDissolve, 
      animations: nil, 
      completion: { finished in 
       self.currentVC.removeFromParentViewController() 
       newController.didMoveToParentViewController(self) 
       self.currentVC = newController 
     }) 

    } 
0

Nếu bạn đối phó với quan điểm điều khiển hơn 2 con đó là một ý tưởng tốt để lưu trữ chúng trong mảng. Ngoài ra, bạn sẽ cần áp dụng các ràng buộc mỗi khi bạn thêm một chế độ xem con mới vào chế độ xem vùng chứa. Đây là phiên bản sửa đổi của câu trả lời được chấp nhận (sử dụng cú pháp Swift 2):

import UIKit 

class FooBarViewController: UIViewController 
{ 
    // MARK: - IBOutlets 

    @IBOutlet weak var containerView: UIView! 

    // MARK: - Properties 

    var vcs = [UIViewController]() 
    var currentVC: UIViewController! 

    // MARK: - ViewController Lifecycle 

    override func viewDidLoad() 
    { 
     super.viewDidLoad() 

     vcs.append(childViewControllers.last!) 
     vcs.append(storyboard!.instantiateViewControllerWithIdentifier("FooViewControler"))  
     vcs.append(storyboard!.instantiateViewControllerWithIdentifier("BarViewController")) 
     currentVC = vcs[0] 

     for vc in vcs { 
      vc.view.frame = containerView.bounds 
     } 
    } 

    // MARK: - Methods 

    func switchToViewController(targetVC: UIViewController) 
    { 
     addChildViewController(targetVC) 
     currentVC.willMoveToParentViewController(nil) 

     transitionFromViewController(
      currentVC, 
      toViewController: targetVC, 
      duration: 0.0, // 0.3 
      options: .TransitionNone, // .TransitionCrossDissolve, 
      animations: nil, 
      completion: { finished in 
       self.currentVC.removeFromParentViewController() 
       targetVC.didMoveToParentViewController(self) 
       self.currentVC = targetVC 
       self.applyConstraints() 
     }) 
    } 

    func applyConstraints() 
    { 
     let viewsDict = ["newSubview": currentVC.view] 

     currentVC.view.translatesAutoresizingMaskIntoConstraints = false 

     containerView.addConstraints(
      NSLayoutConstraint.constraintsWithVisualFormat("H:|[newSubview]|", 
       options: NSLayoutFormatOptions(rawValue: 0), 
       metrics: nil, 
       views: viewsDict)) 

     containerView.addConstraints(
      NSLayoutConstraint.constraintsWithVisualFormat("V:|[newSubview]|", 
       options: NSLayoutFormatOptions(rawValue: 0), 
       metrics: nil, views: 
       viewsDict)) 
    } 

    // MARK: - IBActions 

    @IBAction func switchControllers(sender: UISegmentedControl) 
    { 
     let i = sender.selectedSegmentIndex 

     if currentVC != vcs[i] { 
      self.addChildViewController(vcs[i]) 
      switchToViewController(vcs[i]) 
     } 
    } 
} 
+0

Bạn đang thêm bộ điều khiển chế độ xem vào "containerView" ở đâu? – Satyam

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