2012-09-07 23 views
2

我有一個subview它的allmost一半我MAINVIEW也是我有我的MAINVIEW一個UISlider。我silder從0到10,我想一些UIButton s添加到我的SubviewUISilder。現在如果我的silder獲得價值2的話,我想2個UIButton s添加到我的子視圖,就好像我的silder傳遞值的值2並獲得值4,然後我想從Subview中刪除先前的UIButton並將一些新的UIButton s添加到子視圖。如何使用UISilder在SubView中添加和刪除UIButtons?

回答

0

注:該代碼是未經測試。

  1. 啓動一個10 UIButton,並將它們添加到視圖,它們設置爲隱藏的:

    for (int x = 0; x < 10; x++) { 
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(50, x * 100, 100, 50)]; 
        [btn setText:[NSString stringWithFormat:@"Button %d",(x + 1)]]; 
        [btn setHidden:true]; 
        [self.view addObject:btn]; 
        [btn release]; 
    } 
    
  2. 使用滑塊變化值的方法來隱藏/顯示這樣的按鈕:

    -(IBAction) sliderChanged:(id) sender{ 
          UISlider *slider = (UISlider *) sender; 
          for (int x = 0; x < slider.value; x++) { 
           UIButton *btn = (UIButton *)[savedBtn objectAtIndex:x]; 
           [btn setHidden:false]; 
          } 
        } 
    

更新

要使用UIScrollView,您必須將UIButton s添加到滾動視圖,而不是觀點的,像這樣的:

UIScrollView *sView = [[UIScrollView alloc] initWithFrame:CGRectMake(10,10,200,200)]; 
    [sView setDelegate:self]; 
    for (int x = 0; x < 10; x++) { 
     UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(50, x * 100, 100, 50)]; 
     [btn setText:[NSString stringWithFormat:@"Button %d",(x + 1)]]; 
     [btn setHidden:true]; 
     [sView addSubview:btn]; 
     [btn release]; 
    } 

然後添加滾動視圖到視圖:

 [self.view addSubview:sView]; 

希望這可以幫助你。

+0

感謝名單@Scar你的答案解決我的基本problem.Can請你幫我更多的視圖滾動,因爲我的子視圖已高度100,所以爲什麼它顯示我只有三個按鍵和我的第四個按鈕和第五個按鈕有y.xis 130,150所以PLZ建議我如何解決這個問題。感謝 – NSCool

+0

請看我更新的答案。 – Scar

+0

thanx @Scar很多我的問題是解決NOw。 – NSCool

0

首先你添加一個選擇方法您UISlider

[customSlider addTarget:selfaction:@selector(sliderEnd:)forControlEvents:UIControlEventTouchUpInside]; 

現在選擇方法應該是在Mainview類項目的進行定義。

現在,在選擇方法你確定在函數滑塊值,並根據其值可能會添加或刪除或添加按鈕到其他視圖。您可以在將參考添加到視圖時將參考存儲在所有按鈕中,然後在slider值建議這樣做時將其從superview中刪除。

希望它有幫助。

+0

thanx @ c4code幫助我。 – NSCool