28

There is probably a simple solution but I can't figure it out.

I am using storyboards for the interface.

I start with a tab bar controller, but before the user is allowed to use the app the user has to authenticate himself trough a loginview which is modally pushed at the start.

I want to configure the loginview at the same storyboard, but I can't seam to figure out how to link the view controller at the storyboard and my code.

What I have done:

  • Create a new UIViewController subclass trough file > new > new file.
  • Drag a new UIViewController in the story board
  • Set the class in the custom class tab
  • drags a UILabel for test purpose.
  • run

No label...

Justin
  • 2,960
  • 2
  • 34
  • 48

3 Answers3

40

Pull on a new UIViewController that will act as the login view controller onto the MainStoryboard. In the attribute inspector change the identifier to LoginViewController (or something appropriate). Then add

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
    [vc setModalPresentationStyle:UIModalPresentationFullScreen];

    [self presentModalViewController:vc animated:YES];
}

to the First view controller and the login screen will be loaded from your storyboard and presented.

Hope this helps.

Scott Sherwood
  • 3,108
  • 25
  • 30
  • No that's not what I meant. I have .m and .h files and I want no .xib file but configure the view in the main storyboard. – Justin Nov 04 '11 at 17:18
  • I have explained how to create the Login Screen inside the Main Storyboard so you don't need a xib. – Scott Sherwood Nov 04 '11 at 17:22
  • But that's not what I want. For example when you have a dynamic table-view you want to push an view when a cell is pressed. But how can you make that with storyboards. – Justin Nov 04 '11 at 18:51
  • 2
    I guess I don't understand the question. Here is the project that I created if you want to have a quick look and let me know if it helps or if you can clarify where I am going wrong with your question we should be able to find a solution. http://www.scott-sherwood.com/?attachment_id=512 – Scott Sherwood Nov 04 '11 at 19:46
  • Thats almost what I want. The only addition I need is the code behind for that login view. So that I can manage the login. (thanks for helping me out) – Justin Nov 05 '11 at 01:05
  • If you just want the first view controller in that storyboard (ie, the initial one) you can use `instantiateInitialViewController` instead of `instantiateViewControllerWithIdentifier:`. – Alan Zeino Feb 10 '12 at 02:14
  • but, if the login successful, how can i go back to the tab bar menu? thanks. – Alfred Angkasa Nov 05 '12 at 11:29
6

The answer by Scott Sherwood above is most correct answer I found after lot of searching. Though very slight change as per new SDK (6.1), presentModalViewController shows deprecated.

Here is very small change to above answer.

 UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
    HomeViewController * hvc = [sb instantiateViewControllerWithIdentifier:@"LoginView"];
    [hvc setModalPresentationStyle:UIModalPresentationFullScreen];
    [self presentViewController:hvc animated:YES completion:nil]; 
OSXMonk
  • 581
  • 6
  • 6
1

I'm new in this field. But if the first view controller is a navigation view controller and its rootviewcontroller is a table view controller. If you want to push a view controller like the LoginViewController when you click the cell, and you also want to go back to the table view by using the navigation bar. I recommend this way:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  {

   UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
   UIViewController *controller = [sb instantiateViewControllerWithIdentifier:@"LoginViewController"];
   [self.navigationController pushViewController:controller   animated:YES];
}

In this way, you can have the navigation.

By the way, I don't know why this kind of problem you asked will appear. I guess when the loginviewcontroller is created in the code, its view is not the view in the storyboard. If someone know the cause, please tell me! thanks!

tryKuldeepTanwar
  • 3,490
  • 2
  • 19
  • 49
Soul
  • 11
  • 2