presenting modal views in a popover - ipad

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

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

How do I present a modal UINavigation view over my tab bar iOS app?

Here is the code I am calling to pop a navigation controller over my entire tab bar app:
LoginViewController *logController = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
EditNavController *navController = [[EditNavController alloc] initWithRootViewController:logController];
[self.navigationController presentModalViewController:navController animated:NO];
and I am getting the following warning in my log:
Warning: Attempt to present <EditNavController: 0xa95b980> on <MyTabController: 0x1152fd60> whose view is not in the window hierarchy!
The code works, but I want to do it the correct way so this warning doesn't appear.
How do I fix this?
if MyTabController is a tabbarcontroller, you should use
[self presentViewController:navController animated:YES completion:^{}];
remember that presentModalViewController:animated: is deprecated since iOS6. I think the problem is you are trying to present the viewController in Navigation Controller of the tabbar, wich is not in the view hierarchy (because it's not shown). Instead, you should present the modal view controller in the Tab Bar controller (wich IS in the view hierarchy).
I hope it helps!

Having issues displaying modal view programmatically

I am very very new to iOS programming / Objective C.
The flow of events I want to have happen is: user selects tab from tab bar view controller. Once VIEW A has been loaded it will open a modal window to get some information
VIEW A - (void)viewDidLoad
ModalYearPickerViewController *modalYearPickerViewController= [[ModalYearPickerViewController alloc] init];
[self presentViewController:modalYearPickerViewController animated:NO completion:nil];
I am trying to have my year picker view load up right away so the user can select a year from my picker (in VIEW B), then close the modal window after the value has been passed back to VIEW A.
Now, the fist view loads, then goes to a black screen automatically. I am unsure why as my view controller for modalYearPickerViewController has a picker etc on it.
Any tips or help loading a modal view controller programmatically would be greatly appreciated!
Thanks!
If you are using storyboards :
UIStoryboard *storyBoard = [self storyboard];
This will return the storyboard of your current view controller. I am assuming your View Controller A is also on your storyboard.
ModalYearPickerViewController *modalYearPickerViewController = [storyBoard instantiateViewControllerWithIdentifier:#"ModalYearPickerViewController"];
This will instantiate your view controller from the storyboard. But one other thing you have to do is set your view controllers storyboard id to ModalYearPickerViewController. You can set this right below where you set your custom view controller class in the storyboard.
[self presentViewController:modalYearPickerViewController animated:NO completion:nil];
and done.
If you have a xib file for that viewContrioller, it has to be loaded as well, to do that you have to call:
ModalYearPickerViewController *modalYearPickerViewController =
[[ModalYearPickerViewController alloc] initWithNibName:#"ModalYearPickerViewController" bundle:nil];

iPad presentViewController is always Fullscreen (need formSheet) iOS6

I need to present a viewController as a formSheet when the user taps a 'settings' button in the navigation bar in the iPad side of my universal app. The view is called "SettingsViewController," and it's a View Controller in my iPad Storyboard with an ID of "settings".
I'm not using segues because in my iPad version of the app, I have more than one button in the navigation bar on the left side, so I have to add the buttons progammatically. (can't connect a segue to a nonexistent UIBarButtonItem in the storyboard.)
The functionality I want is there, the presentation is not.
When I present my SettingsViewController, on the iPad, it's presenting as a full screen view.
I need it to be a form sheet.
I'm wrapping it in a NavigationController, which I need and works fine, just need it to present itself in the correct manner.
Here's the code that presents the view:
-(void)showSettings:(id)sender {
SettingsViewController *svc = [self.storyboard instantiateViewControllerWithIdentifier:#"settings"];
svc.delegate = self;
svc.modalPresentationStyle = UIModalPresentationFormSheet;
UINavigationController *navcont = [[UINavigationController alloc] initWithRootViewController:svc];
[self presentViewController:navcont animated:YES completion:NULL];
}
I've tried everything I can think of. Any help is deeply appreciated.
Looks like you set the modal presentation style on SettingsViewController but you should have set it on your UINavigationController.
navcont.modalPresentationStyle = UIModalPresentationFormSheet;

navigation bar not showing with a programmatically made UINavigationController

I have a navigation controller named navController made programmatically in my modal view controller during its viewDidLoad:
self.navController = [[UINavigationController alloc] initWithRootViewController:self];
self.navController.view=self.view;
[self setView:self.navController.view];
But when i launch the modal view controller i dont see the navigation bar, just the standard view i made in IB. Whats wrong?
Your solution cannot work.
Suppose that you have your modal controller called ModalViewController. It's a simple UIViewController linked with a xib created interface.
Now, at some point you need to present ModalViewController modally. As you wrote in your specification, I think you want to use also a UINavigationController and control its navigation bar.
The code to do this could be the following, where presentModally could be a method that it's not contained in ModalViewController.
- (void)presentModally:(id)sender {
ModalViewController *modalController = [[ModalViewController alloc] initWithNibName:#"ModalView" bundle:nil];
// Create the navigation controller and present it.
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:modalController];
[self presentViewController:navigationController animated:YES completion: nil];
}
Now, within viewDidLoad of your ModalViewController you have access to navigationController property. In this manner you can control navigationController behaviour. For example:
- (void)viewDidLoad
{
[super viewDidLoad];
// the code changes the title for the navigation bar associated with the UINavigationController
self.title = #"Set from ModalViewController";
}
Some notes
To understand how UINavigationController works read UINavigationController class reference
To understand how modal controllers work read Modal view controllers documentation
The code I provided is a simple example and only demonstrative (I've written by hand so check for syntax). You need to make attention to memory management and how to present modal controllers. In particular, as Apple documentation suggests, to present modal controllers you need to follow these steps:
Create the view controller you want to present.
Set the modalTransitionStyle property of the view controller to the desired value.
Assign a delegate object to the view controller. Typically the delegate is the presenting view controller. The delegate is used by the presented view controllers to notify the presenting view controller when it is ready to be dismissed. It may also communicate other information back to the delegate.
Call the presentViewController:animated:completion: method of the current view controller, passing in the view controller you want to present.
Trigger (when necessary) some action to dismiss the modal controller.
Hope it helps.

Resources