【问题标题】:Switch the sub viewController use segmented control切换子viewController使用分段控制
【发布时间】:2017-04-20 07:02:45
【问题描述】:

我想从段控制切换子视图控制器。

storyboard中有vc1和vc2,主vc的导航控制器栏有段控制。

我想在主vc上添加vc1和vc2,如何切换子vc使用段控制器?

如何处理?

【问题讨论】:

标签: ios uiviewcontroller storyboard


【解决方案1】:

按照以下步骤操作。

  1. 将 VC1 和 VC2 添加为 mainVC 的 childVC。

  2. 在第 1 段选择 VC1.view.hidden = false & vc2.view.hidden = true

  3. 在第 2 段选择 VC2.view.hidden = false & vc1.view.hidden = true

参考

  1. How-to-add-childVC

  2. How-tobind-segment-control-action

代码工作

@IBAction func indexChanged(_ sender: AnyObject) {
    switch segmentedControl.selectedSegmentIndex
    {
    case 0:
        vc1.view.hidden = false
        vc2.view.hidden = true
    case 1:
        vc2.view.hidden = false
        vc1.view.hidden = true
    default:
        break
    }
}

【讨论】:

  • 你能详细解释一下吗?我的朋友。
【解决方案2】:

公认的答案显然是正确的,但老实说,我更喜欢为 UISegmentedControl 中的每个选项卡使用容器视图。这样与每个视图相关的逻辑在不同的视图控制器中分离。您可以通过这种方式实现:

class TopViewController: UIViewController {

    @IBOutlet weak var firstContainerView: UIView!
    @IBOutlet weak var secondContainerView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        firstContainerView.alpha = 1.0
        secondContainerView.alpha = 0.0
    }

    @IBAction func didChangeIndex(_ sender: UISegmentedControl) {
        switch sender.selectedSegmentIndex {
        case 0:
            firstContainerView.alpha = 1.0
            secondContainerView.alpha = 0.0
        case 1:
            firstContainerView.alpha = 0.0
            secondContainerView.alpha = 1.0
        default:
            break
        }
    }
}

如果你想访问FirstViewController或SecondViewController的属性,你可以实现prepare(for segue: UIStoryboardSegue, sender: Any?)方法。

【讨论】:

    【解决方案3】:

    您应该将 vc 和 vc.view 添加到 Main ViewController:

    当您选择分段控件时,您可以隐藏子视图控制器的视图,如下所示:

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        _vc1 = [sb instantiateViewControllerWithIdentifier:@"ViewController1"];
        _vc2 = [sb instantiateViewControllerWithIdentifier:@"ViewController2"];
    
        [self addChildViewController:_vc1];
        [self addChildViewController:_vc2];
        [self.view addSubview:_vc1.view];
        [self.view addSubview:_vc2.view];
    
    }
    
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    - (IBAction)segAction:(UISegmentedControl *)sender {
    
        if (1 == sender.selectedSegmentIndex) {
            NSLog(@"1");
    
            _vc1.view.hidden = YES;
            _vc2.view.hidden = NO;
        }else {
            NSLog(@"%ld", sender.selectedSegmentIndex);
            _vc2.view.hidden = YES;
            _vc1.view.hidden = NO;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-26
      • 1970-01-01
      相关资源
      最近更新 更多