如何使用UISegmentedControl切换视图? [英] How do I use a UISegmentedControl to switch views?

查看:109
本文介绍了如何使用UISegmentedControl切换视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to figure out how to use the different states of a UISegmentedControl to switch views, similar to how Apple does it in the App Store when switiching between 'Top Paid' and 'Top Free'.

推荐答案

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



The simplest approach is to have two views that you can toggle their visibility to indicate which view has been selected. Here is some sample code on how it can be done, definitely not an optimized way to handle the views but just to demonstrate how you can use the UISegmentControl to toggle the visible view:

- (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];
  }
}



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


You can of course further re-factor the code to hide/show the right view.

这篇关于如何使用UISegmentedControl切换视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