2012-04-24 190 views
1

我在下面有一些來源。爲什麼這會導致SIGABRT錯誤?

- (void)Button:(UIButton *)button { 

    NSString *imageName = ((UIButton *)[self.view viewWithTag:button.tag]).titleLabel.text; 

} 

- (void)viewDidLoad { 

    NSMutableArray *_array = [[NSMutableArray alloc] init]; 

    NSInteger iCount = [_array count]; 

    for (i = 0; iCount > i; i++) { 

     UIButton *btn = [[UIButton alloc] init]; 
     btn.titleLabel.text = [[_array objectAtIndex:i] objectForKey:@"FILE"]; 
     btn.tag = i; 
     [btn addTarget:self action:@selector(Button:) forControlEvents:UIControlEventTouchUpInside]; 
     [self.view addSubview:btn]; 
     [btn release]; 

} 

當我使用0索引標記訪問Button方法時,出現SIGABRT錯誤。 我能做什麼?

+0

你得到這個錯誤,因爲你傳遞的自己的對象實際上是視圖控制器,而不是按鈕。 – rishi 2012-04-24 06:25:54

回答

2

正確讀取您的控制檯輸出,默認情況下,每個視圖的標記都爲'0',因此它可能會因爲除了uibutton以外的其他視圖而崩潰,並且可能是該視圖不具有titleLabel屬性。因爲它是UIButton的屬性。

+0

***由於未捕獲的異常'NSInvalidArgumentException',原因:' - [UIView titleLabel]:無法識別的選擇器發送到實例0x98053a0' ***第一次拋出調用堆棧:blar blar .. – 2012-04-24 06:31:46

+0

控制檯消息高於 – 2012-04-24 06:32:07

+1

** [UIView titleLabel]:**它應該是** [UIButton titleLabel] **,它意味着 - [self.view viewWithTag:button.tag]這裏返回UIView(您當前的視圖(您通過'self'訪問)) )..你需要用1或別的東西來啓動按鈕標籤。但不能用0.所以它不會與任何視圖發生衝突。 – 2012-04-24 06:38:09