- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
从标题中可以清楚地看出,我正在尝试找到一种使用分段控件在 View 之间切换的方法。实际上,我已经得到了一些东西并且它正在工作。您可以在下面找到它:
- (IBAction)segmentChanged:(id)sender{
UISegmentedControl *segmentedControl = sender;
switch (segmentedControl.selectedSegmentIndex) {
case 0:
self.currencyContainer.hidden = NO;
self.goldContainer.hidden = YES;
self.alarmContainer.hidden = YES;
break;
case 1:
self.currencyContainer.hidden = YES;
self.goldContainer.hidden = NO;
self.alarmContainer.hidden = YES;
break;
case 2:
self.currencyContainer.hidden = YES;
self.goldContainer.hidden = YES;
self.alarmContainer.hidden = NO;
break;
default:
break;
}
}
因此,这取决于原理变得隐藏和可见。我认为它很常用,因为一般人都有这样的解决方案,正如我从互联网上搜索的那样。但是,我希望它略有不同。使用我的代码,所有的 View 都是一次加载的,但我想在只有 View 出现在屏幕上时加载。如果我从 0 切换到 1 索引段,那么我想加载第一个 View 并显示在屏幕上。
我该如何管理它?
谢谢。
最佳答案
我在这里回答了一个类似(不一样,但结果相似)的问题:How To Switch Views With NSNotifications
基本上,您可以使用 NSNotifications 切换 View 。您的第一个 View 将加载并使用 NSNotifications,您可以将通知发送到自定义类,该类通过更改 View 进行监听和响应;当您在 UISegmentControl 上选择一个索引时,您的 View 将会改变。
在您的代码中,在您的 NSObject 类 ButtonHandler
中,它看起来像这样:
- (IBAction)segmentChanged:(id)sender{
UISegmentedControl *segmentedControl = sender;
switch (segmentedControl.selectedSegmentIndex) {
case 0:
[[NSNotificationCenter defaultCenter] postNotificationName:@"notifyButtonPressed1" object:self];
break;
case 1:
[[NSNotificationCenter defaultCenter] postNotificationName:@"notifyButtonPressed2" object:self];
break;
case 2:
[[NSNotificationCenter defaultCenter] postNotificationName:@"notifyButtonPressed3" object:self];
break;
default:
break;
}
}
然后只需按照我在另一篇文章中列出的其余代码即可。
效果是这样的:
更新
我已经在这个链接上创建了一个关于如何做到这一点的教程:iOS Custom Navigation with UISegmentedControl
要将 View 设置为与绑定(bind)的设备相同,请使用以下命令:
//this will get the size of your device view
//and set it to width and height aka w & h
int w = self.view.frame.size.width;
int h = self.view.frame.size.height;
//THIS SETS THE SIZE AND POSITION OF THE NEW CONTENT
self.content.view.frame = CGRectMake(0, 0, w, h);
关于ios - 使用 UISegmentedControl 在 View 之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31245281/
根据以下文档 ( https://developer.apple.com/documentation/uikit/uisegmentedcontrol/1618570-settitletextattr
我试图在登录时隐藏 nameTextField 并使其在注册时显示切换。我一直试图自己解决这个问题,但这就是我所得到的。我已经把它归结为两个错误,它们是相同的。 Issue Navigator laz
我有两个 UISegmentedControl,我想用其中一个来启用/禁用另一个。我编写了一个函数来处理这个问题,如下所示: - (void)disableSegment2 { if (
我有一个以编程方式定义的 UISegmentedControl。 我正在尝试添加布局约束,以便当我的 iPad 旋转时,分段控件在旋转 View 中正确调整大小,而不是溢出屏幕。 我应用了以下约束:
如何通过再次按下同一段来取消选择 UISegmented 控件中的给定段? 例如按下段 0,它将被选中并保持突出显示。再次按下段 0,它将变为未选中且未突出显示。 该控件仅触发 UIControlEv
我正在为我的分段控件设置颜色,如下所示: segmentedControl.backgroundColor = .gray segmentedControl.selectedSegmentTintCo
我在 iOS7 中更改分段控件的边框颜色时遇到问题。我在 stackoverflow 的其他地方找到了以下建议: [[UISegmentedControl appearance] setTit
是否可以让 UISegmentedControl 在 iPhone 上跨越两行?我已经在一些应用程序中看到了这一点,但没有在文档中找到我需要的内容。也许它是一个自定义的 UIButton,设计得像 U
我的应用程序中有一个 UISegmentedControl(请参阅下面的代码): // --------------- SETTING NAVIGATION BAR RIGHT BUTTONS NSA
我的 iPhone 应用程序中有一个分段控件,在 ios6 上运行良好,但在 ios7 上,分段图 block 被截断(有足够的空间容纳文本,但无论如何都会截断它们) self.segment
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: How to change font color of UISegmentedControl 是否可以为 selec
有没有办法通过编程使UIsegmentedControl具有方角?请帮忙!!! 最佳答案 您可以为将完全覆盖边框的片段设置背景图像。如果背景图像是方形的,那么分段控件将显示为方形。对您想要自定义的每个
如何制作多行 UISegmentedControl。我需要它有 6 个按钮,每行 3 个。我如何以编程方式执行此操作? 最佳答案 您将需要使用其中两个,并使用 selectedSegmentIndex
我正在使用带有一些自定义图像的 UISegmentedControl: UISegmentedControl *segmentedControl = [[UISegmentedControl allo
有没有办法摆脱 UISegmentedControl 的圆角或者它是默认行为? 最佳答案 有一些非常简单的方法可以让您摆脱 UISegmentedControl 上的圆形垃圾...将样式更改为“7”。
就像标签栏一样,我想在 UISegmentedControl 上显示徽章。因为我看不到 UISegmentedControl 的任何预定义方法,就像可用于 UITabBar 的方法一样。 我考虑过将徽
如何获得一个类似于邮件应用程序中的 UISegmentedControl,以便它与 UIToolbar 按钮颜色相同(就好像两个段都处于选定状态一样)。 我想使用分段控件来实现与 Mail 完全相同的
这可能是一个非常简单的问题,但我似乎无法在 API 或任何搜索引擎中找到任何内容。 我有一个分段控件,我已将其设置为瞬时,因为用户将选择他们想要搜索的几个汽车品牌。我遇到的问题是我似乎无法弄清楚如何识
当我将整个导航栏设置为黑色时,通常在所选按钮上的 UISegmentedControl 上显示的色调不会显示(self.navigationController.navigationBar.tintC
我想为 UISegmentedControl 提供以下方面: 注意灰色背景 View ,以及分段控件未选定项目的白色背景。 但是,如果我为 UISegmentedControl 提供白色背景,我会得到
我是一名优秀的程序员,十分优秀!