2013-07-31 83 views
0

如何在Objective C编程中使用UISegmentedControl来显示或隐藏屏幕上的某些按钮?使用UISegmentedControl显示/隐藏按钮

这个网站的其他问题表明此代码:

if (selectedSegment == 0) { 
    [firstView setHidden:NO]; 
    [secondView setHidden:YES]; 
} else { 
    [firstView setHidden:YES]; 
    [secondView setHidden:NO]; 
} 

但究竟如何我把东西进入的firstView和secondView? 如果有人给我示例,请添加一个UIButton作为示例。 注意:由于我的程序很长,我不能使用基于视图的应用程序来执行此操作。 在此先感谢。

回答

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