24

All I need is to view a UIView controller in same storyboard file manually with code. I use storyboard to make all forms and connections. My application starts in navigation controller, which provides me access to UIView (LoginViewController) and then it goes to tab bar controller, which provides 4 UIViews. According to every UIView I have .h and .m files. I know about segue method, it is simple, but I need manual method. Maybe I am doing something wrong.

I was trying to use this method for pushing view controller in IBAction:

[self.view pushViewController:LoginViewController animated:YES];

But it makes an error:

Unexpected interface name ‘LoginViewController’: expected expression

It took a lot of time to figure out what is wrong, but I had not succeed. Here is my RollEnemyController.m file:

//  RollEnemyController.m
#import "RollEnemyController.h"
#import "LoginViewController.h"
@implementation RollEnemyController;
@synthesize AttackButtonPressed;

- (IBAction)AttackButtonPressed:(id)sender {
    LoginViewController* controller = [[LoginViewController alloc] initWithNibName:@"LoginViewController"  bundle:nil];
    [self.view pushViewController:controller];
}

@end

And this is header file:

//  RollEnemyController.h

#import <UIKit/UIKit.h>

@interface RollEnemyController : UIViewController

- (IBAction)RollButtonPressed:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *AttackButtonPressed;

@end
César
  • 9,939
  • 6
  • 53
  • 74
kokoko
  • 2,705
  • 3
  • 17
  • 28
  • It's hard to tell what is wrong with what you provided. Is it possible to provide more code? – 5StringRyan Dec 01 '11 at 20:53
  • `// RollEnemyController.m #import "RollEnemyController.h" #import "LoginViewController.h" @implementation RollEnemyController; @synthesize AttackButtonPressed; - (IBAction)AttackButtonPressed:(id)sender { LoginViewController* controller = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil]; [self.view pushViewController:controller]; } @end` – kokoko Dec 01 '11 at 21:12
  • Could you update your original post, and add this code properly formatted please? It will help others that wish to help. – 5StringRyan Dec 01 '11 at 21:13

4 Answers4

108

I'm guessing that you are using a UINavigationController. Then you can simply do like this:

LoginViewController *controller = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];

Update:

If you are using a UIStoryboard, you can set the identifier of your new viewcontroller, and then push it onto your navigationController. To set the identifier, choose your view, open the Attributes Inspector, and set the identifier ("LoginIdentifier" in my example). Then you can do this:

LoginViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"LoginIdentifier"];
[self.navigationController pushViewController:controller animated:YES];

As a sidenote, I see that you are using capital characters for your methods. You should probably try to avoid that, and instead use lowered first-characters in your method names. And since you say you are learning Objective-C, you should check out this awesome thread here on SO: link.

Update 2:

Here is a zip file with a project showing how to do this. :-)

Community
  • 1
  • 1
matsr
  • 4,302
  • 3
  • 21
  • 36
  • It works when I use `init` instead of `initWithNibName:@"LoginViewController" bundle:nil` but it creates an empty view. I need to push existing view in storyboard. Your solution causes run-time error **Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle** – kokoko Dec 01 '11 at 21:37
  • What is your .xib file named? The string passed in `initWithNibName:bundle:` is the name of your .xib file, without ".xib". Or, do you even have a .xib file for this view controller? (One that you made in Interface Builder.) – matsr Dec 01 '11 at 21:41
  • I made a mistake when said that it was XIB. I use storyboard to make all forms and connections. My application starts in navigation controller, which provides me access to UIView (LoginViewController) and then it goes to tab bar controller, which provides 4 UIViews. – kokoko Dec 01 '11 at 21:47
  • no. Nothing works. It would be great if someone create a simple project with 2 uiviews, 2 buttons and show how it works. Can you help me with this? – kokoko Dec 02 '11 at 18:10
  • @kokoko: Allright, I made a little project showing how to do it. I updated my answer with the link. It also shows how you can change to the `UITabBar`. :) – matsr Dec 02 '11 at 21:53
  • Thank you very much. I already lost my mind figuring it out. I will see that project as soon as i can and answer you. – kokoko Dec 09 '11 at 12:49
  • Thank you one more time! It works great. It's much easier to understand how it is working in correct project. – kokoko Dec 09 '11 at 14:39
  • @kokoko: That's great! Just glad I could help. :-) – matsr Dec 09 '11 at 15:52
  • @matsr : When I download your zip and run, nothing happen when I click `Swap to second view` – Fahim Parkar Dec 25 '12 at 17:58
  • @FahimParkar, that is very strange. I just re-downloaded it here and ran it on both the iOS 5- and iOS 6 simulators and it worked as expected. Did you change anything in the project before running it? – matsr Dec 26 '12 at 15:54
  • @matsr : nope.. I just unzipped and open in Xcode. I will try again and get back to you incase of any queries. However I got idea how to deal with Storyboards. – Fahim Parkar Dec 26 '12 at 16:04
  • Downloaded the sample project and works great! But where do you create instance of variable self.navigationController ??? – mahega Aug 29 '14 at 16:02
  • @mh-itc: I don't have access to Xcode for the time being and I can't remember how the project was laid out, but it might be an instance from the xib or storyboard. – matsr Aug 29 '14 at 18:46
4

hello try to use this code

Storyboard put ID = "xxx * Name Desire" mark use StoryboarID

UIStoryboard * storyboard = self.storyboard;

DetailViewController * detail = [storyboard instantiateViewControllerWithIdentifier: @ "xxx * Name Desire"];

[self.navigationController pushViewController: detail animated: YES];
Kirk
  • 16,182
  • 20
  • 80
  • 112
Danieldms
  • 1,006
  • 9
  • 11
1

In this statement:

[self.view pushViewController:LoginViewController animated:YES];

it seems you are trying to push a class. You should push an object, your actual controller:

LoginViewController* controller = [[LoginViewController alloc] init...];
[self.view pushViewController:controller animated:YES];

this will at least compile, and if all the rest is fine, also give you the second controller.

EDIT:

I missed one point. You are pushing the view controller on to a view. That makes no sense, you should push the controller on to the navigation controller:

<AppDelegate> *del = (AppDelegate*)[UIApplication sharedApplication].delegate;
[del.navigationController pushViewController:controller animated:YES];

This is true, at least, if you created your project from the Navigation-based template (which creates an application delegate with a reference to the navigation controller). Otherwise, please provide details about how you create the navigation controller.

sergio
  • 68,819
  • 11
  • 102
  • 123
  • `LoginViewController* controller = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil]; [self.view pushViewController:controller];` I have tried this, but still have and error: **Receiver type 'UIView' for instance message does not declare a method with selector 'pushViewController:'** – kokoko Dec 01 '11 at 21:08
  • 2
    `UIView` has no method called `pushViewController:animated:`. Which means you can't call it from `self.view` (which is a `UIView`). You would rather use this line: `[self presentViewController:controller animated:YES completion:nil]` – matsr Dec 01 '11 at 21:21
  • I edited my answer to provide full details... @matsr: thanks! i had not noticed that it was the view, the class thing got all my attention. – sergio Dec 01 '11 at 21:39
1

You mentioned in a comment that you're using UIStoryboard. Are you aware of UIStoryboardSegue? All you have to do it control-drag from the button to the next view controller to establish a segue. Then you can choose the type of transition. Be aware that your view controllers need to be part of a UINavigationController in the storyboard to perform a "Push" animation.

Mark Adams
  • 30,776
  • 11
  • 77
  • 77
  • Yes, I know that is very simple. But I am learning Obj-C and I was wondering how can I do it with code. – kokoko Dec 01 '11 at 21:48