0
votes

I went through all similar questions on stackoverflow, but questions are quite stale. Since then Xcode has been updated multiple times, and the answers are not working for me.

I am extremely new to iOS development. I just want to open a new view controller once the user is logged in.

I am only using storyboards. I don't have .xib file, since I followed Apple's iOS tutorial.

  1. Created a single view project. Removed original view controller. (Changed this to LoginViewController, was causing problems.)

  2. Added two new view controllers: LoginViewController and MainMenuViewController. Both have navigation controllers embedded. I also set LoginViewController as the initial view controller of the storyboard.

  3. According to this post added the following code:

(Navigation Controller Push View Controller)

In LoginViewController.m:

- (IBAction)GoToMainMenu:(id)sender 
{
    MainMenuViewController* controller = [[MainMenuViewController alloc] init];
    [self.navigationController pushViewController:controller animated:YES];
}

In AppDelegate.m:

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

    self.viewController = [[ViewController alloc] initWithNibName:@"LoginViewController"
                                                           bundle:nil];
    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.window.rootViewController = navigation;
    [self.window makeKeyAndVisible];
    return YES;
}

In the above code, it said:

self.viewController is not found on object of type 'AppDelegate'

My question is how to accomplish this task using Xcode 6.0.1 and storyboards?

2
Did you declare @property (strong) UIViewController *viewController; in your @interface? - Ian MacDonald
No. Just when user is successfully logged in, i'll present new viewcontroller (which is mainmenu). If user typed in wrong input, it stays in loginviewcontroller. - Emma
No, where should I place it. In my case, is it @property (strong) UIViewController *LoginviewController;? - Emma
You're trying to access it as a property of the class @implementation that you've written from appdelegate.m. I would guess the @interface for this class is in a file called appdelegate.h? Put it between the @interface .. and @end lines somewhere. - Ian MacDonald

2 Answers

1
votes

Found many problems in your code:

1) There is absolutely no need of writing following code in your application didFinishLaunchingWithOptions when your using the Storyboard. This code is being written to embed the UINavigationController to ViewController, when you want to load .xib file and make your viewController as initial viewcontroller and when you dont want to use storyboard as storyboard will be taken as default.

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

self.viewController = [[ViewController alloc] initWithNibName:@"LoginViewController"
                                                   bundle:nil];
UINavigationController *navigation = [[UINavigationController  
alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = navigation;
[self.window makeKeyAndVisible];
return YES;

Your are loading .xib file with the above code which is not there. Remove the above code and setup the storyboard as you are using storyboard for this.

To push the viewcontroller:

In your case, take a button. Give its -IBAction like this in your LoginViewController.h file:

- (IBAction)GoToMainMenu:(id)sender; 

Now in your LoginViewController.m file implement this method:

- (IBAction)GoToMainMenu:(id)sender
{

    if(check login success)

    {
         [self performSegueWithIdentifier:@"loginMainSegue" sender:self];
    }

    else
    {
        You can set Alert here saying invalid login.
    }
} 

Follow this SIMPLE STORYBOARD TUTORIAL and you will be through. The only difference will be of Xcode Version and segues used. You should use the segues of Xcode6 thats it and no other difference.

0
votes

In your AppDelegate.h file, you need to declare the UIViewController *viewController. The following is an example of how your AppDelegate.h file should look.

#import <UIKit/UIKit.h> 

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window; 
@property (strong) UIViewController *viewController;

@end

In your AppDelegate.m file, you need to add the following line @synthesize viewController right below where it says @implementation.

After this, the code should work.