Beginner Troubles -- Tab Bar & Views

Discussion in 'iOS Programming' started by Stachelsk, Jan 31, 2009.

  1. Stachelsk macrumors regular

    Joined:
    Dec 17, 2008
    #1
    I used Apple's template to make a MainWindow.xib file with a TarBar at the bottom. I then made 2 more views, FirstView.xib and AboutView.xib. I also made two copies of view controllers, FirstViewController.h/m and AboutViewController.h/m. Then I set AboutView.xib's class identity to AboutViewController and set FirstView.xib's class identity to FirstViewController Then I assigned each tab to a nib. The first tab's view is loaded from FirstView.xib and the second tab's view is loaded from AboutView.xib.

    I then added a label to AboutView.xib. Made a referencing outlet to a IBOutlet (UILabel*) called dayLabel. When I run the program and try to switch to the AboutView tab, the gdb reports a crash.

    Code:
    [Session started at 2009-02-01 00:23:26 -0500.]
    2009-02-01 00:23:29.778 Marathon[8761:20b] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x523c90> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key dayLabel.'
    2009-02-01 00:23:29.782 Marathon[8761:20b] Stack: (
        2496831755,
        2488671803,
        2496830513,
        2523021960,
        2523020526,
        2523563969,
        818019035,
        2496785477,
        818013737,
        818022028,
        816607014,
        816608011,
        816608381,
        816649675,
        816645441,
        816115430,
        818036263,
        816115430,
        816516406,
        816517630,
        816516519,
        816115430,
        816516406,
        816517630,
        816514372,
        816216343,
        816148479,
        816144864,
        827743722,
        827753484,
        2496333301,
        2496335064,
        827745792,
        827745989,
        816114848,
        816160924,
        9736,
        9590
    )
    
    [Session started at 2009-02-01 00:23:29 -0500.]
    Loading program into debugger…
    GNU gdb 6.3.50-20050815 (Apple version gdb-962) (Sat Jul 26 08:14:40 UTC 2008)
    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 "i386-apple-darwin".warning: Unable to read symbols for "/System/Library/Frameworks/UIKit.framework/UIKit" (file not found).
    warning: Unable to read symbols from "UIKit" (not yet mapped into memory).
    warning: Unable to read symbols for "/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics" (file not found).
    warning: Unable to read symbols from "CoreGraphics" (not yet mapped into memory).
    Program loaded.
    sharedlibrary apply-load-rules all
    Attaching to program: `/Users/Tyler/Library/Application Support/iPhone Simulator/User/Applications/A0F78B38-2BEA-4A4A-A5B7-8D1AB711409E/Marathon.app/Marathon', process 8761.
    However, if I remove the referencing outlet for the label (and change nothing else) I can switch to the AboutView and it all works fine. This would be great and all, except I can't modify the label or do anything with it since I don't have a referencing outlet to it.

    Anyone have any idea where I screwed up or how to fix this?
     
  2. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #2
    Try this:

    I assume in the AboutView.xib, you set the class of File's Owner to AboutViewController.

    But I'm guessing you forgot to do that back in the TabBarController in the MainView.xib.

    So under the TabController, if you look at the entry for the AboutViewController, you need to specify its class and its NIB to load.

    What the TabBarController will do is essentially instantiate a ViewController and then initialize it with your AboutView NIB. If I'm right, since you didn't tell the TabBarController to use your AboutViewController class, it is instead setting up a UIViewController with your AboutView NIB.

    Then when that nib is loaded, it is failing since a regular UIViewController does not have a property called "dayLabel"

    So if you set the class to AboutViewController instead, that should work.

    Hope that helps.
     
  3. Stachelsk thread starter macrumors regular

    Joined:
    Dec 17, 2008
    #3
    Ahhh, clever. It worked, thanks.

    I'm a little confused though. Am I supposed to put all of my outlets in one view controller? The Tab Bar Controller is now linked to AboutViewController... but what happens when I want to make outlets in FirstViewController...? Do I have to make one unifying view controller for this kind of Tab Bar-based project?

    Thanks again!
    - Tyler
     
  4. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #4
    Well, in your case each of the two view controllers has it own xib, which is fine.

    Each view controller will have its own outlets which you will want to set. Those outlets can be wired up in that controller's own xib.

    Maybe I didn't understand your question?
     
  5. Stachelsk thread starter macrumors regular

    Joined:
    Dec 17, 2008
    #5
    Right. Each controller has it's own outlets... buttt

    Currently, Tab Bar Controller is of type AboutViewController::UIViewController. It has to be set to AboutViewController, or the label I made a referencing outlet to causes the program to crash when you click on the 'About' tab. But I also have other view controllers (FirstViewController, etc.) that have their own referencing outlets.

    Does this make better sense: ?

    Code:
    Tab Bar Controller -- AboutViewController::UIViewController
     IBOutlet UILabel* label....
    
    AboutView.xib -- AboutViewController::UIViewController
     IBOutlet UILabel* label...
    
    FirstView.xib -- FirstViewController::UIViewController
     // IBOutlet ... *not implemented yet*
    
     // If I add an IBOutlet UILabel* here, and reference it in the FirstView
     // tab, it will cause the program to crash because Tab Bar Controller is
     // only referencing outlets from AboutViewController...
    
     // So what am I supposed to do...?
    
    Something tells me that even through the program works, I shouldn't be declaring two IBOutlets for the same UILabel object in two different classes... that would just be redundant, no? How can I fix this issue?

    Thanks again... you've been a huge help!
     
  6. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #6
    Oh no, that's not how it should be. The tab bar controller is of type UITabBarController

    A UITabBarController has a property called viewControllers. You set those in IB by basically listing them under the UITabBarController.

    Now those viewControllers each need to be of the correct types for your app. If those are just ordinary UIViewControllers, then they will not be very interesting. :)

    Again, I think you may have misunderstood the fix I proposed earlier. But on the other hand, you must have done it right or I think it wouldn't have worked.

    So maybe you didn't understand quite what you fixed?

    As I understood it, the UILabel was on the view which is managed by AboutViewController.

    Do you want FirstViewController to have access to the very same UILabel? Or its own UILabel(s) on its own view?

    np.
     
  7. Stachelsk thread starter macrumors regular

    Joined:
    Dec 17, 2008
    #7
    Yes, I changed the viewControllers property accordingly.

    I thought I changed the type of Tab Bar Controller from UITabBarController to one of my view controllers, but I must have lost my mind because interface builder says that Tab Bar Controller is indeed of type UITabBarController.

    I'm not really sure what I changed that got it to work... I thought I had everything as it was before... I might have had the viewControllers in the Tab Bar Controller setup incorrectly this whole time.

    Either way, now everything is as I expected it to be and is working!

    Thanks!
    - Tyler
     
  8. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #8
    Now would be good to take the time to understand what you did to make it work. Understanding this now will make the rest of your work with the phone much easier.

    What I suspect you did is that in your mainwindow.xib you originally had the AboutViewController (which is one of the viewControllers under the UITabBarController) defined as a UIViewController instead of an AboutViewController.

    You had correctly changed the class on File's Owner of the AboutView.xib. But you did not change it in the AboutViewController under the TabBarController back in mainwindow.xib.

    Take the time to understand why you must change it in both places and all this stuff will make much more sense.

    Good luck!
     
  9. Stachelsk thread starter macrumors regular

    Joined:
    Dec 17, 2008
    #9
    I'm almost positive that's what it was because I can't really remember changing anything else. I know I deleted all the view controllers and entered them back in manually. I think I know what I need to do for the future, but just in case I backed up this project so I can always look at it for reference. :)
     

Share This Page