UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard"
bundle: nil];
MenuScreenViewController *controller = (MenuScreenViewController*)[mainStoryboard
instantiateViewControllerWithIdentifier: #"<Controller ID>"];
Where exactly do i write this code if i have to make sure that the current view is instantiated with the identifier? Which means if i write any code on this class it has to appear when this viewcontroller loads? Also how would i use it? I dont want to create an instance of the menuscreenviewcontroller. WHich means i have to say self but i used self.view and that doesnt work.
You need to push or present the view controller that you have created. You can not directly change views of the controllers by instantiating.
For example you need to use this code to trigger the transition (maybe a button action):
MenuScreenViewController* controller = (MenuScreenViewController*)[ourStoryBoard instantiateViewControllerWithIdentifier:#"<Controller ID>"];
controller.controlFlag = YES;
controller.controlFlag2 = NO; // Just examples
//These flags will be set before the viewDidLoad of MenuScreenViewController
//Therefore any code you write before pushing or presenting the view will be present after
[self.navigationController pushViewController:controller animated:YES];
// or [self presentViewController:controller animated:YES];
As per Uğur Kumru's answer, with a small edit: if you are not using a Navigation Controller, and you are developing against iOS 5.0+ you will need to use:
MenuScreenViewController* controller = (MenuScreenViewController*)[ourStoryBoard instantiateViewControllerWithIdentifier:#"<Controller ID>"];
[self presentViewController:controller animated:YES completion:nil];
If you omit the completion:nil you will face errors
Related
I added one ViewController to my project and created one class.
After I bind this class to my ViewController.
In another controller I have method:
- (IBAction)login:(id)sender {
// How here I can do redirect to controllerViewController
}
There are two ways to push view controllers in the application.
1) by Segue
2) By View controller identifier
1) By Segue :
[self performSegueWithIdentifier:"SegueIdentifier" sender:self];
2) By View controller identifier :
Yourclassname *gotoYourClass = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerIdentifier"];
[self.navigationController pushViewController:gotoYourClass animated:YES];
- (IBAction)login:(id)sender {
ViewController *vc = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
[self presentViewController:vc animated:YES completion:nil]; }
In the storyboard give your view controller an identifier (under the Attributes Inspector) then use the following code, to bring that view forward.
IF YOU WANT TO USE PUSH THEN USE THIS CODE
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"STORYBOARDNAME" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"VIEWCONTROLLERIDENTIFIER"];
[self.navigationController pushViewController:vc animeted:YES];
IF YOU WANT TO PRESENT THEN USE THIS CODE
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"STORYBOARDNAME" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"VIEWCONTROLLERIDENTIFIER"];
[self presentModalViewController:vc animated:YES];
Note: In UIViewController please enter your view controller name which you want to push into another view.
If you want to comeback to the current ViewController later then use Navigation ViewController or else just use the presentedViewController of self(your current viewController)- no going back business, like they have previously illustrated.
For a simple block of execution(demo or practice) it's all the same but for a project application it completely matters to make the correct choice.
I'm new on IOS platform and after study a little about how does it works, i had one doubt about how to call a new class/view and overlay the current view when a button is pressed. In android i do:
Intent intent = new Intent(a.class, b.class);
startActivity(intent);
Searching on internet, i noticed that i have to use navigation bars to do it. I start an app with tab bar controller and putted a navigation controller. I used the code below:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
UIViewController *myController = [storyboard instantiateViewControllerWithIdentifier:#"Dicas"];
[self.navigationController pushViewController: myController animated:YES];
and the return:
There is a way to overlay the current view? I always have to use navigation bars to call another class (use bottom e upper controllers will make my app ugly)?
To hide the navigation bar:
[[self navigationController] setNavigationBarHidden:YES animated:YES];
To hide tab bar:
yourViewController.hidesBottomBarWhenPushed = YES;
You can also call another viewController by using presentViewController function of a viewController class.
[self presentViewController: myController animated:YES completion:nil];
As you said u want to overlay your current view so not sure but You can show your view controller using model view like this
yourViewController *secView = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
[secView setModalPresentationStyle:UIModalPresentationPageSheet];//u can use different UIModalPresentationStyle
[secView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self.navigationController presentViewController:secView animated:YES completion:nil];
Above one will show your view controller over existing view controller, once presented you need to make provision to dismiss this modal view.
Simple way to present new class or ViewController like this..
ViewController *view = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewController"];
[self presentViewController:view animated:YES completion:nil];
I am using storyboards, I have one viewcontroller and on click I need to show another view controller modally. I am trying using this code
[self presentViewController:zoomV animated:YES completion:NULL];
I am coming up with a blank screen.
This is how I create
zViewController *zoomV = [[zViewController alloc] init];
[self presentViewController:zoomV animated:YES completion:NULL];
I tried researching this and some answers revolve around using storyboards and not having a rootviewcontroller associated. So what I have is in the initial scene I have a navigationController, and from there I drag to another Viewcontroller a relationship which defines it as a rootViewcontroller. Is that sufficient ? or is this irrelevant?
Since you have your zViewController in your storyboard, you should instantiate your zViewController using UIStoryboard instantiateViewControllerWithIdentifier:.
In your first view controller, instead of creating the zViewController using alloc/init do this, of course setting an identifier for your zViewController in your storyboard.
zViewController *zoomV = [self.storyboard instantiateViewControllerWithIdentifier:#"yourIdentifier"];
[self presentViewController:zoomV
animated:YES
completion:NULL];
Also you could accomplish the same using a segue and executing it directly, without the need of instantiating the zViewController, but is up to you.
As a second(small) comment, do not name classes starting with lowercase in ObjC :).
You may refer below snippet for story board:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
AddNameViewController *sfvc = [storyboard instantiateViewControllerWithIdentifier:#"AddNameViewController.m"];
[sfvc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:sfvc animated:YES completion:nil];
I am trying to present a view controller modally from another view controller that is the second view inside UINavigationController.
But it keep giving error and crashing. Application tried to present modally an active controller
I dont know what does it mean. Following is the way how i am calling.
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
FBPreviewPostViewController *previewPostViewController = [storyBoard instantiateViewControllerWithIdentifier:#"FBPreviewPostViewController"];
previewPostViewController.delegate = self;
previewPostViewController.selectedImg = selectedImage;
[self presentViewController:previewPostViewController animated:YES completion:nil];
Thanks
The error indicates that the view controller is presented twice, which is not allowed.
I don't know much about Storyboards and your app, perhaps FBPreviewPostViewController has been presented by storyboards. You can check whether it is in view hierarchy by checking viewController.isViewLoaded && viewController.view.window is true or not.
BTW, you are using storyboards, why don't use segue?
Can you try following code
NSString *stbName = [[NSBundle mainBundle].infoDictionary objectForKey:#"MainStoryboard_iPhone"];
UIStoryboard *Mainstoryboard = [UIStoryboard storyboardWithName:stbName bundle:nil];
FBPreviewPostViewController *previewPostViewController = [Mainstoryboard instantiateViewControllerWithIdentifier:#"FBPreviewPostViewController"];
previewPostViewController.delegate = self;
previewPostViewController.selectedImg = selectedImage;
[self presentModalViewController:previewPostViewController animated:YES];
How can you push to another view controller without prepareForSegue?
myClassVC *viewController = [myClassVC alloc];
UIStoryboardSegue *segue = [[UIStoryboardSegue alloc] initWithIdentifier:#"pushToMyVC" source:self destination:viewController];
if ([segue.identifier isEqualToString:#"pushToMyVC"]) {
NSLog(#"logging");
myClassVC *viewController = (myClassVC *)[segue destinationViewController];
[self presentViewController:viewController animated:YES completion:nil];
}
If you want to programmatically invoke a push segue, you give the segue a "storyboard id" in Interface Builder and then you can:
[self performSegueWithIdentifier:"pushToMyVC" sender:self];
Alternatively, if you don't want to perform the segue, you can instantiate the destination view controller and then manually push to that view controller. All you need to do is to make sure that the destination view controller has its own "storyboard id" in Interface Builder, then you can:
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"DestinationController"];
[self.navigationController pushViewController:controller animated:YES];
You said "push" (and hence I used pushViewController above). If you really meant to "present a modal view controller", then that second line is:
[self presentViewController:controller animated:YES completion:nil];
As you can see, you don't have to use prepareForSegue to push to new scene. You only use prepareForSegue if you want to pass information to the destination view controller. Otherwise it is not needed.
Clearly, if you're not using storyboards (e.g., you're using NIBs), then the process is entirely different. But I assume you're not using NIBs because prepareForSegue is not applicable in that environment. But if you were using NIB, it would be as follows:
SecondViewController *controller = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[self presentViewController:viewController animated:YES completion:nil]; is not needed, as the segue will push the destination view controller using the transition you selected automatically.
If you don't want to use the segue process you'll need to manually push the view controller with:
[self presentViewController:viewController animated:YES completion:nil];
but make sure you remove the segues in the Storyboard first.
NSString * storyboardName = #"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"SecondViewControllerID"];
[self presentViewController:vc animated:YES completion:nil];
In my case all viewControllers are built dynamically. I do not make use of the storyboard for this purpose. Should this be your case you may just instantiate the viewController class you want to open and make a push as in my example below:
MyViewController* myViewController = [[MyViewController alloc] init];
[self.navigationController pushViewController:myViewController animated:YES];