【问题标题】:Using UISegmentedControl to show/hide buttons使用 UISegmentedControl 显示/隐藏按钮
【发布时间】:2013-08-01 01:18:44
【问题描述】:
如何在 Objective C 编程中使用 UISegmentedControl 来显示或隐藏屏幕上的一些按钮?
这个网站上的另一个问题显示了这个代码:
if (selectedSegment == 0) {
[firstView setHidden:NO];
[secondView setHidden:YES];
} else {
[firstView setHidden:YES];
[secondView setHidden:NO];
}
但是我究竟如何将某些内容放入 firstView 和 secondView 中?
如果有人给我一个例子,请添加一个 UIButton 作为例子。
注意:我不能使用基于视图的应用程序来执行此操作,因为我的程序已经很远了。
提前致谢。
【问题讨论】:
标签:
ios
uibutton
views
uisegmentedcontrol
【解决方案1】:
在视图控制器中的@implementation 行之后:
UIButton *firstButton;
UIButton *secondButton;
在您的视图控制器中,在 viewDidLoad 函数中(或任何您想要初始化按钮的地方),像这样初始化您的按钮:
firstButton = [UIButton buttonWithType:(UIButtonTypeRoundedRect)];
[firstButton setFrame:CGRectMake(20, 100, 50, 50)];
secondButton = [UIButton buttonWithType:(UIButtonTypeRoundedRect)];
[secondButton setFrame:CGRectMake(20, 150, 50, 50)];
显然,将样式更改为您选择的样式并使用 CGRectMake 将按钮放置在屏幕上的某个位置。然后当你想隐藏/显示一个按钮时:
if (selectedSegment == 0) {
[firstButton setHidden:NO];
[secondButton setHidden:YES];
} else {
[firstButton setHidden:YES];
[secondButton setHidden:NO];
}