2012-01-15 57 views
0

我有一個UITableViewCell有2個UIButton實例。兩個按鈕都有一個默認圖像。只有「壞」按鈕才具有禁用狀態的圖像。而已。神祕的UIButton崩潰

在細胞類這兩個按鈕被聲明爲與IBOutlet關鍵字屬性:

@property (nonatomic, readonly) IBOutlet UIButton* buttonCall; 
@property (nonatomic, readonly) IBOutlet UIButton* buttonGPS; 

在的.xib 2個按鈕,並在類中的屬性之間的連接製成。

所以,當的.xib加載以下完成:

MyCell* cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:cellID]; 
if (nil == cell) 
{ 
    NSArray* nibContents = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:nil options:nil]; 
    cell = [nibContents objectAtIndex:0]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
} 

// gps button 
[cell.buttonGPS setTitle:[NSString stringWithFormat:@"%d", indexPath.row] forState:UIControlStateNormal]; 
[cell.buttonGPS addTarget:self action:@selector(actionGPS:) forControlEvents:UIControlEventTouchUpInside]; 

if (...condition...) 
{ 
    cell.buttonCall.enabled = NO; 
    [cell.buttonCall removeTarget:self action:@selector(actionCall:) forControlEvents:UIControlEventTouchUpInside]; 
} 
else 
{ 
    cell.buttonCall.enabled = YES; 
    [cell.buttonCall setTitle:[...TheTitleString...] forState:UIControlStateNormal]; 
    [cell.buttonCall addTarget:self action:@selector(actionCall:) forControlEvents:UIControlEventTouchUpInside]; 
} 

當視圖控制器似乎成功地經過(UITableViewCell*)tableView:cellForRowAtIndexPath:的所需次數。然後應用程序崩潰和以下堆棧:

0 kill 
... 
[UIButton imageRectForContentRect:] 
[UIButton(UIButtonInternal) _setupImageView] 
[UIButton layoutSubviews] 
... 
start 

我除去按鈕的圖像,並開始與堆棧崩潰:

0 kill 
... 
[UIButton layoutSubviews] 
... 
start 

我添加了一些標識符標籤的按鈕 - 仍然會崩潰。

爲「壞」按鈕刪除了XCode-InterfaceBuilder連接,並且停止了崩潰。但該按鈕不起作用。

任何想法是什麼導致崩潰?

調用堆棧:

0 CoreFoundation      0x013d406e __exceptionPreprocess + 206 
1 libobjc.A.dylib      0x019f0d0a objc_exception_throw + 44 
2 CoreFoundation      0x013d5ced -[NSObject doesNotRecognizeSelector:] + 253 
3 CoreFoundation      0x0133af00 ___forwarding___ + 432 
4 CoreFoundation      0x0133ace2 _CF_forwarding_prep_0 + 50 
5 UIKit        0x0086de3a -[UIButton imageRectForContentRect:] + 350 
6 UIKit        0x0086ed5b -[UIButton(UIButtonInternal) _setupImageView] + 158 
7 UIKit        0x0086e5f8 -[UIButton layoutSubviews] + 693 
8 UIKit        0x0069f322 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 178 
9 CoreFoundation      0x013d5e72 -[NSObject performSelector:withObject:] + 66 
10 QuartzCore       0x0042092d -[CALayer layoutSublayers] + 266 
11 QuartzCore       0x0042a827 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 231 
12 QuartzCore       0x003b0fa7 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 377 
13 QuartzCore       0x003b2ea6 _ZN2CA11Transaction6commitEv + 374 
14 QuartzCore       0x003b2580 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 80 
15 CoreFoundation      0x013a89ce __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30 
16 CoreFoundation      0x0133f670 __CFRunLoopDoObservers + 384 
17 CoreFoundation      0x0130b4f6 __CFRunLoopRun + 1174 
18 CoreFoundation      0x0130adb4 CFRunLoopRunSpecific + 212 
19 CoreFoundation      0x0130accb CFRunLoopRunInMode + 123 
20 GraphicsServices     0x03258879 GSEventRunModal + 207 
21 GraphicsServices     0x0325893e GSEventRun + 114 
22 UIKit        0x00660a9b UIApplicationMain + 1175 
23 MyApp        0x000023f9 main + 169 
24 MyApp        0x00002345 start + 53 

