2013-07-31 84 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]; 
}