Chance
Chance

Reputation: 33

Objective-C: Unable to instantiate a class to create a UINavigationController subview

I'm attempting to create (in XCode) a Tabbed Application, which houses in one of its tabs a UINavigationController, which is used to show a "FourthViewController", and then when a button is pressed within that "FourthViewController", a new "SlideViewController" is pushed onto the UINavigationController (phew!). I'm having problems understanding how to make the UINavigationController push a new ViewController from inside FourthViewController or SlideViewController. I'm including a flowchart and some code to try to make it more clear what I'm doing. (And for the record, it's an app for a restaurant, which is why there are names like "shrimpquesadilla.jpg".)

The following is a link to the flowchart, I don't have enough reputation to embed it in the post: https://i.sstatic.net/tjTT1.jpg

DemoTabbedAppDelegate.h

    @interface DemoTabbedAppDelegate : UIResponder <UIApplicationDelegate,
                                     UITabBarControllerDelegate>
    {
        UINavigationController *globalUINavigationController;
    }
    ... //other syntheses
    @synthesize globalUINavigationController;

DemoTabbedAppDelegate.m

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
                    (NSDictionary *)LaunchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        ... /creating the first three tab ViewControllers
        globalUINavigationController = [[UINavigationController alloc] initWithNibName:
                    @"DemoTabbedFourthViewController" bundle:nil];
        ... //setting last tab title and image
        UIViewController *viewController4 = [[DemoTabbedFourthViewController alloc]
                    initWithNibName:@"DemoTabbedFourthViewController" bundle:nil];
        [globalUINavigationController pushViewController:viewController4 animated:YES];
        ... //Starting the TabBarController and making it the rootViewController
    }

DemoTabbedFourthView.m

    //initialization and all that junk
    - (void)goToSlide:(UIImage *)image
    {
        DemoTabbedSlideViewController *d = [[DemoTabbedSlideViewController alloc]
                                            initWithImage:image];
        [globalUINavigationController pushViewController:d animated:YES];
        NSLog(@"test");
        //what goes here to call push on the UINavigationController?
    }

    - (void)clickQuesadilla:(id)sender
    {
        [self goToSlide:[UIImage imageNamed:@"shrimpquesadilla.jpg"]];
    }

DemoTabbedSlideViewController

    - (id)initWithImage:(UIImage *)image
    {
        self = [super init];
        imageView = [[UIImageView alloc] initWithImage:image]; 
        NSLog(@"test 2");
        return self;
    }

Please let me know if you need more information, I've tried to be as clear about this as possible. Thank you for your time.

Upvotes: 0

Views: 175

Answers (2)

Pedro Cattori
Pedro Cattori

Reputation: 2805

I think the problem might be with how you are initializing your navigation controller. When you instantiate your navigation controller, try using initWithRootViewController: instead of initWithNibName:.

Also, from inside your DemoTabbedFourthView's .m, you can get a pointer to the navigation controller it is in by accessing its navigationController property and then calling pushViewController:animated: from that:

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

Upvotes: 2

Kaelin Colclasure
Kaelin Colclasure

Reputation: 3975

This type of application is practically a poster child for Storyboards. Is there some reason you prefer not to use them?

You could easily lay out a Storyboard with Segues between all of these views, and let the frameworks handle all of this navigation between views for you.

Upvotes: 0

Related Questions