0

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?

Community
  • 1
  • 1
Emma
  • 60
  • 10
  • Did you declare `@property (strong) UIViewController *viewController;` in your `@interface`? – Ian MacDonald Oct 17 '14 at 18:33
  • 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 Oct 17 '14 at 18:35
  • No, where should I place it. In my case, is it `@property (strong) UIViewController *LoginviewController;`? – Emma Oct 17 '14 at 18:38
  • 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 Oct 17 '14 at 18:48

2 Answers2

1

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.

Rumin
  • 3,787
  • 3
  • 27
  • 30
  • Ok, now it can compile but once submit, it goes to viewcontroller with black background and navigation controller on it points to login page. It is not connected to my main menu viewcontroller. Currently my storyboard contains 2 viewcontrollers with navigation controller,but NOT connected to each other. Do i need to create segue to connect them? – Emma Oct 17 '14 at 19:00
  • exactly. Drag a segue from button to MenuViewController. BE CAREFULL, DONT USE DEPRECATED SEGUES. – Rumin Oct 17 '14 at 19:02
  • Drag to MainMenuViewController or it's navigationbar? I created modal segue.The problem is now it is going to mainmenucontroller no matter user input is right or wrong. And everytime i compile, it is going to blank page with black background, then i need to click navigation button to go back to login viewcontroller. – Emma Oct 17 '14 at 19:09
  • Drag To MainViewController. Give segue a identifier:"loginMainSegue" (it can be anything you want) in storyboard. N sorry I made a mistake. check my - (IBAction)GoToMainMenu:(id)sender method again in answer. You need to implement performSegueWithIdentifier method in storyboards. – Rumin Oct 17 '14 at 19:14
  • Still some issue. I created `- (IBAction)makeConn` and connected this function with my submit button on the storyboard. Then i called this function in `- (void)viewDidLoad` as `[self makeConn]`. So basically my function does not have `(id) sender`. Is it causing problem? – Emma Oct 17 '14 at 19:29
  • You can't call the IBAction of the button as [self method_name] anywhere. This method will be automatically implemented on button click event. Also follow the LINK i have mentioned in the answer. It is a very simple storyboard tutorial. Refer it and you will have an idea. – Rumin Oct 17 '14 at 19:31
0

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.

Rumin
  • 3,787
  • 3
  • 27
  • 30
davetw12
  • 1,815
  • 2
  • 20
  • 27
  • I added like this `@interface AppDelegate () @property (strong) UIViewController *viewController; @end` Otherwise, with `UIResponder `, it said _Duplicate interface definition for class 'AppDelegate'_ – Emma Oct 17 '14 at 18:47
  • Could you check whether i am using right viewcontroller name on the right method, i feel like messed up on names. With all these codes added, it won't even compile. – Emma Oct 17 '14 at 18:48
  • Don't create a category. Modify the root `@interface`. – Ian MacDonald Oct 17 '14 at 18:49
  • Ok, I added to into appdelegate.h. I think the name should be `*LoginViewController`, right? instead of `viewController`. Since I found the code someone's questions. – Emma Oct 17 '14 at 18:54
  • I don't understand why the vote down. To make this simple, just replace your AppDelegate.h code with what I have here. Then your app should compile. – davetw12 Oct 17 '14 at 18:56
  • Whatever name you choose is fine. What matters is that you stick to that name in both your `AppDelegate.h` and `AppDelegate.m` files. – davetw12 Oct 17 '14 at 18:58