2013-04-03 37 views
2

這是我的代碼:ios-「交換」UIViews可能嗎?

if([pantallas objectForKey:par]){ 
    UIView *vista= [[UIView alloc] initWithFrame:self.Botones.frame]; 
    vista.backgroundColor= [UIColor brownColor]; 

    CGSize la= CGSizeMake(50,60); 
    int cuantos= [part2 count]; 
    NSArray *arr= [COLGenerales tileN:cuantos RectsOfSize:la intoSpaceOf:vista.frame withMaxPerRow:5 spaceVertical:10 spaceHorizontal:10]; 
    for(int j=0; j<cuantos; j++){ 
     UIButton *bot= [[UIButton alloc] initWithFrame:[[arr objectAtIndex:j] CGRectValue]]; 
     bot.tag=j; 
     bot.titleLabel.text=par; 
     bot.titleLabel.hidden=true; 
     bot.imageView.image = [UIImage imageNamed:[[part2 allKeys] objectAtIndex:j]]; 
     [bot addTarget:self action:@selector(registrar:) forControlEvents:UIControlEventTouchDown]; 
     [vista addSubview:bot]; 

    } 

    [pantallas setObject:vista forKey:par]; 

    self.Botones= vista; 
    }else{ 
    self.Botones= [pantallas objectForKey:par]; 
} 

Botones是嵌入到視圖的簡單視圖這類控制(第一通過筆尖文件發起的),COLGenerales的類方法返回編碼爲NSValues CGRects的陣列,和註冊:是一種本地方法。

一切都正確設置(我用調試器徹底檢查了這一點)。視圖被成功創建,設置並添加到字典中。

但是,我絕對沒有得到實際的屏幕更改。我甚至包含了背景顏色變化,以檢查它是否不是按鈕的某種問題。沒有。任何建議的解決方案?

+0

此代碼在哪裏存在,以及它如何被調用?此視圖何時添加到屏幕視圖? – 2013-04-03 19:50:27

+0

在詳細視圖的控制器的函數內,這由主視圖調用。 Botone本身就是細節場景的一部分(其IBOutlet屬性設置爲「strong」),並且我認爲改變它的值應該反映在屏幕上的內容上。 – 2013-04-03 20:15:58

回答

1

作爲IBOutlet的屬性沒有與視圖層次結構的內在連接 - 它只能從xib中填充該屬性。當您設置self.Botones,你需要做一些這樣的:

[self.Botones removeFromSuperview]; 
self.Botones = newValue; 
[self.BotonesSuperview addSubview:self.Botones]; 

如果你在很多地方更新self.Botones,而你總是要變化反映在屏幕上,你可以添加到這個setter實現:

-(void)setBotones:(UIView*)newValue { 
    if (newValue != _Botones) { 
     [_Botones removeFromSuperview]; 
     _Botones = newValue; 
     [self.BotonesSuperview addSubview:_Botones]; 
    } 
} 
+0

完美的工作,但現在不顯示按鈕。作爲對象,他們必須在添加之前在特定的範圍內嗎? _Botones的子視圖計數器本身正在增加。 – 2013-04-03 21:37:38

+1

不要使用'button.imageView.image',使用'[set setImage:forState:]'。 – 2013-04-03 21:43:57