How to move from one view controller to another with an Action? - ios

IM not Using Storyboard.
Ive place a button, on my PlayViewController and when tapped I want the user to go back to the Main menu which is the ViewController.
When I add this line of code:
- (IBAction)backToMenu:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
It dismisses the ViewController - I want it to present it, So I used:
ViewController *viewcontroller = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
viewcontroller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:viewcontroller animated:YES completion:nil];
I get this ERROR: "Receiver 'ViewController' for class is a forward declaration"
I need to present my main menu, how am i supposed to do it?

You need to import the target view controller in your PlayViewController:
#import "ViewController.h"

Related

How to present a view controller programmatically

I have a button inside my nib file. How can I programmatically present another view controller when the user taps on this button?
As I understood , you want to push new viewController on Button click . Please try this
- (IBAction)buttonClicked {
MyViewController *myViewController = [[MyViewController alloc]init];
[self presentViewController: myViewController animated:YES completion:nil];
}
First you need to create an action for your button, normally via Ctrl+drag. On that action implementation you should alloc the destination ViewController. The code would be something like this:
- (IBAction)buttonTapped {
MyViewController *modalViewController = [MyViewController new];
[self presentViewController:modalViewController animated:YES completion:nil];
}
Since you said that your file is a nib, I'm assuming that you aren't on a storyboard with both ViewControllers on it and the ViewController to be presented is code based. If this isn't the case, please elaborate your question.
As requested: Loading a VC from a storyboard
- (IBAction)buttonTapped {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MyStoryboard" bundle:nil];
RSSViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"RSSVC"];
// OR If is initial
// RSSViewController *viewController = [storyboard instantiateInitialViewController];
viewController.feedURL = #"...";
[self presentViewController:viewController animated:YES completion:nil];
}
If you need, ViewController identifier should be set in the Identity Inspector in the Storyboard.
Refer the below screenshots
First : control+drag the button
Second: Once you release the mouse,it shows you the box
Third:Click Connection Dropdown box.It shows you 3 option
Fourth:Choose or Select Action from the drop down.
Five : Finally give action name
now in ViewContoller.h the action method is
- (IBAction)actionGoNextPage:(id)sender;
Then in ViewContoller.m
#import "SecondViewController.h"
- (IBAction)actionGoNextPage:(id)sender
{
SecondViewController *secondVC = [[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
[self presentViewController:secondVC animated:YES completion:nil];
}

when i use dismissviewcontrolleranimated , i got exc_bad_access

I got an error exc_bad_access,when i use dismissviewcontrolleranimated
the presentViewController code is:
TestViewController *testViewController=[[TestViewController alloc] init];
UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:testViewController];
[self presentViewController:nav animated:YES completion:^{
NSLog(#"presentViewController is finish");
}];
but,when i remove UINavigationController ,is error is disappear.
like this:
TestViewController *testViewController=[[TestViewController alloc] init];
[self presentViewController:testViewController animated:YES completion:^{
NSLog(#"presentViewController is finish");
}];
Thanks for help.
Present View Controller is presented over a "Root View Controller" not a UINavigationController.
You should present your VC from the Parent VC or Base VC like this
TestViewController *testViewController=[self.storyboard instantiateViewControllerWithIdentifier:#"storyboardIDofViewController"];//set the storyboard ID of the TestViewController in your storyboard by selecting attribute inspector after selecting the view controller.
[self presentViewController:testViewController animated:YES completion:^{
NSLog(#"presentViewController is finish");
}];
This is the correct way to present a VC from another VC.
UINavigationController have Push and Pull transitions and you are trying to apply Modal Transition to it.

Creating modal view from another modal view fails

In a view that was created modally, pressing a button causes the modal view to be dismissed and another modal view to load.
- (void)loadLanguageSelectionView {
[self dismissViewControllerAnimated:YES completion:nil];
UIViewController *languageSelectionController = [[LanguageSelectionViewController alloc] initWithNibName:nil bundle:nil];
[languageSelectionController setModalPresentationStyle:UIModalPresentationCustom];
[languageSelectionController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:languageSelectionController animated:YES completion:nil];
}
The following error is thrown when this code block executes:
DenkoStation[4259:73173] Warning: Attempt to present <LanguageSelectionViewController: 0x7b185430> on <ViewController: 0x79f52e50> whose view is not in the window hierarchy!
What surprises me is the fact that the code was running happily before I made some changes to my code as outlined here.
Where's the mistake?
Because you are trying to present a viewController on top of a viewController which is already dismissed and no longer in window hierarchy.
What you can try is, you can take the ParentViewController reference from current viewController and then you can present new viewController on ParentViewController Like This :
- (void)loadLanguageSelectionView {
UIViewController *parentController = self.presentingViewController;
[self dismissViewControllerAnimated:YES completion:^{
UIViewController *languageSelectionController = [[LanguageSelectionViewController alloc] initWithNibName:nil bundle:nil];
[languageSelectionController setModalPresentationStyle:UIModalPresentationCustom];
[languageSelectionController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[parentController presentViewController:languageSelectionController animated:YES completion:nil];
}];
}

Opening a ECSlidingViewController from a Normal View Controller

I'm using ECSliding and I have this problem!
In my project there are this files:
InitViewController (ECSlidingController)
FirstViewController (UIViewController)
SecondViewController (UIViewController)
LeftMenuViewController (UIViewController)
ThirdViewController (UIViewController)
I use InitView to instatiate my FirstView as the topview, sliding to right opens the LeftMenu.
In the LeftMenu there are 2 buttons, one button loads as topview the FirstView, the second one loads the SecondView.
In my First and SecondView there is an identical button that loads the ThirdView not as topview controller but as a new view:
ThirdViewController *third = [self.storyboard
instantiateViewControllerWithIdentifier:#"Third"];
[self presentViewController:third animated:YES completion:nil];
In my ThirdView are 2 buttons, one button loads the FirstView, the second one loads the SecondView.
As the ThirdView is not a topview but another view I have to recall ECSliding to open my FirstView or SecondView.
I succeeded loading the FirstView from my ThirdView by using my InitView
InitialSlidingViewController *home = [self.storyboard
instantiateViewControllerWithIdentifier:#"Init"];
[self presentViewController:home animated:YES completion:nil];
But how can I load the SecondView from my ThirdView?
My question basically is how can I load something that use ECSliding after a normal View.
From Third we want to go back to the sliding view controller and change the top view controller. What you're currently doing with this code:
InitialSlidingViewController *home = [self.storyboard instantiateViewControllerWithIdentifier:#"Init"];
[self presentViewController:home animated:YES completion:nil];
Is creating an entirely new sliding view controller to show. Eventually, by doing this, you'll run out of memory because you'll have hundreds of presented views allocated and never visible.
So, what we want to do is give Third a property:
#property (weak, nonatomic) ECSlidingViewController *slideController;
And we want to set that property before presenting Third:
ThirdViewController *third = [self.storyboard instantiateViewControllerWithIdentifier:#"Third"];
third.slideController = self.slidingViewController;
[self presentViewController:third animated:YES completion:nil];
Now, when one of the buttons is pressed on third we can say: "what's on display? Can we just dismiss or do we need to change and dismiss?":
- (void)oneButtonPressed {
if ([self.slideController.topViewController isKindOfClass:[SecondViewController class]]) {
FirstViewController *first = [self.storyboard instantiateViewControllerWithIdentifier:#"First"];
self.slideController.topViewController = first;
}
[self dismissViewControllerAnimated:YES];
}
You need to write the corresponding method for twoButtonPressed and you're done.
For your new comment, rather than presenting the third, instead you should put it into a navigation controller and present that. Then, when you need to present fourth and fifth you just push them into the nav controller and pop them off again. If required you can also give them a reference to the slideController.
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController :third];
[self presentViewController:nav animated:YES completion:nil];

Change view in one XIB

I'm trying to make an app using Interface Builder (instead of Storyboard) for the first time, but I'm stuck. Is it possible to change view using a ViewController in the same XIB?
I'd like that when I tap a button, the view changes to the other ViewController present in the same XIB file. Must I call back an action? If yes, which?
If you have 2 view controllers with 2 .xib files, like this
You can present the NextViewControler from your ViewController's button press method,
-(IBAction) yourButtonPressed:(id)sender;{
NextViewController *nextViewController = [[NextViewController alloc] initWithNibName:#"NextViewController" bundle:nil];
[self presentViewController:nextViewController animated:YES completion:nil];
}
EDIT :
You can present the same viewcontroller like this.
-(IBAction) yourButtonPressed:(id)sender;{
ViewController *viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[self presentViewController:viewController animated:YES completion:nil];
}

Resources