如何製作一個顯示「顯示圖片」的按鈕,當它被點擊時,它會變爲「隱藏圖片」。我是新來的目標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. 通過UIButton切換視圖?
- 2. UIButton的切換功能
- 3. iPhone iOS 4 UIButton切換突出顯示的狀態
- 4. 在iPhone的觸摸動畫UIButton交換?
- 5. 在iPhone SDK中切換xib?
- 6. 在同一個標簽上的三個UIButton之間切換selectedState
- 7. iPhone視圖切換
- 8. 在iPhone上繪製三角形UIButton?
- 9. UIButton沒有出現在iPhone 4上
- 10. iphone sdk在uibutton上隱藏uitextview點擊
- 11. 懸停在iPhone上的UIButton狀態
- 12. 的UIButton不會留在iPhone上
- 13. UIButton沒有出現在iPhone 5S上
- 14. 在iPhone上剪切視頻
- 15. UIButton setTitle切換,自動重新加載?
- 16. 編程一個UIButton作爲切換
- 17. 如何在iPhone上的UINavigationBar上切換頁面
- 18. 切換上一個UIButton中的UILabel文本編程
- 19. iphone應用程序顯示uibutton轉換
- 20. 在Imageviews中的圖像在SwingGGridView上下切換iphone
- 21. UIButton image property iPhone
- 22. iPhone開發 - 切換視圖
- 23. 切換意見 - iPhone開發
- 24. iPhone視圖切換基礎
- 25. 將UIButton的長按在iPhone
- 26. UIButton切斷圖像
- 27. iPhone切換屏幕在xcode 4.2
- 28. 在iPhone和iPad之間切換Div
- 29. iPhone:IB,在iPhone和iPad之間切換
- 30. 使用Monotouch在iPhone上切換3G和WiFi
謝謝。這是什麼?標籤屬性呢? – 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