繁体   English   中英

如何使用UISegmentedControl切换视图?

[英]How do I use a UISegmentedControl to switch views?

我正试图弄清楚如何使用UISegmentedControl的不同状态来切换视图,类似于Apple在“顶级付费”和“顶级免费”之间切换时在App Store中的操作方式。

最简单的方法是使用两个视图来切换其可见性以指示已选择哪个视图。 下面是一些关于如何完成它的示例代码,绝对不是处理视图的优化方法,而只是为了演示如何使用UISegmentControl来切换可见视图:

- (IBAction)segmentSwitch:(id)sender {
  UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
  NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;

  if (selectedSegment == 0) {
    //toggle the correct view to be visible
    [firstView setHidden:NO];
    [secondView setHidden:YES];
  }
  else{
    //toggle the correct view to be visible
    [firstView setHidden:YES];
    [secondView setHidden:NO];
  }
}


您当然可以进一步重新调整代码以隐藏/显示正确的视图。

在我的情况下,我的观点非常复杂,我不能只改变不同视图的隐藏属性,因为它会占用太多内存。

我已经尝试了几种解决方案,并且它们不适用于我,或者执行不正常,特别是在推送/弹出视图时,navBar的titleView并不总是显示segmentedControl。

我发现这篇关于该问题的博客文章解释了如何以正确的方式做到这一点。 似乎他在2010年WWDC上得到了苹果工程师的帮助,想出了这个解决方案。

http://redartisan.com/2010/6/27/uisegmented-control-view-switching-revisited

此链接中的解决方案是我迄今为止发现的有关此问题的最佳解决方案。 通过一点调整,它也可以在底部的tabBar中正常工作

或者,如果是表,则可以重新加载表,并在cellForRowAtIndex中,根据所选的段选项从不同的数据源填充表。

一个想法是让具有分段控件的视图具有容器视图,您可以使用不同的子视图填充(在切换段时添加为容器视图的唯一子视图)。 你甚至可以为这些子视图设置单独的视图控制器,但如果你需要它们,你必须转发重要的方法,如“viewWillAppear”和“viewWillDisappear”(并且必须告诉他们它们所属的导航控制器)。

通常情况下效果很好,因为您可以在IB中布置带容器的主视图,子视图将填充容器允许的任何空间(确保正确设置自动调整大小的掩码)。

试试这段代码,这将帮助您在更改Segment COntrol片段的不同视图之间切换

在选择UISegmentControl的不同段时打开不同的视图

尝试使用SNFSegmentedViewController ,这是一个开源组件,可以使用像UITabBarController这样的设置来完成您正在寻找的内容。

分配.H in

 UISegmentedControl *lblSegChange;

- (IBAction)segValChange:(UISegmentedControl *) sender

声明.M

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

 if(sender.selectedSegmentIndex==0)
 {
  viewcontroller1 *View=[[viewcontroller alloc]init];
  [self.navigationController pushViewController:view animated:YES];
 }
 else 
 {
  viewcontroller2 *View2=[[viewcontroller2 alloc]init];
  [self.navigationController pushViewController:view2 animated:YES];
 }
} 

从@Ronnie Liew的回答中,我创建了这个:

//
//  ViewController.m
//  ResearchSegmentedView
//
//  Created by Ta Quoc Viet on 5/1/14.
//  Copyright (c) 2014 Ta Quoc Viet. All rights reserved.
//
#define SIZE_OF_SEGMENT 56
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize theSegmentControl;
UIView *firstView;
UIView *secondView;
CGRect leftRect;
CGRect centerRect;
CGRect rightRect;
- (void)viewDidLoad
{
    [super viewDidLoad];
    leftRect = CGRectMake(-self.view.frame.size.width, SIZE_OF_SEGMENT, self.view.frame.size.width, self.view.frame.size.height-SIZE_OF_SEGMENT);
    centerRect = CGRectMake(0, SIZE_OF_SEGMENT, self.view.frame.size.width, self.view.frame.size.height-SIZE_OF_SEGMENT);
    rightRect = CGRectMake(self.view.frame.size.width, SIZE_OF_SEGMENT, self.view.frame.size.width, self.view.frame.size.height-SIZE_OF_SEGMENT);

    firstView = [[UIView alloc] initWithFrame:centerRect];
    [firstView setBackgroundColor:[UIColor orangeColor]];
    secondView = [[UIView alloc] initWithFrame:rightRect];
    [secondView setBackgroundColor:[UIColor greenColor]];
    [self.view addSubview:firstView];
    [self.view addSubview:secondView];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)segmentSwitch:(UISegmentedControl*)sender {
    NSInteger selectedSegment = sender.selectedSegmentIndex;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.2];
    if (selectedSegment == 0) {
        //toggle the correct view to be visible
        firstView.frame = centerRect;
        secondView.frame = rightRect;
    }
    else{
        //toggle the correct view to be visible
        firstView.frame = leftRect;
        secondView.frame = centerRect;
    }
    [UIView commitAnimations];
}
@end

Swift版本:

父视图控制器负责设置每个子视图控制器的视图的大小和位置。 子视图控制器的视图成为父视图控制器的视图层次结构的一部分。

定义惰性属性:

private lazy var summaryViewController: SummaryViewController = {
   // Load Storyboard
   let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)

   // Instantiate View Controller
   var viewController = storyboard.instantiateViewController(withIdentifier: "SummaryViewController") as! SummaryViewController

   // Add View Controller as Child View Controller
   self.add(asChildViewController: viewController)

   return viewController
}()

private lazy var sessionsViewController: SessionsViewController = {
    // Load Storyboard
    let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)

    // Instantiate View Controller
    var viewController = storyboard.instantiateViewController(withIdentifier: "SessionsViewController") as! SessionsViewController

    // Add View Controller as Child View Controller
    self.add(asChildViewController: viewController)

    return viewController
}()

显示/隐藏子视图控制器:

private func add(asChildViewController viewController: UIViewController) {
    // Add Child View Controller
    addChildViewController(viewController)

    // Add Child View as Subview
    view.addSubview(viewController.view)

    // Configure Child View
    viewController.view.frame = view.bounds
    viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    // Notify Child View Controller
    viewController.didMove(toParentViewController: self)
}

private func remove(asChildViewController viewController: UIViewController) {
    // Notify Child View Controller
    viewController.willMove(toParentViewController: nil)

    // Remove Child View From Superview
    viewController.view.removeFromSuperview()

    // Notify Child View Controller
    viewController.removeFromParentViewController()
}

管理SegmentedControl tapEvent

private func updateView() {
    if segmentedControl.selectedSegmentIndex == 0 {
        remove(asChildViewController: sessionsViewController)
        add(asChildViewController: summaryViewController)
    } else {
        remove(asChildViewController: summaryViewController)
        add(asChildViewController: sessionsViewController)
    }
}

当然,您可以在子视图控制器类中使用:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    print("Summary View Controller Will Appear")
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    print("Summary View Controller Will Disappear")
}

参考: https//cocoacasts.com/managing-view-controllers-with-container-view-controllers/

一个快速的Swift版本:

@IBAction func segmentControlValueChanged(_ sender: UISegmentedControl) {

    if segmentControl.selectedSegmentIndex == 0 {

        // do something
    } else {

        // do something else
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM