presentViewController over TabBarController causes "attempt to present *VC on TabBarVC whose view is not in the window hierarchy" - presentmodalviewcontroller

My root view controller is a Tab Bar Controller loaded in the delegate. Each of the tabs is a table view controller. When the application is first loaded I want a login screen to popup via presentViewController, which I have in my viewDidLoad method of the Tab Bar Controller. It results in the window hierarchy error that I have read about - though none of the solutions have worked for me. I tried instead presenting the modal view in the viewDidLoad method of the first tab but this resulted in the same error.
In my tab bar view controller in viewDidLoad, my code is:
CLLoginViewController *loginVC = [[CLLoginViewController alloc] init];
loginVC.delegate = self;
[self.view addSubview:loginVC.view];
[self presentViewController:loginVC animated:NO completion:nil];

Your question is quite vague but what i have understood, You want to present a login screen before the tabController is loaded.
So a better way would be, In your AppDelegate when setting rootViewController of window, do something like this
if([[NSUserDefaults standardUserDefaults] boolForKey:#"showTabBar"]){
self.window.rootViewController = tabBarControllerObject; //TabBarView is loaded
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"showTabBar"];
}
else{
self.window.rootViewController = tabBarControllerObject; //Login Screen is loaded for first time.
}
Hope this helps

Related

dismiss rootViewController on button tap

