[英]Determine Button Background Image - Objective-C (Xcode)
我有一個視圖控制器與不同的按鈕,分配了背景圖像。 我的頭文件如下所示:
@interface ImageSelect : UIViewController
- (IBAction)ImageButton:(id)sender;
- (IBAction)Done:(id)sender;
@property(nonatomic, readonly, retain) UIImage *currentImage;
@end
我的主文件中包含按鈕方法的部分如下所示:
- (IBAction)ImageButton:(id)sender {
if ([@"railway-336702_1280.jpg" isEqual:self.currentImage]) {
img = @"railway-336702_1280.jpg";
NSLog(@"Test");
}
}
我想將圖像名稱保存到名為img的NSString。 目前,代碼運行但實際上並不執行保存到img。
一種更簡單的方法是:如果你只想檢查UIButton
是否包含你設置的圖像,你可以簡單地使用tag
屬性:
假設您使用名為:railway-336702_1280.jpg的圖像設置其圖像
將tag
設置為更簡單的“1280”。
然后針對整數檢查此tag
。 如果您再次更改圖像,請相應更改tag
。
if (((UIButton*)sender).tag == 1280)
{
// Do stuff
}
創建UIImage后,它將丟失與加載文件的所有連接。 您無法在運行時確定背景圖像是什么。
如果需要在運行時獲取名稱,請將UIButton子類化,然后創建名為imageName的屬性,並使用Interface Builder通過用戶定義的運行時屬性進行設置。
例:
在ImageSelectButton.m中:
@interface ImageSelectButton ()
@property(nonatomic, retain) NSString *imageName;
@end
在ImageSelect.m中:
@interface ImageSelect
...
- (IBAction)ImageButton:(ImageSelectButton*)sender {
if ([sender.imageName isEqualToString:@"railway-336702_1280.jpg"]) {
// Do stuff
}
}
...
在Interface Builder中選擇UIButton時,設置Custom Class和runtime屬性:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.