Search code examples
iosiphoneobjective-cuistoryboardsegue

uinavigationcontroller bar button item not responding


I am a beginner iPhone developer so I apologise if there is a lack of information here. I have created a storyboard which has an initial view controller (for app login) and if I detect that there is already a user logged in (I have an API token for a user stored already) then I will load a navigation controller instead which has a root view controller defined which is a table view controller. Within the table view controller I have a navigation item which contains left and right bar button items. I added another view controller with a label on, then I ctrl dragged from the right navigation bar button item to the simple view controller to create a segue. This segue is the one which does not work when I build and run the app. I also tried to create an IBAction and hook that up to the right bar button item and NSLog "Hello world" but that doesn't work. Here is how I am initialising the storyboard:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Instantiate the UIStoryBoard
    UIStoryboard *initiialStoryBoard = [UIStoryboard storyboardWithName:@"iPhoneStoryboard" bundle:nil];

    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

    // Set the first view controller of the storyboard as the root controller.
    User *user = [[User alloc] init];

    if ([user token] == nil) {
        [self.window setRootViewController: [initiialStoryBoard instantiateInitialViewController]];
    } else {
        UINavigationController *feedNavigationController = [initiialStoryBoard instantiateViewControllerWithIdentifier:@"feedNavigationController"];
        [self.window setRootViewController:feedNavigationController];
    }

    [self.window makeKeyAndVisible];

}

Can anyone suggest what I could be doing wrong?


Solution

  • First, remove all your code within the method application:didFinishLaunchingWithOptions: and just return YES;.

    Create your storyboard like in the image below.

    1. Use a UINavigationController as your entry point.

    2. Set your authentication view controller as the rootViewController of the UINavigationController

    3. Use another segue from your authentication view controller to your feed view controller and give it the segue identifier segueFeed

    Controllers and Segues in Storyboard

    After that add the following code to your authentication view controller:

    - (void) awakeFromNib
    {
        // Set the first view controller of the storyboard as the root controller.
        User *user = [[User alloc] init];
    
        if ([user token] == nil)
        {
            // do nothing because the correct view controller will be shown
        }
        else
        {
            // show the feed view controller
            [self performSegueWithIdentifier:@"segueFeed" sender:nil];
        }
    }
    

    Now the authentication view controller will be shown automatically if no token is given and otherwise the feed view controller will be shown immediately. At this point you can add your own navigation items (like Action 1 and Action 2) and connect them with other view controllers via segues.