2011-02-01 30 views
3

你好,我已經嘗試了3個星期來解決這個問題,它讓我難過。我想要做的是從數組中創建一個3部分片段,將其顯示在某個位置的視圖中,然後在設置「OFF」標誌時將其從視圖中移除。除了刪除細分外,每件事都有效。它甚至可以與(pickOne)交換並在標籤中顯示段碼。我無法工作的是以下兩種:setHidden:YES或removeAllSegments。任何幫助,將不勝感激。這是我的代碼。隱藏或移動SegmentContoller

- (void) showSegment { 

    int x = 192; 
    int y = 212; 

    int w = 125; 
    int h = 25; 

    SegUnit1 = @"A"; 
    SegUnit2 = @"B"; 
    SegUnit3 = @"C"; 

    threeSegs = [NSArray arrayWithObjects: SegUnit1, SegUnit2, SegUnit3, nil]; 
    segSize = [NSArray arrayWithArray:threeSegs]; 

    UISegmentedControl *heightSC = [[UISegmentedControl alloc] initWithItems:segSize]; 

    if ([segmentState_height isEqualToString:@"ON"]) { 

     NSLog(@"segmentState_height = %@",segmentState_height); 
     heightSC.frame = CGRectMake(x, y, w, h);  
     heightSC.segmentedControlStyle = UISegmentedControlStyleBar; 
     heightSC.selectedSegmentIndex = -1; 
     [heightSC addTarget:self 
        action:@selector(pickOne:) 
      forControlEvents:UIControlEventValueChanged]; 
     [self.view addSubview:heightSC]; 
     [heightSC release]; 
    } else if ([segmentState_height isEqualToString:@"OFF"]) { 

     NSLog(@"segmentState_height = %@",segmentState_height); 
     [heightSC setHidden:YES]; // NSLog showing "OFF" but segment will not hide. 
     [heightSC removeAllSegments]; // NSLog showing "OFF" and segment is suppose to dismantle and does not. 

    } 
} 

現在我知道,我必須「而不是」創建並在同一個函數中刪除,並給予有關更正此小費,但我不知道如何使用尖端。

這是什麼建議。

那麼,你的方法有點困惑,因爲你正試圖同時創建和隱藏。所以你可以考慮把它分解成單獨的方法。

在一般情況下,它會沿着這些路線:

代碼:

if ([self theControlProperty] == nil) 
{ 
    UISeg... *theControl = [[UISeg alloc] ....]; 

    [self setTheControlProperty:theControl]; 

    ... 

} 

if (shouldHideTheControl) 
{ 
    [[self theControlProperty] setHidden:YES]; 
} 

任何幫助,將不勝感激。

回答

2

您遇到的問題是您每次調用該方法時都要創建一個新的UISegmentedControl實例。第一次通過創建實例並將其作爲子視圖添加到視圖中。這顯然工作得很好,因爲它應該。然後該方法返回,並且不再有簡單的方法來引用您創建的實例。當您重新輸入-showSegment時,您將創建一個不同的實例,然後隱藏和/或銷燬它。這個不同的實例對您提供給視圖的實例沒有任何影響。

你需要做的是使heightSC成爲一個實例變量。將它添加到頭文件中的接口聲明中,然後將其初始化一次,然後根據需要隱藏或修改它。關鍵在於你需要引用正在屏幕上繪製的UISegmentedControl的實例,這個引用不在方法本身中,你可以使用第二,第三,第四等時間來調用該方法。

-1
[yourSegment removeFromSuperview]; 

+0

我已經試過了,用你的代碼,你不能刪除的SuperView從一個子視圖了崩潰。有辦法做到這一點,但它會刪除整個視圖。我想要做的就是隱藏段不刪除它。感謝您的迴應。 – hodji 2011-02-01 18:44:30

0

嘗試在按鈕選擇方法pickOne中使用刪除段。這將它放在showSegment方法之外,並匹配用戶所需的操作以進行更改並清除按鈕。

- (void) pickOne:(id)sender { 

    UISegmentedControl* userChose = sender; 

    if([userChose selectedSegmentIndex] == 0){ 

     your first button operation; 
     [heightSC removeAllSegments]; 
    } 

    if([userChose selectedSegmentIndex] == 1){ 

     your second button operation; 
     [heightSC removeAllSegments]; 

     } 
    if([userChose selectedSegmentIndex] == 2){ 

     your third button operation; 
     [heightSC removeAllSegments]; 

     } 
    } 
+0

這將工作,但我想清除它的視圖外的按鈕。即在看到它顯示之前或之後。無論如何,感謝您的建議。 – hodji 2011-02-02 00:09:13

0

我試過這個,得到了我一直在尋找的結果。感謝Mythogen和BrianSlick,我只需要檢查並確保沒有泄漏。現在這將是一項任務。

有誰知道我是否需要第二個[heightSC release];

// .H

@ interface ------ { 
UISegmentedControl *segmentPicked; 
} 

| 

@property (nonatomic, retain) UISegmentedControl *segmentPicked; 

// .M

| 

@synthesize segmentPicked; 

| 

if ([self segmentPicked] == nil) { 

    UISegmentedControl *heightSC = [[UISegmentedControl alloc] initWithItems:segSize]; 
    [self setSegmentPicked:heightSC]; 
    [heightSC release]; 
    heightSC.frame = CGRectMake(x, y, w, h);  
    heightSC.segmentedControlStyle = UISegmentedControlStyleBar; 
    heightSC.selectedSegmentIndex = -1; 
    [heightSC addTarget:self 
    action:@selector(pickOne:) 
    forControlEvents:UIControlEventValueChanged]; 
    [self.view addSubview:heightSC]; 
    [heightSC release]; 
} 

if ([segmentState_height isEqualToString:@"OFF"]) 
{ 
    [[self segmentPicked] setHidden:YES]; 
} else { 
    [[self segmentPicked] setHidden:NO]; 
}