2011-10-08 73 views
0

代碼:self.parentViewController記錄的UINavigationController,但像個UIViewController中

- (IBAction)continueTouchHandler:(id)sender { 
    RegistrationViewController *registration = [[RegistrationViewController alloc] initWithNibName:@"RegistrationView" bundle:nil]; 
    UINavigationController *navController = (UINavigationController *)self.parentViewController; 
    [navController pushViewController:registration animated:YES]; 

    [navController release]; 
    [registration release]; 
} 

這是被稱爲上UIButtonTouchUpInside

NSLog(@"%@", self.parentViewController)日誌一個UINavigationController但沒有類型轉換self.parentViewController作爲UINavigationController我從Xcode後續警告: Incompatible pointer types initializing 'UINavigationController *' with an expression of type 'UIViewController *'

我想我得到的,因爲Xcode認爲self.parentViewControllerUIViewController警告。那是當我決定我需要「鍵入」(我不是xcode/ios dev)。

斯特拉克跟蹤類型轉換前:類型轉換後

GNU gdb 6.3.50-20050815 (Apple version gdb-1518) (Sat Feb 12 02:52:12 UTC 2011) 
Copyright 2004 Free Software Foundation, Inc. 
GDB is free software, covered by the GNU General Public License, and you are 
welcome to change it and/or distribute copies of it under certain conditions. 
Type "show copying" to see the conditions. 
There is absolutely no warranty for GDB. Type "show warranty" for details. 
This GDB was configured as "x86_64-apple-darwin".sharedlibrary apply-load-rules all 
Attaching to process 99533. 
Current language: auto; currently objective-c 
(gdb) 

堆棧跟蹤:

GNU GDB 6.3.50-20050815(蘋果版GDB-1518)(週六2月12日二時52分12秒版權所有2004 Free Software Foundation,Inc. GDB免費 軟件,覆蓋GNU通用公共許可證,您是 ,歡迎您在 條件下對其進行更改和/或分發它的副本。輸入「show copying」查看條件。對於GDB,絕對不存在 。請輸入「顯示保修」以瞭解詳情。 這個GDB被配置爲「x86_64-apple-darwin」.sharedlibrary apply-load-rules all附加到進程99324. 2011-10-08 14:27:52.593你喜歡我嗎[99324:207] - [RegistrationViewController tableView:numberOfRowsInSection:]:無法識別的選擇器發送到 實例0x6833500 2011-10-08 14:27:52.596你喜歡我嗎?[99324:207] *終止應用程序由於未捕獲異常'NSInvalidArgumentException',原因:' - [RegistrationViewController tableView:numberOfRowsInSection:]:無法識別的選擇器發送到 實例0x6833500' *調用堆棧在第一次拋出:(0 CoreFoundation 0x00dc35a9 exceptionPreprocess + 185 1 libob jc.A.dylib
0x00f17313 objc_exception_throw + 44 2的CoreFoundation
0x00dc50bb - [NSObject的(NSObject的)doesNotRecognizeSelector:] + 187 3
的CoreFoundation 0x00d34966 __ 轉發
+ 966 4的CoreFoundation 0x00d34522 _CF_forwarding_prep_0 + 50 5的UIKit 0x001d22b7 - [UISectionRowData refreshWithSection:的tableView:tableViewRowData:] + 1834 6的UIKit
0x001cfd88 - [UITableViewRowData numberOfRows] + 108 7的UIKit
0x00083677 - [UITableView的noteNumberOfRowsChanged] + 132 8的UIKit
0x00090708 - [UITableView的reloadData] + 773 9的UIKit
0x0008d844 - [UITableView的layoutSubviews] + 42 10 QuartzCore
0x016ada5a - [CALayer的layoutSublayers] + 181 11 QuartzCore
0x016afddc CALayerLayoutIfNeeded + 220 12 QuartzCore
0x016550b4 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310 13 QuartzCore 0x01656294 _ZN2CA11Transaction6commitEv + 292 14 QuartzCore 0x0165646d _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99 15的CoreFoundation 0x00da489b CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 27 16的CoreFoundation 0x00d396e7 __CFRunLoopDoObservers + 295 17的CoreFoundation 0x00d021d7 __CFRunLoopRun + 1575 18的CoreFoundation 0x00d01840 CFRunLoopRunSpecific + 208 19的CoreFoundation
0x00d01761 CFRunLoopRunInMode + 97個20 GraphicsServices
0x00ffb1c4 GSEventRunModal + 217 21 GraphicsServices
0x00ffb289 GSEventRun + 115 22的UIKit
0x00023c93 UIApplicationMain + 1160 23你喜歡我嗎
0x00001fde main + 126 24你喜歡我
0x00001f55 start + 53)拋出 的實例後調用'NSException'當前語言:auto;目前客觀-c(gdb)

我應該如何去過去呢?

回答

3

三件事。首先,使用navigationController屬性而不是parentViewController。因此,而不是這樣的:

UINavigationController *navController = (UINavigationController *)self.parentViewController; 
[navController pushViewController:registration animated:YES]; 

做到這一點:

[self.navigationController pushViewController:registration animated:YES]; 

其次,不發送release消息,導航控制器:

[navController release]; // Don't do this! 

第三,在錯誤消息控制檯給出了有關問題性質的強烈暗示:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 
'-[RegistrationViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x6833500 

這意味着程序崩潰嘗試將消息tableView:numberOfRowsInSection:發送到RegistrationViewController的實例,因此請確保該類具有缺少方法的實現。 (注意:如果您認爲它已經實現了該方法,請仔細檢查以確保拼寫正確。)

+0

出於好奇,爲什麼我不釋放navController?我仍然敬畏於你如此精確地提出這一點。我得到了答案,但它只是表明我仍然有很多東西需要學習。謝謝你的幫助! – Jacksonkr

+1

@Jackson不客氣。用保留/釋放手動引用計數有點複雜。爲了理解何時,更重要的是*爲什麼*您應該(或不應該)發送'release'或'autorelease'消息,請閱讀iOS內存管理指南中記錄的對象所有權規則:http://developer.apple .COM /庫/ IOS /#文檔/可可/概念/ MemoryMgmt /用品/ MemoryMgmt.html – jlehr

相關問題