我有很多按鈕,我想在視圖加載時給一個標題。 按鈕的標題因當月的哪一天而異。因此我創建了一個包含所有按鈕名稱的數組。我的代碼如下:從一個數組中選擇一個按鈕的標題集 - Xcode iOS
我的界面:
@property (strong, nonatomic) IBOutlet UIButton *button1;
@property (strong, nonatomic) IBOutlet UIButton *button2;
@property (strong, nonatomic) IBOutlet UIButton *button3;
@property (strong, nonatomic) IBOutlet UIButton *button4;
@property (strong, nonatomic) IBOutlet UIButton *button5;
@property (strong, nonatomic) IBOutlet UIButton *button6;
@property (strong, nonatomic) IBOutlet UIButton *button7;
@property (strong, nonatomic) IBOutlet UIButton *button8;
@property (strong, nonatomic) IBOutlet UIButton *button9;
@property (strong, nonatomic) IBOutlet UIButton *button10;
我的實現:
@synthesize button1 = _button1;
@synthesize button2 = _button2;
@synthesize button3 = _button3;
@synthesize button4 = _button4;
@synthesize button5 = _button5;
@synthesize button6 = _button6;
@synthesize button7 = _button7;
@synthesize button8 = _button8;
@synthesize button9 = _button9;
@synthesize button10 = _button10;
NSArray *myArray = [NSArray arrayWithObjects: @"dummyButton", @"_button1", @"_button2", @"_button3", @"_button4", @"_button5", @"_button6", @"_button7", @"_button8", @"_button9", @"_button10", nil];
for (int i = varDefinedEarlier; i<=totalAmountOfNeededTitles; i++) {
NSString *theTitleSting = [NSString stringWithFormat:@"%i",i];
NSLog(@"arrayValue:%@", [myArray objectAtIndex:i]); //Works prints the button name (for example _button7)
[[myArray objectAtIndex:i] setTitle:theTitleSting forState:UIControlStateNormal];
//The line above is where the app crashes. Everything else works just fine.
}
提供給我的錯誤是一個「主題1:信號SIGABRT
此外,這是日誌說:
- [__ NSCFConstantString的setTitle:forState:]:無法識別的選擇發送到實例0x14a48 2012-07-10 22:57:49.649不要打破枷鎖[10302:707] * * *終止應用程序由於未捕獲異常 'NSInvalidArgumentException',原因是: ' - [__ NSCFConstantString的setTitle:forState:]:無法識別的選擇發送到實例0x14a48' * * *第一擲調用堆棧: (0x3774788f 0x3544c259 0x3774aa9b 0x37749915 0x376a4650 0xdc2f 0x31454c8b 0x314611e9 0x31461059 0x31460f3f 0x3146070b 0x31460503 0x31454aff 0x314547d5 0x314cd903 0x31547627 0x37fb8933 0x3771ba33 0x3771b699 0x3771a26f 0x3769d4a5 0x3769d36d 0x33e5c439 0x31449cd5 0xb7cf 0xb774) 終止調用拋出異常(lldb)
我認爲錯誤是我嘗試告訴應用程序女巫按鈕它需要設置標題,但我真的不知道如何讓它採取我的數組的值並將其用作按鈕名稱。
我在iPhone iOS開發很新,所以忍耐一下,如果答案很簡單;)
就像我說的..簡單的答案;)非常感謝您的幫助。它工作完美:) – MortenHC 2012-07-10 21:20:44
偉大的,我的榮幸。乾杯! – Till 2012-07-10 21:21:32