I have a sidebar menu, made with SWRevealViewController.
I want to switch from the view controller and the view with the button to the next view controller. How I do that?
I try to used the ACTION SEGUE: "push", "modal", "revealview controller" and "revealview controller push controller".
But it doesn't works.
And I try it programmatically switch to the next viewcontroller.
MY CODE:
ViewController2 *vc2 = [[ViewController2 alloc] init];
[self.navigationcontroller pushViewController:vc2 animated:YES]
This change the ViewController, but without the view (blackscreen).
It seems ViewController2 is a ViewController in storyboard. Try the below code:
ViewController2 * vc2 = [[self storyboard] instantiateViewControllerWithIdentifier:#"ViewControllerId"];
You should give Storyboard ID for ViewController2 as ViewControllerId in your storyboard.
Please note that your current ViewController should be in a Navigationcontroller stack to call self.navigationcontroller.
Related
I've two ViewControllers in Main.Storyboard, entry point ViewController have green background, and another is red background. At the button click of first ViewController it should show second ViewController, but the first and second ViewControllers are overlapping each other and both ViewControllers Buttons are clickable.
Ist view controller (green), bingo is the UIButton
2nd view controller (red), hello is the UIButton
On the click event of the button in first view controller is :
MViewController *mvc = [self.storyboard instantiateViewControllerWithIdentifier:#"mvc"];
[self presentViewController:mvc animated:YES completion:nil];
Try this if its a push segue :
MViewController *mvc = [self.storyboard instantiateViewControllerWithIdentifier:#"mvc"];
[self.navigationController pushViewController:mvc animated:YES];
And you should take one navigation controller and assign it as initial view and make green view the root view. Delete the default root view controller comes with it.
You can write this code in the action block of bingo button
{
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let redVC = storyBoard.instantiateViewControllerWithIdentifier("**RedViewControllerSID**") as RedViewController
self.presentViewController(redVC, animated:true, completion:nil)
}
go storyboard and create Storyboard ID as RedViewControllerSID
Lets say I am in ViewController1 and I want to load ViewController2. The only way I know to accomplish this is using a button in ViewController1, and connecting to ViewController2 in the storyboard but I do not want to do it that way. I want to do in a method in MyViewController1. For example, in my viewDidLoad method.
Any help is greatly appreciated. Thanks
Create a segue from vc1 to vc2 inside the storyboard, give it a name and call:
[self performSegueWithIdentifier: #"mySegueCustomName" sender: self];
If you are trying to embed the view managed by ViewController2 into the view managed by ViewController1, you can do this by adding a UIContainerView to ViewController1 with an embed segue pointing to ViewController2.
This is useful, for example, if you have a reusable view managed by ViewController2 and want to use that in multiple places.
You can do this in InterfaceBuilder by dragging a UIContainerView from the Object library onto the view for ViewController1. You can then change the class of the view controller it creates to ViewController2.
Do not confuse Views and ViewControllers. You post title suggests you do not have it ingrained yet fully.
If you want to present another view controller programatically, then simply create an action method for the button and do it there. ViewDidLoad is absolutely NOT fit for that purpose.
You can present a view controller included in the storyboard like this:
-(IBAction)goToDetails:(id)sender
{
//identify the correct storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"storyboard's name" bundle:nil];
//extract a view controller instance from the storyboard
DetailViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:#"details view controller's identifier"];
//present the view controller instance
[self presentViewController:detailViewController
animated:YES
completion:nil];
}
And here's an example to go a to an unrelated non-storyboard view controller.
-(IBAction)loginToSystem:(id)sender
{
LoginViewController *loginViewController = [[LoginViewController alloc] init];
[self presentViewController:loginViewController
animated:YES
completion:nil];
}
In my program have 6 view controllers.(in storyboard)
Lets define 1,2,3,4,5,6.
1 is my main view.
I want to navigate like this(image bellow).Is it possible to do?
give me a idea to do this navigation.
Yes, u can implement this. This is a simple navigation in iOS using NavigationController.
you have six viewControllers 1, 2, 3, 4, 5, 6.
to do this:
First create a NavigationController and initialize it with ViewController 1 (ie a root View Controller).
Now your navigationController behaves like a stack which contains all ur pushed view controller. NavigationController is only push and pop ur view controllers.
So, every time when u want to navigation first check ur viewController is inside navigationController stack or not. If it is already in stack then pop to that controller, if not then push the same view controller.
for this use following:
In case ViewController3
-(void)popToSelectedViewController
{
NSArray *vc=[self.navigationController viewControllers];
ViewController3 *vc3=nil;
for (int i=0; i<[vc count]; i++)
{
UIViewController *tempVC=[vc objectAtIndex:i];
if([tempVC isKindOfClass:[ViewController3 class]])
{
vc=[vc objectAtIndex:i];
break;
}
}
if(vc3)
{
//If exists inside stack the pop
[self.navigationController popToViewController:vc3 animated:YES];
}
else
{
//If not exists inside stack push ViewController3
ViewController3 *vc3New= [[ViewController3 alloc]initWithNibName:#"ViewController3" bundle:nil];
[self.navigationController pushViewController:vc3New animated:YES];
[vc3New release];
}
}
For initializing ur ViewController1 with navigationController:
if using storyboard
Embed ur initialViewController(ie viewController3) with UINavigationController.
for this:
Step1: open storyboard, and select ur initialViewController(ie viewController3).
Step2: Go to Editor in menu -> Choose Embed In -> Select UINavigationController.
this creates a navigationcontroller and initializes with viewController3 as rootViewController.
if not using storyboard
make property of vc3 (ViewController3) and applicationNavigationController (UINavigationController) in .h
and in .m:
got method "application... didFinishedLaunching...." in appDelegate
and write:
self.vc3=[[ViewController3 alloc]initWithNibName:#"ViewController3" bundle:nil];
self.applicationNavigationController=[[UINavigationController alloc] initWithRootViewController:self.vc3];
self.window.rootViewController=self.applicationNavigationController;
[self.window makeKeyAndVisible];
First You create a navigation controller object
UINavigationController *navCtrl = [[UINavigationController alloc]initWithRootViewController:rootViewController];
self.window.rootViewController = navCtrl;
If you want to go to 1->2,1->3,1->6,etc, create an object for the next viewcontroller and push it to navigation stack
[self.navigationController pushViewController:second animated:YES];
You dont need to do any additional work to go back to the previous view controller. The default back button lets you go back.
If you need to return to the root view controller, then use this:
[self.navigationController popToRootViewControllerAnimated:YES];
if you want to return to any particular view controller, then use this
[self.navigationController popToViewController:viewController animated:YES];
Use UINavigationController.It is nor circular.It can be assumed as stacked approch.That is what navigation controller do
A must read for you
I am having a problem with this.
In my root view controller I am having a textfield & one button. I am giving a condition like if i entered 0 in textfield then only it should move to next view.
upto here it is working correctly. But now here is problem. Here I am having one button & given navigation to third view controller for that button. here i am getting error as
Terminating app due to uncaught exception 'NSGenericException', reason: 'Push segues can only be used when the source controller is managed by an instance of UINavigationController.'
image ref: http://img12.imageshack.us/img12/8533/myp5.png
and i am giving action for button in first view as below
- (IBAction)Submit:(id)sender {
if([tf.text isEqual:#"0"])
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
SecondViewController *vc2 = [storyboard instantiateViewControllerWithIdentifier:#"SecondViewControllerID" ];
[self presentViewController:vc2 animated:YES completion:NULL];
}
}
Go To:
Editor--> Embed In --> Navigation Controller, to add Navigation Controller to your initial view
After looking at your screenshot, you either need to
Push your SecondViewController onto the existing navigation controller's stack instead of presenting it modally OR
You need to embed your SecondViewController in another navigation controller and then create and present that navigation controller modally from your SamplesViewController
Either way, SecondViewController needs to be embedded in a navigation controller before you can use Push Segues from it
Either embed your view controller in a Navigation controller or if there is a Navigation controller in the story board mak it the initial view controller
I also faced same problem. My problem was I was using model segue style (rather that push) for one before the current controller because of that I think it broke the Navigation chain before already.
Embed your view controller in a UINavigationController is a good option if there is no UINavigationController in the storyboard. Otherwise you can select the UINavigationController and select the root view controller in the inspector pane, then drag it to the UIViewController you want to use as root. the screenshot is as below:
I solved this by creating a custom UIStoryboardSegue between the UINavigationController and the destination view controller:
-(void) perform
{
assert([self.sourceViewController isKindOfClass:[MyNavigationController class]]);
MyNavigationController *launchController = (MyNavigationController *) self.sourceViewController;
[launchController setViewControllers:#[self.destinationViewController] animated:YES];
}
Try this. It works for me.when you set root view controller to first view and use self.presentViewController ,controller move to next view but instance of navigation controller is only for first view,third view required navigation instance so use self.navigationController instead of presentViewController.
NSString * storyboardName=#"Main";
UIStoryboard *storybord=[UIStoryboard storyboardWithName:storyboardName bundle:nil];
SecondViewController *vc=[storybord instantiateViewControllerWithIdentifier:#"SecondViewControllerID"];
[self.navigationController pushViewController:vc animated:YES];
In my application 2 tabs are there. I am adding tabbarController to the window with two view controllers viewcontroller1 and viewcontroller2 in didFinishLaunchingWithOptions. Now I need to add a button in the viewcontroller1 and in the button action i need to push a new viewcontroller nextViewController. To do this in the button action, i had created a navigation controller and sets its rootview controller as viewcontroller1 and then i push the nextViewController through that navigation controller. But the nextViewController is not getting loaded. Why?
(void) buttonAction {
UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:self];
nextViewController *nextViewControllerObj = [[nextViewController alloc]init];
[self.navigationController pushViewController:nextViewControllerObj animated:YES];
}
Instead of adding first tab view controller into UINavigationController, please try adding main UITabBarController into UINavigationController in "didFinishLaunchingWithOptions".