How can I present a modal view controller from the app delegate's view, the top most? Trying to present a modal view controller from a UIView, which made me confused.
Use your rootViewController. You can present a modal view controller from any view controller subclass. If your root VC is a UITabBarController, then you can do:
[self.tabBarController presentModalViewControllerAnimated:YES]
or if its a navigation controller:
[self.navigationController presentModalViewControllerAnimated:YES]
etc.
EDIT: MVC
By trying to present a controller from within a view you are breaking the MVC pattern. Generally, a view is concerned with its appearance and exposing interfaces to communicate user interface state to its controller. For example, if you have a UIButton in your view and you want it to present a modal view controller, you don't hard wire the view to do this. Instead, when a controller instantiates the view, the controller configures the button by setting itself as a target to receive the touchUpInside action where it can present the appropriate modal view controller.
The view itself does not (and should not) have this contextual knowledge to do the work of a controller.
The best way to do this is to create a new UIWindow, set it's windowLevel property, and present your UIViewController in that window.
This is how UIAlertViews work.
Interface
#interface MyAppDelegate : NSObject <UIApplicationDelegate>
#property (nonatomic, strong) UIWindow * alertWindow;
...
- (void)presentCustomAlert;
#end
Implementation:
#implementation MyAppDelegate
#synthesize alertWindow = _alertWindow;
...
- (void)presentCustomAlert
{
if (self.alertWindow == nil)
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
UIWindow * alertWindow = [[UIWindow alloc] initWithFrame:screenBounds];
alertWindow.windowLevel = UIWindowLevelAlert;
}
SomeViewController * myAlert = [[SomeViewController alloc] init];
alertWindow.rootViewController = myAlert;
[alertWindow makeKeyAndVisible];
}
#end
Application delegates do not manage a view. You should present a modal view controller from the -viewDidAppear: method of the first view controller that gets put on screen in -application:didFinishLaunchingWithOptions:.
Related
I am presenting a view controller from another view controller using presentViewController.
The presenting view controller (The "SourceViewController") creates the new view controller and assigns it to a navigation controller before presentation (because the "NextViewController" wants a navigation bar and navigation controller).
// from the source view controller
#implementation SourceViewController
-(void)showNextViewController
{
NextViewController *viewController = [[NextViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
[self presentViewController:viewController animated:YES];
}
#end
#implementation NextViewController
// in NextViewController
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
#end
But when I present the view controller when the originating view controller is in landscape the "NextViewController" isn't presented in portrait but rather in landscape like the source view controller.
I've tried many combinations of rotation methods but haven't been able to get it to present in the correct orientation.
I assume that it is possible because many apple components like UIImagePickerController are always presented in portrait , so how do I force its orientation?
Thanks
EDIT:
I've created a UINavigationController sub class:
PortraitNavigationController : UINavigationController
#implementation
-(BOOL)shouldAutorotate
{
return NO;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
#end
and then when presenting the NextViewController
PortraitNavigationController *nav = [PortraitNavigationController initWithRootViewController:nextViewController];
[self presentViewController:nav animated:YES];
and now NextViewController is indeed in portrait - but when I rotate the device to use this view controller and eventually dismiss it - the underlying source view controller looks all messed up.
The underlying view controller is a custom container view controller which is embedded in a UINavigationController
The containers it uses to display the child view controllers are not in their correct places
I don't want the container view controller to rotate at all as soon as the NextViewController is displayed and dismissed.
When you rotate your device the presented view controller is asked about the rotations and orientations it supports - In your case it's a UINavigationController and not an instance of NextViewController. One way to fix this is to subclass UINavigationController and override the rotation methods and forward the calls onto it's root view.
As a side not UIImagePickerController is a subclass of UINavigationController. It might make more sense to make NexViewController a subclass of UINavigationController and then inside that subclass initialize it with the correct root view controller.
Another option is to just alloc and init a UINavigationBar inside of NextViewController and add it as a subview if you don't need to use the navigation controller for anything. In this case autolayout comes in handy because you can pin it to the top, left, and right and let it figure out the correct size and location for it.
I am presenting a WEPPopoverController in my iPhone Application. In that WEPPopoverController, I have added a UITableViewController. This popover I am using for the purpose of search.
Now when I open this Popover, the screen looks something like this:
Now, I want to close dismiss this Popover from the UITableViewController's didSelectRowAtIndexPath,
How to do this?
Code:
self.searchTableViewController = [[SearchTableViewController alloc] initWithStyle:UITableViewStylePlain]; // UITableViewController
self.seachPopoverController = [[[WEPPopoverClass alloc] initWithContentViewController:self.searchTableViewController] autorelease]; // WepPopoverController
self.seachPopoverController.delegate = self;
[self.seachPopoverController presentPopoverFromRect:[searchB frame] inView:topPanelV permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
Thanks in advance!!!
Add 1 variable in SearchTableViewController as parent like
id parent;
Create property for this as
#property (nonatomic,assign)id parent;
Synthesize it
#synthesize parent;
Now
when you create self.searchTableViewController
assign its parent as
self.searchTableViewController.parent = self;
Now in didSelectRowAtIndex
[parent dismisMyPopoverMethod];
-(void)dismisMyPopoverMethod
{
[self.seachPopoverController dismissPopoverAnimated:YES];
self.seachPopoverController = nil;
}
And thats it. Your are done.
Do it this way: make your view controller a delegate of table view controller for only one method - dismissPopover (for other methods the delegate must be table view controller). So your table view controller will have two delegates. Implement this method in your delegate (view controller). But call it from table view controller. In more details step by step:
Declare a property of your popover in a view controller, where you show this popover.
#property (strong, nonatomic) UIPopoverController *popoverController;
Create a new protocol (let's name it TableInPopoverDelegate), that has this method:
- (void) dismissPopover;
Add this method to the implementation file of your view controller and add this:
- (void) dismissPopover {
[self.popoverController dismissPopoverAnimated:YES];
}
Add a header file of protocol and add a delegate property to your table view controller:
#property (strong, nonatomic) id <TableInPopoverDelegate> popoverDelegate;
Make your view controller a delegate of your table view controller before adding it:
tableViewController.popoverDelegate = self; // Here self is your view controller
Call dismissPopover on the delegate when user selects a row.
[popoverDelegate dismissPopover];
I need to push a UIView into my UINavigation controller. I am doing it by
[self.view addSubview:showContactFlow];
And on a button click in UIView I need to push another UIViewController over the UIView. From the UIView I am not able to access self.navigationcontroller How can I do this?
Edit:
I have set the UIView as the view of a new UIViewController I am pushing into, the before mentioned UIViewController . Now I would like to know, how to handle the UIView button event inside its UIViewController, in which's view it is set.
Add a UINavigationController ivar to the UIView and assign it to the main view controller's. Then you should be able to access it from the UIView.
Edit:
Your UIView subclass:
// CustomView.h
#interface CustomView: UIView {
// ...
// your variables
// ...
UINavigationController *navController;
}
#property (nonatomic, assign) UINavigationController *navController; // assign, because this class is not the owner of the controller
// custom methods
#end
// CustomView.m
#implementation Customview
// synthesize other properties
#synthesize navController;
// implementation of custom methods
// don't release the navigation controller in the dealloc method, your class doesn't own it
#end
Then before the [self.view addSubview:showContactFlow]; line just add [showContactFlow setNavController:[self navigationController]]; and then you should be able to access your hierarchy's navigation controller from your UIView and use it to push other UIViewControllers.
You should try to work with an MVC approach. So your controller has access to all that stuff and can keep pushing and popping views, so the view doesn't need to know too much about the controller.
Otherwise, and for this case you can solve it fast by using delegation. So:
showContactFlow.delegate = self;
[self.view addSubview:showContactFlow];
So later in the UIView, you can just say:
[self.delegate addSubview:self];
This is gonna work, but it's not likely to be the best approach you should use.
On button click, you can present a view controller like,
-(void)buttonFunction{
ThirdVC *third= [[ThirdVC alloc]initWithNibNme];......
[self presentViewController:third animated:NO];
}
Using Core animation you can make NavigationController's pushviewController like animation on writing code in ThirdVC's viewWillAppear: method.
where do you add the UIButton is it in showContactFlow view or in the ViewController's view??
In regard to the modalViewControllers issue the correct method is
[self presentModalViewController:viewController animated:YES];
the standard animation in upwards
I am having issues with the back button not showing up on the SettingsViewController. The navigation bar does show up when the view is pushed, but no back button.
I am creating this inside a view controller, which is not a navigation controller. Any ideas or suggestions on what is actually going on here.
- (void)viewDidLoad
{
self.title = #"Settings";
}
- (IBAction)showSettingsModal:(id)sender
{
SettingsViewController *settingsViewController = [[SettingsViewController alloc] initWithNibName:#"SettingsViewController" bundle:nil];
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:settingsViewController] autorelease];
[self presentModalViewController:navController animated:YES];
[settingsViewController release];
}
You are creating a new navigation stack. You will need to add your own Back button and set the action of that to a delegate method on the calling VC to dismiss it.
UPDATE:
There seems to be lots of confusion about where and how to dismiss ModalViewControllers. The wrong thing to do in most cases is to call the Dismiss method from the Modal VC itself if you are wanting the parent to act on that dismissal. Instead, use delegation. Here is a simple example:
ModalViewController.h:
#protocol ModalViewControllerDelegate
-(void)dismissMyModalVC;
#end
#interface ModalViewController : UIViewController {
id < ModalViewControllerDelegate > delegate;
}
#property (nonatomic, retain) id < ModalViewControllerDelegate > delegate;
// The rest of your class properties, methods here
ModalViewController.m
#synthesize delegate;
...
// Put in the Method you will be calling from that Back button you created
[delegate dismissMyModalVC];
CallingViewController.h:
#import "ModalViewController.h"
#interface CallingViewController : UIViewController
<ModalViewControllerDelegate>
// Rest of class here
CallingViewController.m:
ModalViewController *mvc = [[ModalViewController alloc] initWithNibName:#"ModalViewController" bundle:nil];
mvc.delegate = self
[self presentModalViewController:mvc animated:YES];
...
// The ModalViewController delegate method
-(void)dismissMyModalVC {
// Dismiss the ModalViewController that we instantiated earlier
[self dismissModalViewControllerAnimated:YES];
That way the VC gets dismissed properly from the controller that instantiated it. That delegate method can be modified to pass along objects as well (like when you are finished logging a user in, etc)
SettingsViewController does not have a back button because it is at the bottom of stack. If you want a button to dismiss the modal dialog, you will have to add it yourself.
you can try this
UIBarButtonItem * backButton = [[UIBarButtonItem alloc]initWithTitle:#"Back"style:UIBarButtonItemStylePlain target:self.navigationItem.backBarButtonItem action:#selector(dismissModalViewControllerAnimated:)];
You are presenting your new controller as modal view controller. Modal controller presents its topmost. You should:
[self.navigationController pushViewController:navController animated:YES];
to push view controller onto the stack, and then you will see Back button.
Read Apple documenation on presenting view controllers:
https://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html
EDIT Didn't see that the calling view controller is not part of the navigation controller. In that case, you will have to create back button manually, and set it as a left bar navigation item.
I have been attempting to present a modal view from one of my views that I implemented following the following iDev tutorial/source code. The custom tab bar manages view by inserting them as subviews beneath the tabBar so when I call the modal view to be displayed from the subview it is covered up at the bottom by the tab bar. I have switched the following code to above the tab bar sub view which allows the whole screen to be displayed but it still creates animation problems.
// Set the view controller's frame to account for the tab bar
viewController.view.frame = CGRectMake(0,0,self.view.bounds.size.width, self.view.bounds.size.height-(tabBarGradient.size.height));
// Set the tag so we can find it later
viewController.view.tag = SELECTED_VIEW_CONTROLLER_TAG;
// Add the new view controller's view
[self.view insertSubview:viewController.view aboveSubview:tabBar];
http://idevrecipes.com/2011/01/04/how-does-the-twitter-iphone-app-implement-a-custom-tab-bar/
Please help this is driving me nuts!
So, i think you are presenting modal view from your subview like this:
[self presentModalViewController:someController animated:YES];
If so, your controller is presenting on current view (self) and if current view situated beneath other views - modal controller will be presented under them too.
You should get a reference to main parent view, so you could call presentModalViewController there.
In big projects with many subviews it's handy to have a reference to main view, I did it like this:
in MyAppDelegate #interface I have:
{
UINavigationController *navController;
}
+ (UINavigationController *) navController;
#property (nonatomic, retain) IBOutlet UINavigationController *navController;
And in the implementation:
// Used for showing modal windows over tabbar
static UINavigationController * _rootController = nil;
+ (UINavigationController *) navController {
return _rootController;
}
#synthesize navController;
in didFinishLaunchingWithOptions:
_rootController = navController;
(In this example my top view - navigation controller)
So anywhere in application I can call:
[[MyAppDelegate navController] presentModalViewController:someController animated:YES];