adding Navigation Control to an Empty template project

Discussion in 'iOS Programming' started by hassoon, Feb 26, 2013.

  1. hassoon macrumors regular

    Joined:
    Jun 8, 2009
    #1
    hi, i was working with a project using the Empty Template and now i want to implement a navigation controller. first i dropped a navigation controller from the objects library but when running the app, the app doesn't view what i wanted to be views such as buttons, UImages,...etc. i feel like that i must add something in the appDelegate files but i actually don't know. Besides, ya i know i can use the master-detail template but being a developer i wanna know the basics and what's happening behind the scenes in each template. Thank you!
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    When you use the Empty Application template, you must set a UIViewController or, normally, a subclass of it as your window's rootViewController in the appDelegate. UINavigationController is a subclass of UIViewController.
     
  3. hassoon thread starter macrumors regular

    Joined:
    Jun 8, 2009
    #3
    i wrote the following in the application:didFinishLunchingWithOptions:
    Code:
    SSPHomeScreenViewController *myViewController = [[SSPHomeScreenViewController alloc] init];
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
        self.window.rootViewController = navigationController;
    
    and it's still not working.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Not working how? Please elaborate. What were you expecting to happen and what happened instead? Did you get any warnings/errors/crashes? Etc.
     
  5. hassoon thread starter macrumors regular

    Joined:
    Jun 8, 2009
    #5
    i mean that the simulator is showing a blank white page. i'm expecting to show me a navigation controller base view with a bar (besides the buttons that i added). To best understand my problem just create a an empty project in xcode and once created add a navigation controller object from the library and run the app. an error will tell you that the application window is expecting a view controller. so i'm sure that there's something to add in order to hook the navigation controller to the root controller but i don't know what it is. Thank you so much for the help you're giving me.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    From the library? What library? Add the navigation controller object to what? All you have at this point is a AppDelegate class. Maybe you should provide some screenshots or better details of what exactly you are doing in order for us to better understand.
     
  7. hassoon thread starter macrumors regular

    Joined:
    Jun 8, 2009
    #7
    oh, i mean the object library to the right under the inspector. and add the navigation controller to the storyboard. anyway, here are the screenshots and a briefer explanation.
    I created a project on xcode with an Empty project template. i added a navigation controller object from the object library to the story board. i ran the app and a white blank view appeared only. i was expecting a view with a top bar for navigation (at least). besides an error occurred that the application window is expecting a root controller. Problem: i don't know how to hook up the navigation controller object in the storyboard to the app root controller so that when i run the app the navigation controller and bar will appear. the attached pictures shows my set up (didn't add any code yet).
     

    Attached Files:

  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Do you mean the Empty Application template? As seen in the screenshot below? If so, that doesn't come with any storyboards, so I'm confused.
     

    Attached Files:

  9. hassoon thread starter macrumors regular

    Joined:
    Jun 8, 2009
    #9
    ya exactly, i know it doesn't but you can add one.
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Well, that's kind of an important step that you failed to mention previously. Anyways, just because you add a storyboard to an Empty Application template does not mean the application will use it. You would still need to set it as the main storyboard for your target.
     
  11. hassoon thread starter macrumors regular

    Joined:
    Jun 8, 2009
    #11
    I did and the problem persist.. Did it work to you?
     
  12. dejo, Feb 26, 2013
    Last edited: Feb 26, 2013

    dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    It did. But I removed some code from application:didFinishLaunchingWithOptions:. Can you guess what?
     
  13. hassoon thread starter macrumors regular

    Joined:
    Jun 8, 2009
    #13
    i tried deleting with all the possible combinations and it's not working. did you add anything beside deleting ?
     
  14. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #14
    Nope. Only deleted code. Think about what happens when you set the Main Storyboard for your target and how that might conflict with the template code that resides in application:didFinishLaunchingWithOptions:.
     
  15. hassoon thread starter macrumors regular

    Joined:
    Jun 8, 2009
    #15
    mmm... sounds like a riddle. can i take a look of the template code or the automatic code that is generated for any template chosen?
     
  16. dejo, Feb 27, 2013
    Last edited: Feb 27, 2013

    dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #16
    Why not just create a stub-project using the template you're curious about?
     
  17. hassoon thread starter macrumors regular

    Joined:
    Jun 8, 2009
    #17
    I have two reasons why I don't want to use another template:
    1. I want learn the basics of navigation controllers.
    2. If I choose single view template I will not be able to use core data (automatic implementation) since I'm a Beginner. However I can choose a master-detail template and then create my own classes and stuff but I will also face the obstacle of implementing core data methods and stuff.

    If you're willing to help me please do, it seems you got the answer.
     
  18. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #18
    Perhaps you misunderstood. (Or I'm confused.) You asked "can i take a look of the template code or the automatic code that is generated for any template chosen?" I gave you a way: create a stub-project based on that template. Before you change anything, look at the code it created.

    Anyways, back to the Empty Application project. Initially, it should contain the following:
    Code:
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
        return YES;
    }
    
    Then read what the View Controller Programming Guide says happens if you define a main storyboard for your project. How does the code from the template method conflict with what iOS is doing in regards to the main storyboard?

    I am willing to help, but I'm not just going to give you the solution. I find that if I discover the answer through my own problem-solving (and perhaps a few hints), then that "eureka" moment causes me to better recall the solution later on than if someone else just gave me the code. So that's how I try to help others, as well.
     

Share This Page