例外是:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance 0x146acd8' 

info symbol 0x146acd8 
__kCFNull in section LC_SEGMENT.__DATA.__data of /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation 

第二種情況是相似的。但是,例外情況完全相同。這裏是調用堆棧:

0 CoreFoundation      0x013d406e __exceptionPreprocess + 206 
1 libobjc.A.dylib      0x019f0d0a objc_exception_throw + 44 
2 CoreFoundation      0x013d5ced -[NSObject doesNotRecognizeSelector:] + 253 
3 CoreFoundation      0x0133af00 ___forwarding___ + 432 
4 CoreFoundation      0x0133ace2 _CF_forwarding_prep_0 + 50 
5 UIKit        0x0086e851 -[UIButton layoutSubviews] + 1294 
6 UIKit        0x0069f322 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 178 
7 CoreFoundation      0x013d5e72 -[NSObject performSelector:withObject:] + 66 
8 QuartzCore       0x0042092d -[CALayer layoutSublayers] + 266 
9 QuartzCore       0x0042a827 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 231 
10 QuartzCore       0x003b0fa7 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 377 
11 QuartzCore       0x003b2ea6 _ZN2CA11Transaction6commitEv + 374 
12 QuartzCore       0x003b2580 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 80 
13 CoreFoundation      0x013a89ce __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30 
14 CoreFoundation      0x0133f670 __CFRunLoopDoObservers + 384 
15 CoreFoundation      0x0130b4f6 __CFRunLoopRun + 1174 
16 CoreFoundation      0x0130adb4 CFRunLoopRunSpecific + 212 
17 CoreFoundation      0x0130accb CFRunLoopRunInMode + 123 
18 GraphicsServices     0x03258879 GSEventRunModal + 207 
19 GraphicsServices     0x0325893e GSEventRun + 114 
20 UIKit        0x00660a9b UIApplicationMain + 1175 
21 MyApp        0x000023f9 main + 169 
22 MyApp        0x00002345 start + 53 
+1

我們可以看到崩潰細節嗎? – 2012-01-15 14:34:41

+0

任何其他信息?如果你需要任何具體的問題。 – 2012-01-15 15:17:11

+0

拋出的實際異常是什麼? – 2012-01-15 15:23:07

回答

1

從例外情況看,您認爲您將NSString傳遞給按鈕的標題,而實際上您正在傳遞NSNull的實例。如果您從JSON源或類似的表中獲取表視圖數據,這非常常見 - 如果源字段不存在,則使用NSNull。

你得到的例外是長度不是NSNull的公認的選擇器,這是真的,它似乎出現在按鈕佈置它的子視圖時,它會在你分配一個新標題時做。所以我會專注於你的調查。檢查一下您實際發送的按鈕標題。

0
MyCell* cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:cellID]; 
if (nil == cell) 
{ 
    NSArray* nibContents = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:nil options:nil]; 
cell = [nibContents objectAtIndex:0]; 
cell.selectionStyle = UITableViewCellSelectionStyleNone; 
[cell.buttonGPS addTarget:self action:@selector(actionGPS:)  forControlEvents:UIControlEventTouchUpInside]; 
[cell.buttonCall addTarget:self action:@selector(actionCall:) forControlEvents:UIControlEventTouchUpInside]; 
} 

// gps button 
[cell.buttonGPS setTitle:[NSString stringWithFormat:@"%d", indexPath.row] forState:UIControlStateNormal]; 

if (...condition...) 
{ 
cell.buttonCall.enabled = NO; 
} 
else 
{ 
cell.buttonCall.enabled = YES; 
[cell.buttonCall setTitle:[...TheTitleString...] forState:UIControlStateNormal]; 
} 

請在我們的代碼&檢查這些變化。我認爲某些情況可能是錯誤的,所以b4增加了它被刪除的目標,當它試圖崩潰時。