2010-05-10 26 views
5

我使用的標籤屬性來訪問一個UIButton獲得在一個UIButton與標籤屬性IPhone

UIButton *randomButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect ]];  
    randomButton.frame = CGRectMake(205, 145, 90, 22); // size and position of button 
    [randomButton setTitle:@"Random" forState:UIControlStateNormal]; 
    randomButton.backgroundColor = [UIColor clearColor]; 
    randomButton.adjustsImageWhenHighlighted = YES; 
    [randomButton addTarget:self action:@selector(getrandom:) 
      forControlEvents:UIControlEventTouchUpInside]; 
    randomButton.reversesTitleShadowWhenHighlighted=YES; 
    randomButton.toggleButton 

    [self.view addSubview:randomButton]; 

    randomButton.tag=333; 

再後來就在代碼中有一個小麻煩我嘗試在按鈕通過以下方式獲得這給了我一個錯誤說

不兼容的Objective-C類型 初始化 '結構的UIView *', 預期 '結構的UIButton *'

UIButton *random = [self.view viewWithTag:333]; 
    random.highlighted=NO; 

回答

23

嘗試:

UIButton *random = (UIButton *)[self.view viewWithTag:333]; 

而且,你爲什麼分配標記,你可以鬆開按鈕後?

+0

謝謝,你的代碼擺脫了我的警告。整個保留/釋放的東西不應該在那裏。 即使我現在成功地進入按鈕,我似乎不能將其狀態更改爲「突出顯示」。我嘗試在UIControlEventTouchUpInside事件的處理程序中執行此操作。 基本上我有3個按鈕,我試圖實現切換效果。一次只顯示一個按鈕 – dubbeat 2010-05-10 14:04:27