I have a viewController instantiated by storyboard on application launch which is a part of hybrid app. On top of that I have presented one more view controller. Now While dismissing the presentedViewController I want to dismiss the rootViewController as well so as to show the hybrid app screen. How can I achieve this?
[self dismissViewControllerAnimated:YES completion:^{
// after your second view controller dismissed.
// set your new view controller as a root of window.
// You need to set navigation controller and set any root view controller for that navigation controller in storyboard.
// also don't forget to set identifier of your navigation controller.
UINavigationController* rootController = [[UIStoryboard storyboardWithName:kStoryboardName bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:#"controllerIdentifier"];
UIWindow* window = [[UIApplication sharedApplication] keyWindow];
window.rootViewController = rootController;
// this will set your new navigation controller with root view on UIWindow.
}];
OK, I don't know anything about Worklight. Or what a hybrid app is. So this may not make sense. But here's a strictly iOS answer to your question.
It doesn't really make sense to dismiss the root view controller without replacing it with another view controller. If you do, you would leave your app with no way to interact with it (except maybe shaking the phone?).
So there isn't a way to dismiss it as you can with child view controllers. But you can just remove it.
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
[keyWindow.rootViewController.view removeFromSuperview];
keyWindow.rootViewController = nil;
NSMutableArray *viewControllers = [self.navigationController.viewControllers mutableCopy];
[viewControllers removeObjectAtIndex:0];
[self.navigationController setViewControllers:viewControllers];

iOS7 - popToRootViewControllerAnimated not doing anything

I have looked around but haven't found a satisfying answer. My problem is that whenever I call popToRootViewControllerAnimated:(BOOL) it is not doing anything. When I NSLog it, it logs (null).
Let me back up a bit here. I have a table view controller that has a list of things, at the navigation bar up top there is an option to add and that takes me to a new view controller with a segue "Present as PopOver" which gets rid of the principal or main navigation bar. So I made one manually and added 2 bar button items "Cancel" and "Add". When "Cancel" is tapped, it should take the user back to the table view controller and discard changes, when "Add" button is tapped, it should also take user back to the previous table view controller with the changes. But it's not doing anything.
Here is my code.
- (IBAction)cancelButton:(UIBarButtonItem *)sender {
UINavigationController * navigationController = self.navigationController;
NSLog(#"%#", navigationController);
NSLog(#"cancel tapped though");
ListingTableViewController *rootController = [[ListingTableViewController alloc] init];
[navigationController popToRootViewControllerAnimated:NO];
[navigationController pushViewController:rootController animated:YES];
}
As far as the segue, this view controller is not connected to anything, or should I connect it? This is a noobish question indeed. Here is my xcode screenshot.
Check this link for the screenshot of the storyboard
http://i.stack.imgur.com/lqnCF.png
You must call
- (IBAction)cancelButton:(UIBarButtonItem *)sender {
NSLog(#"cancel tapped though");
[self dismissViewControllerAnimated:YES completion:nil];
}
instead of popToRootViewControllerAnimated because your VC presented and not pushed!
When presenting a view, you are not pushing it in your navigation controller, but having it presented. To dismiss it, try using [self.presentingViewController dismissViewControllerAnimated:NO completion:nil].

navigating from popup to popup in objective c

I am working with Objective C using xcode and SUP 2.1.3 as backend.I am verymuch new to the technology.I have created a project with master detail as the design template.In that in the detail view I am using a popup to display some details.And also I have a button over there. When I click in this button I have to go to the next UIView in popup itself.
For that I have created two more UIViewControllers (viewController1 and viewController2).
And in detailview I have written the code for a popup like,
-(void)popupAgain
{
ViewController1 *viewController = [[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil];
viewController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:viewController animated:YES];
[viewController release];
}
And I put a button over their in that popup, I am calling the next popupAgain funcion to get viewController2 like
-(IBAction)next:(id)sender
{
[self popupAgain];
}
-(void)popupAgain
{
ViewController2 *viewController_new = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
viewController_new.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:viewController animated:YES];
[viewController_new release];
}
Now the problem is when I click on the next button I have to dismiss the first popup and display the second.But I am not able to dissmiss the first one even if I am writing the code [self dissmissModalViewControllerAnimated:YES]; in the action for next button like,
-(IBAction)next:(id)sender
{
[self dissmissModalViewControllerAnimated:YES];
[self popupAgain];
}
Please anyone help me to solve this issue.Or do you have any other idea regarding this?I am very much new to the technology.Thank you very much for any help in advance.
See here:
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html#//apple_ref/doc/uid/TP40007457-CH111-SW1
Dismissing a Presented View Controller
When it comes time to dismiss a presented view controller, the preferred approach is to let the presenting view controller dismiss it. In other words, whenever possible, the same view controller that presented the view controller should also take responsibility for dismissing it. Although there are several techniques for notifying the presenting view controller that its presented view controller should be dismissed, the preferred technique is delegation. For more information, see “Using Delegation to Communicate with Other Controllers.”

Objective C: How to present modal view controller from appdelegate?

I am in the appdelegate of my application. How can I add a modal view controller in the "didfinishlaunching" method?
I tried the following but did not work
SomeViewController *vc = [[SomeViewController alloc]init];
[self.tabController.navigationController presentModalViewController:vc animated:NO];
EDIT:
I changed my implementation to the following
self.tabController.selectedViewController
= [self.tabController.viewControllers objectAtIndex:0];
SomeViewController *vc = [[SomeViewController alloc]init];
[self.tabController.selectedViewController presentModalViewController:vc animated:NO];
I checked that the 'selected view controller' is not null... however I am still not able to get the output I needed. Is there anything I am missing?
Assuming tabController and navigationController are not nil, the applicationDidFinishLaunching may be too soon to display the modal view controller.
Make sure you put that code after you make the window key and visible. [self.window makeKeyAndVisible];
If that does not work try listening for the UIWindowDidBecomeKeyNotification for that window
You can try delaying presentation of that modal a few seconds using performSelector:withObject:afterDelay:

viewController management programmatically

I have a problem about viewController. I created a program What is viewController based applicaiton. There is 4 button on mainViewController. I used this code for calling mainviewController
-(void) applicationDidFinishLaunching:(UIApplication *)application{
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
}
Then click to any button on homePage , I go to the other viewController. This code for call another viewController belong
-(IBAction)clickCalendarButton{
calendarButton.selected=YES;
[calendarButton
setImage:[UIImage imageNamed:#"afvalkalender_pressed.png"] forState:(UIControlStateHighlighted+UIControlStateSelected)];
GarbageCalendar *garbageCalendar = [[GarbageCalendar alloc] initWithNibName:#"GarbageCalendar" bundle:nil];
[self presentModalViewController:garbageCalendar animated:YES];
}
And then I want to go home page from another viewController. But I didn' go home page viewController.
Create button on detail view controller, which calls something like this:
- (IBAction)goBack {
[self dismissModalViewControllerAnimated:YES];
}
If you want to keep your current UI design, based on modal view controllers, then I think you should ensure that your other view controllers have got a button that does the dismiss of the view. Say, e.g., a "Back" or "Done" button. When you click on that button, a delegate method is called that executes: [self dismissModalViewControllerAnimated:YES];
Look also at this document for more info, section "Dismissing a Modal View Controller".
If you would like to consider alternative approaches to your UI, you could look into using a UINavigationController, which would make your life a little bit easier with navigating back from one controller to another.