This really should work. This is one of the simplest things to achieve in iOS and for some reason it's just not working.
I have two view controllers in my storyboard. One is InitViewController and the other is ViewController, with a storyboard ID of Init and ViewOne respectively. I have a button on InitViewController that is running code to switch the views. The code is running properly but nothing happens despite that fact. Here is the code:
-(IBAction)NextPage:(id)sender{
ViewController *wc = [[ViewController alloc]
initWithNibName:#"ViewOne"
bundle:nil];
[self.navigationController pushViewController:wc animated:YES];
}
I imported ViewController.h, I just don't know why this isn't working.
initWithNibName: is for view controllers that you create in .xib files, not storyboards. To create a view controller from a storyboard use -[UIStoryboard instantiateViewControllerWithIdentifier:]
ViewController *wc = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewOne"];
[self.navigationController pushViewController:wc animated:YES];
You need a NavigationController - try to log self.navigationcontroller and you will see is null
In your storyboard add a UINavigationController set it as entrypoint, set InitViewController as root view controller and then you can use:
ViewController *wc = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewOne"];
[self.navigationController pushViewController:wc animated:YES];
Related
I got a source code for an existing app. throughout the app the developer didn't use storyboard for the user interface but rather he used interface builder xib files. As a new developer, i don't know how to develop iOS apps in xcode using xib files for viewcontrollers, i learnt the storyboard way. Now I want to add more screens to the apps source code, i created a new storyboard file to define my new screens, how do I navigate to the entry point of this new storyboard from a button action i defined in one of the xib files so that i can easily implement my features.
while searching i got some suggestions like this one
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"newStoryboard_iPhone" bundle:nil];
MyNewViewController *myVC = (MyNewViewController *)[storyboard instantiateViewControllerWithIdentifier:#"myViewCont"];
but i dont think i understand how to implement this even if this can be in a button IBAction method. and besides subsequent navigations would be segues through the view controllers i define in the new storyboard
It's right suggestion.
You should get storyboard object by its name. Then you should create your viewController using [storyboard instantiateViewControllerWithIdentifier:#"..."];
Then you may navigate to your new VC using appropriate navigation method. For example:
[self.navigationController pushViewController:vc animated:YES]
ViewController identifier you should set in Interface Builder under section "Identity Inspector" in field "Storyboard ID".
That code you should place in IBAction method. But better would be to extract it in separate method. Something like this:
- (IBAction)buttonPressed:(UIButton *)sender {
[self navigateToMyNewViewController];
}
- (void)navigateToMyNewViewController {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"myNewStoryboard" bundle:nil];
MyNewViewController *myNewVC = (MyNewViewController *)[storyboard instantiateViewControllerWithIdentifier:#"MyNewViewController"];
[self.navigationController pushViewController:myNewVC animated:YES];
}
Navigation from MyNewViewController your can implement as you want. It may be segues.
MyNewViewController *myVC = [self.storyboard instantiateViewControllerWithIdentifier:#"YOUR_VC_NAME_HERE"];
This will instantiate your ViewController, from here you can present it like so
[self presentViewController:myVC animated:YES completion:nil]
The image shows where you set the storyboard name that you're referring to
Try This it's working for me ,
Use below line of code :
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main"bundle:nil];
LoginViewController *loginVC = (LoginViewController*)[storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self.navigationController pushViewController:loginVC animated:YES];
We need to set storyboard id as per mention in below,
you can navigate one storyboard to another storybord:
first you import your viewcontroller class to appdelegate.h
AppDelegate *appDelegateTemp = [[UIApplication sharedApplication]delegate];
appDelegateTemp.window.rootViewController = [[UIStoryboard storyboardWithName:#"storyboard_name" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
Like the way you use storyboard, use
MainViewController *mainVC = [[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil]
[self.navigationController pushViewController:mainVC animated:YES];
The code about present another independent storyboard as follows:
- (IBAction)action:(id)sender {
UIStoryboard *secondStoryboard = [UIStoryboard storyboardWithName:#"SecondStoryboard" bundle:nil];
UIViewController *firstVC = [secondStoryboard instantiateViewControllerWithIdentifier:#"22"];
[self.navigationController presentViewController:firstVC animated:YES completion:nil];
}
If my storyboard like follow contain a UITabBarController the Pesentbutton with function -(IBAction)action:(id)sender; can't present another independent storyboard.
If my storyboard like follow contain a UINavigationController the Pesentbutton with function -(IBAction)action:(id)sender; presents another independent storyboard as expected.
Who can tell me the reason?More thanks!
Because in the first case self.navigationController is nil... Since no navigation controller is present. Try presenting from self or self.tabBarController
How can I initiate segue from a custom UIView or custom UIViewController that is not on Storyboard? - they are created programmatically inside a parent UIViewController.
Although the destination UIViewController is on the storyboard.
Then just do it the old fashioned way. Instantiate it and present it modally or just push it if you are in a navigation controller. Hope this helps.
EDIT:
You can talk about segue only if it is in the storyboard. If Your source View Controller is not in it, you just present the next one as I said. You can instantiate your destination view controller from the storyboard:
MyViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"MyViewController"];
[self presentViewController:controller animated:YES];
From this point you will be "back in the storyboard" so you can perform segues in the destination View Controller.
First give storyboard id. Select the view controller than write a identifier(Storyboard ID)
Secondly, just open your .m file then write belove codes.
UIStoryboard * st = [UIStoryboard storyboardWithName:#"nameOfStoryboard" bundle:nil];
MyViewController *viewController = [st instantiateViewControllerWithIdentifier:#"YourStoryboardID"];
[self.navigationController pushViewController:viewController animated:YES];
Last line of code. Might also be
[self presentModalViewController:viewController animated:YES];
This question might have been answered, if yes, please share the link.
I have created a Single View Application, It works fine, but now I have added a new view and on a button click, wants the new view to appear.
This is the code for click action,
SettingsViewController *settingsViewController = [[SettingsViewController alloc] initWithNibName:#"SettingsViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:settingsViewController animated:YES];
The Default ViewController now looks like this in .h file
#interface ViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
The SettingsViewController.m has a default
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{}
Can I add another view to "Single View Application" like this or should I chose another template for my project ?
Thanks.
You need to create a UINavigationController in your AppDelegate. Then make your ViewController the rootViewController of the UINavigationController. Then you will be able to push and pop views.
Here is the code to create the rootViewController where mainNavigationController is the UINavigationController in your AppDelegate:
ViewController *vc = [[ViewController alloc] init];
mainNavigationController = [[UINavigationController alloc] initWithRootViewController:vc];
Once you have the ViewController set up as the rootViewController it will conform to the UINavigationController push and pop methods to create a stack of UIViewControllers.
That is fine. The single view application template is just a barebones template. You can add any type of navigation you like to it.
In iOS 5, switching between views works a bit different i think,
I have created a few apps with the above mentioned code for switching views.
But now, I have to write it like this to work:
SettingsViewController *settingsViewController = [[SettingsViewController alloc] initWithNibName:#"SettingsViewController" bundle:[NSBundle mainBundle]];
[self presentModalViewController:settingsViewController animated:YES];
If I have a navigationController which is init with a root view controller MyViewController's instance.
And in that MyViewController's code
I can use
AnotherViewController *vc = [[AnotherViewController alloc] init];
[self presentModalViewController:vc animated:YES];
or
AnotherViewController *vc = [[AnotherViewController alloc] init];
[self.navigationController presentModalViewController:vc animated:YES];
I found these two works the same. Both present the modal view correctly.And I have found that the presented AnotherViewController's "parentViewController" property are all set to the navigation controller.
Why would this happen?the presentModalViewController automatically detect that the self is the subview of the navigation controller and re send the message to navigation controller?
Because MyViewController is the root view controller of the UINavigationController, it gets the convenience method of presentModalViewController: animated: by default. So when you say self.navigationController, it is referring to the same navigationController that presentModalViewController gives you. I think Apple is just trying to make it more intuitive to use the convenience method.