iOS: Loading UIViewController with a popover segue, trying to dismiss itself - ios

I have a UIViewController instance segued to by way of a popover segue. It is NOT a UIPopOverViewCOntroller so there's no dismissPopoverAnimated: method.
I have a button in the its main UIView that I want to dismiss the VC. How can this be done?
I have this, which does not work:
- (IBAction)submitButtonClicked:(id)sender
{
[self dismissViewControllerAnimated:YES completion:^{}];
}

That is the correct code usually if you aren't going to do anything in the completion handler just set it to nil. Did you create this button in the storyboard and if so did you link it to this action?
If you set a breakpoint on that line of code does it actually run when you click the button? If not then your button isn't linked to the IBAction.

Related

Unwind a Modal UINavigationController to another ViewController

I am trying to segue back to another UIViewController after modally presenting a UINavigationController and with it a root ViewController. What I want to happen is for a button to unwind from the root VC of the UINavigationController and return to the initial UIViewController that presented it. I have the unwind method written in the destination VC and button is hooked up to the Exit point of the VC. But when I press the button, nothing happens. Can someone explain what I am doing wrong? Any advice is appreciated.
Storyboard:
Storyboard
Code:
-(IBAction)unwindToClockView:(UIStoryboardSegue*)sender;
{
NSLog(#"Back to Clock View");
}
Additional code can be found here: https://github.com/xinkecf35/TrafficAlarmClock
Okay, I have a solution. It just involves programmatically calling a method trigger by the button that outright dismisses the view controller. The method is below.
-(IBAction)dismissSettings:(id)sender
{
[self dismissViewControllerAnimated:TRUE completion:nil];
}
If anyone can present a solution that uses Storyboards as well, it will be greatly appreciated.

`dismissViewControllerAnimated: completion:" dismisses keyboard and not VC

I have a ViewController with an UISearchController inside (with a table view on it). I've added this line on didSelectRowAtIndexPath::
[self dismissViewControllerAnimated:YES completion:nil];
For some reason, the viewController isn't being dismissed and instead, the keyboard is being dismissed (the searchController becomes inactive), to dismiss the viewController I have to reselect a cell on the table (and then didSelectRowAtIndexPath: is being called twice).
Any idea why is it happening?
Thank you!
This is probably happening because UISearchController inherits from UIViewController and therefore the controller being dismissed in didSelectRow is actually the search controller.
Perhaps simply try dismissing twice so that it removes the search controller followed by your custom view controller:
[self dismissViewControllerAnimated:YES completion:nil];
[self dismissViewControllerAnimated:YES completion:nil];
Try to resigning keyboard first and then dismiss vc.

segue to the TabbarController

In my image, my first Tabbar is HomeViewController and the second Tabbar is CameraViewController.
What is the proper way to segue to the Tabbarcontroller? You can see the read line, I try to segue this but I
always get the back button in my HomeViewController and It display weird like not showing the navigation name.
In CameraViewController I hide the Tabbar for the use camera button. I try to use segue programmatically like this one.
[self performSegueWithIdentifier:#"sample" sender:sender]
but It doesn't work properly. Is this possible to segue to TabbarController?
You can't call
[self performSegueWithIdentifier:#"sample" sender:sender]
Because you're in TabBarController already. You could implement custom flow. By pressing "Back" button - just show TabBar and change selected tabBarItem for example…
update
used this
[self.tabBarController setSelectedIndex:0];

Give IBAction priority instead of segue

I'm using storyboard, and in my storyboard I got a button which I connected with another view controller. So when you click the button, the other view controller shows up. I connected this using an Action Segue - Push in the storyboard. I also connected the button with an IBAction property. The problem is when I click the button it first goes to the view controller that its connected to, and after that it executes the IBAction function. How can I change this order?
You cannot change that order. There are two things that you can do.
First:
Use the segue only.
Overwrite prepareForSegue: and place your code there. If there is more than one segure from that view controller then you can distinguish within prepareForSegue: which one is currently being performed. For that you should provide them with unique names/segue IDs.
Second:
Use the IBAction only.
Within the IBAction method, at its very end, call performSegueWithIdentifier:sender:. Again, for that you will have to name all your segues with unique segue IDs.
The question to ask yourself is, why are you using both? Pick one and stick with it IMO.
If you want to perform some actions when the segue happens you should implement
prepareForSegue:
Update to address comment
I actually don't know how I can do it in code, how can I load a ViewController that is on my storyboard without using the Action Segue Push?
The easiest way that I've found is in your Storyboard you can set an identifier for the view controller.
Notice Storyboard ID is set to MyViewController
Now in your IBAction method you can do the following.
UIViewController *myViewController = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:#"MyViewController"];
[self presentViewController:myViewController animated:YES completion:nil];
I solved this by chaining the IBAction from my segue. I find this approach the most useful as I can have a "save" action without a segue (chain a button to the IBAction, sometimes useful), and also use a segue for dismissing the view controller when necessary.
-(IBAction)save:(id)sender
{
/* Do some Save stuff */
}
Then in my prepareForSegue:sender:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"save"])
{
[self save:sender];
}
}
Link up your non-segue buttons to the IBAction, and segue your unwind or other such buttons with an identifier as "save". Magic.

iPad dismiss popover with button within the popover itself

I have a button in my popover controller. I want to use it to dismiss the popover, so I am trying to access a method (dismissPopover) of the presenting view controller (the "root" view controller).
Note: the method to dismiss the popover is already set up and working, in the root VC, which is the delegate. If I call it it will dismiss the popover. I just need to access the method from the popover.
To do this I set up a property in the AppDelegate, and get an instance of the rootVC like this: self.rootController = (ViewController*)self.window.rootViewController;. Then I imported the root VC class and the AppDelegate to the popover's view controller's class, as below. Seems to give me access to the rootVC, and the methods, but the results do not fire the method. Any idea what I am missing here?
#import "ViewController.h"
#import "AppDelegate.h"
Action connected to button:
- (IBAction)dismissPopover:(id)sender {
//Checking the button works, it does:
NSLog(#"dismissPopover, from popover");
//Trying to get an instance of the rootViewController, the "presenting view controller"
ViewController *rootVC = [(AppDelegate *)[[UIApplication sharedApplication] delegate] rootController];
//trying to access the method in the rootVC that dismisses the popover
[rootVC dismissPopover];
//Tried the following code, does nothing:
//[self dismissPopoverAnimated:YES];
}
NOTE: I ended up abandoning the use of a popover for this as it became a bit over complicated. I tried loading my view controller into a UIView (so I could load the contents of a nib to a pop-up view). That also became a bit complicated. So, for now I am just building my desired interface in a UIView programatically. So far works great.
dismissPopoverAnimated: is a method of UIPopoverController class. so, you need a popover controller reference in your 'root' view controller.
MyRootViewController.myPopoverController = thePopover;
the button is in your 'root' view controller, and in it's action method:
[self.myPopoverController dismissPopoverAnimated:YES];
In iOS 8, you can dismiss the popover (if it's coming from a segue, at least) with dismissViewControllerAnimated:completion: from within the popover. Doesn't work in iOS 7 (or below), however.
Popover automatically dismissed when clicking outside it , as you order a button to dismiss it you can simply use the following code inside your dismissPopover method :
[self.popoverController dismissPopoverAnimated:YES];
you don't need all this tedious work !
[self dismissViewControllerAnimated:YES completion:nil];
is the solution;
you just need an IBoutlet or add target to your button and then call above line
I had the same problem
just do in your buttonClickMethod:
[yourPopoverController dismissPopoverAnimated:YES];
hope you help!
cheers

Resources