Cancel button in Navigation View not working - ios

code for cancel button is
- (IBAction)cancelButton:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
While tracing the program I can see the method is called however the view is not dissmissing to the previous view controller.

if you use Navigation View controller you should use
[self.navigationController popViewControllerAnimated:YES];
instead of dismissViewControllerAnimated
PS actually, I think you use storyboards and segues and in this case you should use UnwindSegue What are Unwind segues for and how do you use them?
Also you should read some common documentation about in app navigation...

Related

Watchkit , dismissController function

i want to push in another view controller with code
[self pushControllerWithName:#"secondeviewcontroller" context:#"Hello"];
its work great for pushing but for dissmiss
in secondviewcontroller doesn't work
[self dismissController];
In a hierarchical interface, you need to to use [self popController] to go back after pushing a controller.
[self dismissController] is used in modal interfaces.

Auto segue when rootViewControl loads

I have a Navigation Controller in a storyboard set as the initial view. It calls one viewController, lets call it inititalViewCon. I want initialViewCon to modally segue to another viewController as soon as it loads.
So it would open the app, go to initialViewCon and then have a modal segue pop up. How would I accomplish this?
I subclassed the UIViewController, the second one, which I know is against Apple's guidelines.
I added this method to the subclass:
- (IBAction)doneButton:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
It's kind of strange that you can initiate a modal segue from a storyboard but there's no way to dismiss it without subclassing the viewController.

is there any way to popView controller in Storyboard?

I have an application in storyboard and make the connections to second view controller by push operation in the storyboard itself(without using coding).
Is there any way to pop to first view controller without writing any code and using the storyboard?
Note:by using navigation controller we will have back button but if a button is created in the second view controller and when we tap on that button we should pop to first view controller.
Storyboards don't provide a way to return from a segue without coding, but this can be easily accomplished with this code:
[self.navigationController popToRootViewControllerAnimated:YES];
Storyboards doesn't provide a "no-code" way to popViewController, you need to implement by yourself.
And if you want to pop to the first viewController you need to use:
[self.navigationController popToRootViewControllerAnimated:YES];
You can use an UnwindSegue as well since iOS6, although it does require some code)* for push segues. For a good explanation of UnwindSegues, see SO post, scroll down to "In a Nutshell"
)*Specifically, you need an unwind action, e.g.
- (IBAction)unwindToThisViewController:(UIStoryboardSegue *)unwindSegue
A solution that involves writing code once but can be used afterwards in your storyboards as a segue without further implementations would be some kind of PopSegue as subclass of a UIStoryboardSegue.
#interface PopSegue : UIStoryboardSegue
Overwrite the -perform method as follows.
#implementation PopSegue
- (void)perform
{
[[self.sourceViewController navigationController] popViewControllerAnimated:YES];
}
#end
Use a custom segue for back-navigation and specify the PopSegue class.

Dismiss one modal view controller after showing another

I've done a bit of research and read other answers I found here but haven't found anything that actually works. I have an app that when something is posted I want to go to the post and if the back button is pressed when viewing the post it should go back two views basically skip over the compose view.
Below is what I've tried but it gives
Warning: Attempt to present on whose view is not in the window hierarchy!
-(IBAction)post{
[[self presentingViewController] dismissModalViewControllerAnimated:NO];
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
ViewPostViewController *dvController = [[ViewPostViewController alloc] initWithNibName:#"ViewPostViewController" bundle:[NSBundle mainBundle]];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:dvController];
nc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:nc animated:NO];
[nc release];
}
If you trying to create a hierarchy of views like this, you should probably be using these:
[self.navigationController pushViewController:ViewController animated:BOOL completion:nil];
[self.navigationController popViewControllerAnimated:BOOL completion:nil];
Instead of:
[self presentViewController:ViewController animated:BOOL completion:nil];
[self dimissViewControllerAnimated:BOOL completion:nil];
PresentViewController is usually used to show a single view controller then dismiss it, not generally when you want to show several view controllers in a chain, then work your way back up the chain.
The former is advisable because it uses the stack concept to push and pop view controllers. So you can start with your initial list set up as the root view controller, push on your post compose view, then push on the third view to go to posting. Then when you want to go back to the first view controller by popping off two view controllers, you can use:
[self popToRootViewControllerAnimated:BOOL completion:nil];
You might find the UINavigationController reference useful.
Good luck.
If you want to present a view controller right after another modal view controller has animated out then you have to delay it because otherwise the new one will not appear.
before iOS 5 you would do a performSelectorAfterDelay: with something like 0.25 sec. For iOS 5 and above you wouldn't use modelViewController methods any more as those have been deprecated. Instead you use the presentViewController methods which give you an completion block that is called when the animation is done.
I'm a little confused about what you're trying to do. If you're using a navigation controller, you should be doing pushes and pops, not presenting and dismissing. If you want to use navigation controllers, then you can use popToViewController:animated: to go back to any particular controller without passing through the ones in between. You would have to create a custom back button, though, or do it in code, because the standard back button will only take you back to the previous view controller.

How to dismiss a view controller opened with presentModalViewController:

I presented a view controller using presentModalViewController:, now how to close/dismiss it?
For iOS6 use this code
[self dismissViewControllerAnimated:YES completion:Nil];
instead of
[self dismissModalViewControllerAnimated:YES];
This may help you.
From the controller presented modally:
[self dismissModalViewControllerAnimated:YES]
You have two choices, both involves using dismissModalViewController.
The preferred way is to use delegation and tell the view controller who was responsible for presenting the view to dismiss it. The other way is to have the view who was presented to dismiss itself (which actually asks the parent to dismiss it.)

Resources