thanks for your time,here's my app's structure:It contains a MainController,and a LoginController, when user lanuch the app, first thing is to judge the login state,if it's yes, then set childController(aka.MainController) to RootViewController's (aka,NavigationController), else set childController(aka.LoginController) to RootViewController's (aka,NavigationController), when user input the username and password correctly,set childController(aka.MainController) to RootViewController's (aka,NavigationController)
When user click the quit button in the setting controller which belongs to MainController,then:
LoginController *vcLogin = [[LoginController alloc] init];
UINavigationController *ncRoot = [[UINavigationController alloc] initWithRootViewController:vcLogin];
[[UIApplication sharedApplication].keyWindow setRootViewController:ncRoot];
Now, the problem appears, console printed:
attempt to dismiss modal view controller whose view does not currently appear.
self = <_UIModalItemAppViewController: 0x9da0060> modalViewController =<_UIModalItemsPresentingViewController: 0x9e9bac0>
You know where the wrong-thing is?
This is not the way to set a new UIViewController. You should use presentViewController to present a modal view controller. If you want to use pushViewController without animation that's also possible. But it's a bad idea to change the rootViewController. Since that's not how the ViewController hierarchy is set up in iOS.
Related
My problem
I have a standard UIViewController. With the press of a button, it loads a form sheet modal view controller. When dismissing this modal view with the press of a UIBarButtonItem I call a method by doing:
ViewController *main = [[ViewController alloc] initWithNibName:nil bundle:nil];
[main updateLabel];
In the method -(void)updateLabel in the main ViewController I'm setting the text of a label, but the label won't change. But I know the function gets called, because if I do a NSLog(#"Method call test); instead of label.text = #"Test" I can see the message in console.
What am I doing wrong? It must be the way I'm calling the method in the main ViewController, because I can easily change the label anywhere else.
What I want to do:
When dismissing a modal view controller, I want a method to be called in the main view controller, and in this case change the text of a label.
Thanks for your help!
You're creating a new instance of ViewController with that code, not getting a pointer to the one you already have.
If ViewController is the controller that presented the modal view, then you can get a pointer to it with,
ViewController *main = self.presentingViewController;
A better way to do this would be to use the delegate pattern.
https://developer.apple.com/library/ios/documentation/general/conceptual/DevPedia-CocoaCore/Delegation.html
The following is a design pattern suggestion
The modal view controller shouldn't know how to dismiss itself, that is the responsibility of the presenting view controller. After all, it could have been presented in many different ways (modally, popover, push navigation). Using the delegate pattern, the modal view controller would tell its delegate that it should be dismissed when the bar button item gets pressed. The delegate, which is the presenting view controller, would then dismiss the modal view and update the label mentioned in your question.
So in my universal app I have a section where a person can look at an existing list of notes from our system (retrieved through a simple web service) and then also create a new note if they want. So for the iphone it's pretty simple layout, a TableViewController for displaying the list with a "Add" button on the NavigationBar that presents the modalview for adding the new item. On the iPad though, the same layout has a lot of wasted space so I opted to go with the popOver method to show the list in a popOver and then let them add from there. My problem is that when the user clicks on the Add button within the PopOver view, the modal view comes up full screen instead of just coming up within the popover view. Here's the code I have so far:
-(void) AddButtonPressed:(id)sender {
NewNoteVC *newNote = [[[NewNoteVC alloc] initWithNibName:#"NewNoteVC" bundle:nil] autorelease];
newNote.defaultClientID = defaultClientID;
UINavigationController *navCon = [[[UINavigationController alloc] initWithRootViewController:newNote] autorelease];
if ([isPopOver isEqualToString:#"YES"]) {
[navCon setModalInPopover:YES];
[self.navigationController setModalInPopover:YES];
[self.navigationController presentModalViewController:navCon animated:YES];
}
else {
[self.navigationController presentModalViewController:navCon animated:YES];
}
}
The "isPopOver" string is just a placeholder sent from the previous screen that called this TableView (I know I can switch this to a boolean for better performance I just put this together real quick to try it out). I know I messed up somewhere, I just don't know what setting I need to get this working correctly.
You need to define the view controller's modalPresentationStyle to be "current context".
navCon.modalPresentationStyle = UIModalPresentationCurrentContext;
This will result in modal view controller filling the popover like the popover's root controller.
Try using the presentViewController:animated:completion: instead of presentModalViewController:animated: and set self.navigationController.definesPresentationContext = YES
I have created an application that has a Login.
It starts with a Welcome View Controller, checks if the user is logged in if not, opens a Login View Controller. If the user is logged in, or once they have it pushes the user to the Home View Controller like this.
App Delegate (did finish launching)
self.welcomeViewController = [[APPWelcomeViewController alloc] init];
self.homeViewController = [[APPHomeViewController alloc] initWithNibName:#"APPHomeViewController" bundle:nil];
self.navController = [[UINavigationController alloc] initWithRootViewController:self.welcomeViewController];
self.navController.navigationBarHidden = YES;
self.window.rootViewController = self.navController;
Once the user is logged in it pushes the home view.
// Push the homeViewController onto the navController
[self.navController pushViewController:self.homeViewController animated:YES];
This all works fine up to this point. I then use a modal view controller for the Setting, which includes a button to Logout. The logout of the user runs this:
// Log the user out
[User logOut];
// Then we need to remove the Settings Modal View Conrtoller
[self.presentingViewController dismissModalViewControllerAnimated:YES];
// Then we need to take user back to welcomeViewController
[self.navigationController pushViewController:welcomeViewController animated:YES];
It dismisses the Settings View controller as expected, but the navigation controller remains on the Home view. There is no error, does anyone know why this is not working correctly?
Resolved by re-arranging code. Searching for current user on the appdelegate instead then either loading the nav controller with the root controller of home view or running a method to load the welcome view controller.
The welcome view controller then checks for user as well, if not current user it presents the modal login view controller
you dont need to push navigation controller, once you init your window with the navigation controller that contain the welcome view controller.
if you want to add more view inside this stack (inside welcome view controller) you call method self.navigation controller pushviewcontroller ... if you want to remove from stack, call popviewcontroller..you ll go to your main view stack (root).
Try this:
// Take me back to the root navigation view controller (APPWelcomeViewController)
[self.navigationController popToRootViewControllerAnimated:YES];
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.
I am trying to present a modal view controller C from view controller B. B's view does not cover the full screen, and is a subview of another view controller, A. What I am seeing when I try to present a fullscreen modal is the modal is covering the full screen, but when I tap on certain places in the screen the control will get 'passed through' to A's view.
I can bypass this by presenting the modal from A via some kind of delegation, but I have no idea why this is happening! After all, if you have a tab bar controller managing one of your views, and you try to present a modal view, it covers the full screen just fine. Is there some magic going on behind the scenes?
I don't think there is any official documentation on how the modal is implemented, but any view can get the UIWindow of the UIApplication and call -presentModal... on the rootViewController property. This will have the affect of making your modal full screen. I'm sure there are other ways of achieving the same effect though.
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentModalViewController:myModalVC animated:YES];
In that scenario, you need to implement your own 'modal' methods for all your view controllers, using addSubview: and bringSubviewToFront:. I've done this in one of my larger project where I wanted some different behavior from the modal views.
This worked for me:
UIViewController* vc = [[[UIViewController alloc] init] autorelease];
UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView* topView = [[keyWindow subviews] objectAtIndex:[[keyWindow subviews] count] - 1];
vc.view = topView;
[vc presentModelViewController...
Basically, you create a new UIViewController and attach it to the topmost view on the main window (which is the view the user currently sees).
Simple, but not sure about acceptance and efficiency. This is what I use.
Embed all the view controllers in a mainView below the rootView.
Add any modal views to the rootView while disabling userInteractionEnabled flag for the mainView. The modal view should be added to rootView and not mainView
Once done, re-enable the user interaction for mainView.
Hope this helps...