如何製作一個顯示「顯示圖片」的按鈕,當它被點擊時,它會變爲「隱藏圖片」。我是新來的目標C,我知道如何在界面生成器中製作按鈕,但不知道如何切換按鈕的文本和功能。有人可以幫我嗎?在iPhone上切換UIButton
0
A
回答
1
濫用按鈕的.tag屬性。掛鉤觸摸下壓動作給這個函數:
-(IBAction)buttonClick:(UIButton*)sender
{
if (sender.tag)
{
sender.tag = 0;
sender.text = @"Show Picture";
// do actions when "hide" is clicked
} else {
sender.tag = 1;
sender.text = @"Hide Picture";
// do actions when "show" is clicked
}
}
1
而不是使用標籤屬性(AB),你也可以簡單地切換的按鈕來選擇,而不是選擇,就像這樣:
- (IBAction)myButtonAction:(id)sender
{
[sender setSelected:![sender isSelected]];
// or in Objective-C 2.0 if you're so inclined
sender.selected = !sender.selected;
}
在IB中,您可以直接在檢查器中(或以編程方式通過setTitle:forState:方法)設置正常和按鈕的選定狀態的文本。
0
與這一個棘手的事情是,一個UIButton沒有「正式」的文本正確 - 在這裏看到的文檔:
,因爲它的設計有文本的多個獨立的套,根據按鈕的當前狀態顯示;即當前是啓用還是禁用,突出顯示等等。所以沒有一個簡單的屬性可以設置使其工作。
所以,你要聲明你這樣的按鈕,因爲這兩個動作和出口:
從button.h:
// inside the class declaration
BOOL pictureShown ; // initializer not required, defaults to 0 (NO)
UIButton * sampleButton ;
// skip irrelevant lines here
@property (nonatomic, retain) IBOutlet UIButton * sampleButton ;
- (IBAction) doSampleButton ;
胡克都在Interface Builder那些起來的,然後使用setTitle:forState:方法更改文本(在這種情況下,我已指定所有狀態,因此標題在所有狀態中保持不變)。例如:
從button.m:
@synthesize sampleButton ;
- (IBAction) doSampleButton {
if (pictureShown == YES) {
// hide the picture, and then...
[sampleButton setTitle: @"Show Picture" forState: (UIControlStateNormal | UIControlStateHighlighted | UIControlStateSelected | UIControlStateDisabled)] ;
pictureShown = NO ;
} else {
// show the picture, and then...
[sampleButton setTitle: @"Hide Picture" forState: (UIControlStateNormal | UIControlStateHighlighted | UIControlStateSelected | UIControlStateDisabled)] ;
pictureShown = YES ;
}
}
您還會注意到我已經聲明一個實例變量,「pictureShown」,在與按鈕視圖的視圖控制器,跟蹤當前按鈕的「模式」,並且我基本上在按鈕的動作中使用if語句來確定根據按鈕的當前模式執行哪個功能,並相應地切換按鈕文本。 (我正在使用它來跟蹤按鈕的當前模式,而不是直接檢查當前按鈕的標題或按鈕上存儲狀態的其他方式,因爲MVC明智的,這種類型。的狀態中,控制器類所屬)
(如果函數需要大量的代碼,我會使用消息來自,即:
[self showPicture] ;
[self hidePicture] ;
以避免必須填滿它所有內部doSampleButton,但這是純粹的風格,而不是技術上的要求。
3
請勿濫用標籤屬性。建議只用作按鈕標識符(例如,當您的視圖中有幾個按鈕將標籤設置爲0,1,2 ......因此可以識別哪個按鈕是發件人時)。你可以設置一些全局變量,並相應地改變它的值。
相關問題
- 1. iPhone iOS 4 UIButton切換突出顯示的狀態
- 2. 在iPhone SDK中切換xib?
- 3. iPhone視圖切換
- 4. iphone sdk在uibutton上隱藏uitextview點擊
- 5. 在iPhone上繪製三角形UIButton?
- 6. 的UIButton不會留在iPhone上
- 7. 懸停在iPhone上的UIButton狀態
- 8. 編程一個UIButton作爲切換
- 9. 切換上一個UIButton中的UILabel文本編程
- 10. 如何在iPhone上的UINavigationBar上切換頁面
- 11. UIButton image property iPhone
- 12. iPhone視圖切換基礎
- 13. iPhone開發 - 切換視圖
- 14. 切換意見 - iPhone開發
- 15. 將UIButton的長按在iPhone
- 16. iPhone切換屏幕在xcode 4.2
- 17. iPhone:IB,在iPhone和iPad之間切換
- 18. Bootstrap 3.3.5 - 導航欄切換按鈕無法在iphone上工作
- 19. 引導3.2下拉菜單切換不能在iPhone上工作
- 20. iPhone 2G上的自定義UIButton圖像
- 21. UIButton上的圖標/圖像iPhone
- 22. UIButton上的動畫點擊iPhone
- 23. 在MVVM上切換視圖
- 24. 無法在Firefox上切換
- 25. 在Interface Builder中使用UIButton創建自定義切換
- 26. 如何切換到不同的UITabBarItem在UIButton touch up內
- 27. iPhone - 的UIButton - 殘障
- 28. 自定義UIButton iphone
- 29. iPhone - UIButton背景圖像有時被切斷
- 30. 切換視圖iphone - 切換到空視圖
謝謝。這是什麼?標籤屬性呢? – Fasid 2010-08-06 23:44:55
通常情況下,您會以相反的方式使用它:這是一種在IB中「標記」按鈕(或其他項)的簡單方法,因此您只能有一個處理程序接收事件,從標記中可以看到該操作來自哪個按鈕。 (你仍然可以混合這兩個東西,例如通過標記按鈕0,2,4,...並使用'sender.tag^= 1;'翻轉最後一位,並使用'sender.tag&1'進行測試,但我故意保持例子非常簡單,所以你看到發生了什麼。 – mvds 2010-08-06 23:51:38
我明白了。謝謝... – Fasid 2010-08-07 00:08:08