Display an UIPopoverPresentationController from a UIControl? - ios

I am writing a UIControl. I need to display a popover when the user touches an area of the control. But of course the usual code:
[self presentViewController:self.popover animated:YES completion:nil];
does not work because we are in a UIControl, not in a UIViewController.
How can I display a popover from a UIControl?

You should use rootViewController to present it.
Use UIApplication.sharedApplication.delegate.window.rootViewController instead of self
[UIApplication.sharedApplication.delegate.window.rootViewController presentViewController:self.popover animated:YES completion:nil];

Related

iOS - Display a UIViewController as fullscreen from within a UINavigationController

I need to present a UIViewController as fullscreen (Above all other views). This UIViewController is currently inside a UINavigationController.
Is it possible to have the UINavigationController present it's current top UIViewController modally?
From within the UIViewController I would like to fullscreen, if I use:
[self.navigationController presentViewController:self animated:YES completion:nil]
Nothing seems to happen.. Is there a reason this is not allowed?
EDIT
I have posted another question in which I have solved this issue for every scenario. (Code snippet included there)
Is it possible for a UIViewController to present itself?
If you want to present a view controller modally, you should be presenting it from a different view controller. The modal presentation will show the presented view controller over the navigation controller (in fullscreen)
For instance,
UIViewController *firstVC = self;
UIViewController *secondVC = <the VC you want to present>
[firstVC presentViewController:secondVC animated:YES completion:nil]
Or just use a modal segue in Interface Builder.
You should use this :
[self presentViewController:self.navigationController animated:YES completion:nil];
to show a controller.
Assuming that the view controller you want to show modally is the navigation controllers top view controller
[self presentViewController:self.navigationController.topviewcontroller animated:YES completion:nil];
Hope this helps

Saving text to another view controller

I use this code it works fine but not the way I want. I don't want this IBAction to take me to the second View Controller I just want it to save the text to the second View Controller.
- (IBAction)passtexttovc2:(id)sender {
ViewController2 *vc2 =[self.storyboard instantiateViewControllerWithIdentifier:#"ViewController2"];
vc2.stringfromteztfield1= self.Actionforbutton.text;
[self presentViewController:vc2 animated:YES completion:NO];
}
Remove
[self presentViewController:vc2 animated:YES completion:NO];
Only present it when you wanted to.
The presentViewController method is unnecessary here. You do not need to 'present' the view controller here per se.
When you say save, i think of core data: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html

Dismissing view controller to reveal desired one on the backside

Suppose i have 3 view controllers. I initiate vc1 in vc0 and then vc2 in vc1. Now i want vc2 to be dismissed and after that only vc0 should be shown behind it. How's that possible? I read something about delegate declaration. But couldn't understand.
I have UISegmentcontrol where i'm displaying a controller from storyboard like - vc0 = [self.storyboard instantiateViewControllerWithIdentifier=#"vc0"]; and making it a subview of it [self.view addSubview:vc0.view]; Vc0 is a tableView, which has a detailcontroller to be presented. When i tap on a cell, it shows detailview, but actual segmentcontrol.view is lost when detailview is dismissed.
An example would be awesome.
PS: I'm not using segue for the viewControllers. Instead, i'm using presentModalViewController and dismissModalViewController.
In iOS 5 you need to do
[self.presentingViewController.presentingViewController dismissModalViewControllerAnimated:YES]
As of iOS 6 dismissModalViewControllerAnimated: is deprecated.
You need to call
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:^{ // Do something on completion}]
//If you want to perform something while going through views or other wise keep completion to nil as shown in below code.
or
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
or
[self.parentViewController.parentViewController dismissModalViewControllerAnimated:YES];

Dissmis a modal viewcontroller on a Storyboard

I'm developing an iPhone application with latest SDK.
I don't know how to dismiss a ViewController. I've added a back button and this is what I do:
- (IBAction)backButtonClicked:(id)sender
{
[self.navigationController dismissModalViewControllerAnimated:YES];
}
I have also tried:
[self.navigationController popViewControllerAnimated:YES];
But either works. I've added a breakpoint and it stops on it.
And I use a Modal segue to navigate to this ViewController.
How can I dismiss this View Controller programmatically?
Try [self dismissViewControllerAnimated:YES completion:nil];
- (IBAction)backButtonClicked:(id)sender
{
[self.presentingViewController dismissViewControllerAnimated:YES
completion:nil];
}
Is the VC being modally displayed an instance of your own view controller subclass, or did you wrap that inside a UINavigationController? If the former, try
[self dismissModalViewControllerAnimated:YES];
[self.presentingViewController dismiss...] will also work, but only for iOS 5+
create a custom segue,then button action choose the custom segue
http://jeffreysambells.com/2014/02/19/dismissing-a-modal-view-using-a-storyboard-segue

presenting modal views in a popover

Im trying to load a modal view from a view controller that is displayed in a popover. The modal view loads but the problem is that it transitions into the main view and not within the popover. Is it something Im missing? I thought simply initiating it from a vc within a popover would present the modal view within the same popover...
The code is nothing special as bellow:
- (IBAction)myButton{
ModalVC *controller = [[ModalVC alloc] initWithNibName:#"ModalVC" bundle:nil];
[self presentModalViewController:controller animated:YES];
[controller release]; }
If you add
controller.modalPresentationStyle = UIModalPresentationCurrentContext;
before the call to presentModalViewController:animated your ModalVC should be displayed within the popover.
Another way to display something modally in a popover is to use the property modalInPopover.
Unfortunately, it seems you cannot to this and it violates the HIG. From Apple's View Controller Programming Guide
Note: You should always dismiss the visible popover before displaying
another view controller modally. For specific guidelines on when and
how to use popovers in your application, see “Popover (iPad Only)” in
iOS Human Interface Guidelines.
It's because you presentModalViewController to self. Try to add a UIBarButtonItem in your ViewController (self) and do:
[controller presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
where sender is the UIBarButtonItem

Resources