开发者

Runtime error when attempting to construct simple UITabBarController based iOS application

开发者 https://www.devze.com 2023-03-01 04:17 出处:网络
My code compiles but I\'m getting a runtime error when I attempt to launch it. Here is the runtime error:

My code compiles but I'm getting a runtime error when I attempt to launch it.

Here is the runtime error:

2011-04-25 23:56:40.689 Noun[39033:207] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NounAppDelegate 0x4e1c400> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key viewController.'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00dc75a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x00f1b313 objc_exception_throw + 44
    2   CoreFoundation                      0x00dc74e1 -[NSException raise] + 17
    3   Foundation                          0x0002f677 _NSSetUsingKeyValueSetter + 135
    4   Foundation                          0x0002f5e5 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 285
    5   UIKit                               0x004b330c -[UIRuntimeOutletConnection connect] + 112
    6   CoreFoundation                      0x00d3d8cf -[NSArray makeObjectsPerformSelector:] + 239
    7   UIKit                               0x004b1d23 -[UINib instantiateWithOwner:options:] + 1041
    8   UIKit                               0x004b3ab7 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 168
    9   UIKit                               0x002b917a -[UIApplication _loadMainNibFile] + 172
    10  UIKit                               0x002b9cf4 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 2开发者_如何学C91
    11  UIKit                               0x002c4617 -[UIApplication handleEvent:withNewEvent:] + 1533
    12  UIKit                               0x002bcabf -[UIApplication sendEvent:] + 71
    13  UIKit                               0x002c1f2e _UIApplicationHandleEvent + 7576
    14  GraphicsServices                    0x0171f992 PurpleEventCallback + 1550
    15  CoreFoundation                      0x00da8944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    16  CoreFoundation                      0x00d08cf7 __CFRunLoopDoSource1 + 215
    17  CoreFoundation                      0x00d05f83 __CFRunLoopRun + 979
    18  CoreFoundation                      0x00d05840 CFRunLoopRunSpecific + 208
    19  CoreFoundation                      0x00d05761 CFRunLoopRunInMode + 97
    20  UIKit                               0x002b97d2 -[UIApplication _run] + 623
    21  UIKit                               0x002c5c93 UIApplicationMain + 1160
    22  Noun                                0x00001e68 main + 102
    23  Noun                                0x00001df9 start + 53
)
terminate called after throwing an instance of 'NSException'

And here is my code (relevant methods)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //Initiate TabBarController
    self.tabBarController = [[UITabBarController alloc] init];

    NSMutableArray *viewControllersForTabController = [[NSMutableArray alloc] initWithCapacity:2];

    NounViewController *firstViewController = [[NounViewController alloc] initWithNibName:@"NounViewController" bundle:[NSBundle mainBundle]];
    [viewControllersForTabController addObject:firstViewController];
    [firstViewController release];

    PostLoginViewController *secondViewController = [[PostLoginViewController alloc] initWithNibName:@"PostLoginViewController" bundle:[NSBundle mainBundle]];
    [viewControllersForTabController addObject:secondViewController];
    [secondViewController release];

    [tabBarController setViewControllers:viewControllersForTabController];
    [viewControllersForTabController release];

    [window addSubview:tabBarController.view];

    return YES;
}

I'm sure it's probably something trivial that I've forgotten to do, I'm slowly getting up to speed with iOS development


"NSUnknownKeyException" this exception comes when you are accessing a key that is not present, this is not always because of code, there may be some mis connection in Interface Builder. There is no problem is the above code, i tried it my end, its running successfully. Try to check your Interface builder connection may be you are missing some thing there. Most probably first you are trying to add a tabBarController through IB then you are doing it by code.


I had the same issue earlier. The problem is that there is a interface builder connection in the xib file for which there is no corresponding IBOutlet entry in the app delegate. This is how I was able to fix it: Look for the XML tag "IBObjectContainer" in the MainWindow.xib file. You will most likely find an entry like this:

<object class="IBConnectionRecord">
    <object class="IBCocoaTouchOutletConnection" key="connection">
        <string key="label">viewController</string>
        <reference key="source" ref="664661524"/>
        <reference key="destination" ref="943309135"/>
    </object>
    <int key="connectionID">11</int>
</object>

You can remove this entry safely since it seems to have gotten introduced into the xib file by mistake. Then the app delegate stops expecting an IBOutlet entry. Alternately you can add an IBOutlet entry in the app delegate by name "viewController".

Make sure you back up the xib file before you make any changes.

There is probably an easier way to get rid of this from the IB, but i don't normally use to the IB, this is what worked for me.

0

精彩评论

暂无评论...
验证码 换一张
取 消