2015-01-19 4 views
1

간단한 iPhone 앱을 만들려면 this guide을 따르고 있습니다.배열 지정시 인스턴스로 전송 된 인식 할 수없는 선택자

하지만 실행하려고하면 unrecognized selector sent to instance 예외가 발생합니다.

나는 디버깅을했고, 실패는이 라인에 발생 방법의

masterController.bugs = bugs;

(didFinishLaunchingWithOptions).

그건 내 첫 번째 앱이므로 분명해야합니다. 자습서에서 코드를 수정 한 유일한 변경 사항은 #import <UIKit/UIKit.h>입니다. 그렇지 않으면 코드를 컴파일 할 수 없기 때문에 추가되었습니다.

전체 메소드 코드 :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    RWTScaryBugDoc *bug1 = [[RWTScaryBugDoc alloc] initWithTitle:@"Potato Bug" rating:4 thumbImage:[UIImage imageNamed:@"potatoBugThumb.jpg"] fullImage:[UIImage imageNamed:@"potatoBug.jpg"]]; 
    RWTScaryBugDoc *bug2 = [[RWTScaryBugDoc alloc] initWithTitle:@"House Centipede" rating:3 thumbImage:[UIImage imageNamed:@"centipedeThumb.jpg"] fullImage:[UIImage imageNamed:@"centipede.jpg"]]; 
    RWTScaryBugDoc *bug3 = [[RWTScaryBugDoc alloc] initWithTitle:@"Wolf Spider" rating:5 thumbImage:[UIImage imageNamed:@"wolfSpiderThumb.jpg"] fullImage:[UIImage imageNamed:@"wolfSpider.jpg"]]; 
    RWTScaryBugDoc *bug4 = [[RWTScaryBugDoc alloc] initWithTitle:@"Lady Bug" rating:1 thumbImage:[UIImage imageNamed:@"ladybugThumb.jpg"] fullImage:[UIImage imageNamed:@"ladybug.jpg"]]; 
    NSMutableArray *bugs = [NSMutableArray arrayWithObjects:bug1, bug2, bug3, bug4, nil]; 

    UINavigationController *navController = (UINavigationController *) self.window.rootViewController; 
    MasterViewController *masterController = [navController.viewControllers objectAtIndex:0]; 
    masterController.bugs = bugs; 

    // Override point for customization after application launch. 
    UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController; 
    UINavigationController *navigationController = [splitViewController.viewControllers lastObject]; 
    navigationController.topViewController.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem; 
    splitViewController.delegate = self; 
    return YES; 
} 

전체 스택 트레이스 :

2015-01-19 10:06:10.489 ScaryBugs[6603:5904685] -[UINavigationController setBugs:]: unrecognized selector sent to instance 0x7d116690 
2015-01-19 10:06:10.491 ScaryBugs[6603:5904685] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setBugs:]: unrecognized selector sent to instance 0x7d116690' 
*** First throw call stack: 
(
    0 CoreFoundation      0x00951946 __exceptionPreprocess + 182 
    1 libobjc.A.dylib      0x005daa97 objc_exception_throw + 44 
    2 CoreFoundation      0x009595c5 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277 
    3 CoreFoundation      0x008a23e7 ___forwarding___ + 1047 
    4 CoreFoundation      0x008a1fae _CF_forwarding_prep_0 + 14 
    5 ScaryBugs       0x000f65a8 -[AppDelegate application:didFinishLaunchingWithOptions:] + 1288 
    6 UIKit        0x00cfe97c -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 291 
    7 UIKit        0x00cff687 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2869 
    8 UIKit        0x00d02c0d -[UIApplication _runWithMainScene:transitionContext:completion:] + 1639 
    9 UIKit        0x00d1b7d0 __84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke + 59 
    10 UIKit        0x00d0181f -[UIApplication workspaceDidEndTransaction:] + 155 
    11 FrontBoardServices     0x032919de __37-[FBSWorkspace clientEndTransaction:]_block_invoke_2 + 71 
    12 FrontBoardServices     0x0329146f __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke + 54 
    13 FrontBoardServices     0x032a3425 __31-[FBSSerialQueue performAsync:]_block_invoke + 26 
    14 CoreFoundation      0x008751c0 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 16 
    15 CoreFoundation      0x0086aad3 __CFRunLoopDoBlocks + 195 
    16 CoreFoundation      0x0086a238 __CFRunLoopRun + 936 
    17 CoreFoundation      0x00869bcb CFRunLoopRunSpecific + 443 
    18 CoreFoundation      0x008699fb CFRunLoopRunInMode + 123 
    19 UIKit        0x00d011e4 -[UIApplication _run] + 571 
    20 UIKit        0x00d048b6 UIApplicationMain + 1526 
    21 ScaryBugs       0x000f853d main + 141 
    22 libdyld.dylib      0x02cb7ac9 start + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 
(lldb) 

디버거 정보, 실패 전에이 :

Debugger info, before failure

답변

4
UINavigationController *navController = (UINavigationController *) self.window.rootViewController; 
MasterViewController *masterController = [navController.viewControllers objectAtIndex:0]; 

이 문제는 여기. 이 코드는 윈도우의 rootViewController이 탐색 컨트롤러라고 가정하지만 실제로는 분할보기 컨트롤러 일 것입니다. 그런 다음 네비게이션 컨트롤러 자체 인이 실제 - 아닌 - 실제 네비게이션 컨트롤러가 보유한 첫 번째보기 컨트롤러를 요청합니다. 물론 네비게이션 컨트롤러에는 bugs 속성이 없으므로 예외가 발생합니다.

분할보기 컨트롤러의 첫 번째보기 컨트롤러에 대한 참조를 얻으려면 다음은 반환 된 탐색 컨트롤러의 첫 번째보기 컨트롤러 인 실제 MasterViewController을 가져옵니다.

+0

고마워요! 어떤 id에 의해'MasterViewController'에 접근하는 방법이 있습니까? – Tigran

+1

그 트릭을 했어 : 'UISplitViewController * spliController = (UISplitViewController *) self.window.rootViewController; UINavigationController * navController = [spliController.viewControllers objectAtIndex : 0]; MasterViewController * masterController = [navController.viewControllers objectAtIndex : 0]; ' – Tigran

+0

그게 전부입니다. 다행스럽게 생각 했어. – warrenm