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

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:

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];

removeFromParentViewController doesnot update UINavigationbar

I want to remove all the viewcontrollers from UINavigationController. So I am using this code.
for (UIViewController* controller in navigationController.viewControllers) {
[controller removeFromParentViewController];
}
After that I create an new viewController and push it.
UIViewController* newVC=[[UIViewController alloc] init];
[navigationController pushViewController:newVC animated:YES];
Issue is all the viewcontrollers popout perfectly and adding newVC but on pushing newVC the navigationbar is getting a back button and title of newVC. On clicking back button it animates to the navigationbar of oldVC with title of oldVC that I have already removed in above loop;
removeFromParentViewController is a UIViewController method, so it's normal it has nothing to do with UINavigationBar
In the case of a UINavigationController the popViewControllerAnimated: method handles the removeFromParentViewControllerpart for you, along with navigation bar.
you can directly update the whole array of viewControllersof UINavigationController, calling `setViewControllers:animated:
see Replacing rootView in navigationController
[navigationController setViewControllers:[NSArray arrayWithObject:newVC]];

Dismissing a presented UINavigationController in iOS6

In my root UIViewController I present a UINavigationController. The UINavigationController ultimately pushes another view (let's call it MyViewController). How can I dismiss the entire UINavigationController (and the views it has pushed) from MyViewController?
From MyViewController, I've tried calling [[self.navigationController presentingViewController] dismissViewControllerAnimated:NO completion:nil] but nothing happend. I've also tried first popping all the view controllers off the navigation controller but, still, the navigation controller was not dismissed.
Thanks!
UPDATE:
I've tracked down what's causing this issue but haven't been able to solve it. The UINavigationController described above is a custom subclass of UIImagePickerController (we'll call it CameraController) with source type UIImagePickerControllerSourceTypeCamera. Everything works as expected (using the answer provided below) until I set a custom cameraOverlayView on the CameraController in viewDidLoad. Commenting out the following lines gets things working:
self.showCameraControls = NO;
UIView *overlay = [[[NSBundle mainBundle] loadNibNamed:#"CameraController" owner:self options:nil] objectAtIndex:0];
self.cameraOverlayView = overlay;
You can call dismissViewControllerAnimated:NO on your self.navigationController itself.
[self.navigationController dismissViewControllerAnimated:NO completion:nil];

how to access the UITabBarController when its not the initial view?

I'm having problem with accessing my tabbarcontroller in the storyboard when its not the initial view. So basically there is an initial view in the storyboard which leads to the tabbarcontroller. I want to change the color of the tab but i dont have access to it! I know that it can be added to the delegate if its the initial view but in this case its not the initial view in the storyboard! I read somewhere that I have to override a method in the first view but there was no detail about it!
If you are in a viewcontroller in the storyboard then this code:
UIStoryboard *storyboard = self.storyboard;
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"OtherViewController"];
Will get the other viewcontroller youw want and you could do things such as:
set yourself as delegate for it:
[(OtherViewController *)vc setDelegate:self];
display it:
[self presentViewController:vc animated:YES completion:nil];
I think you could use this to get the other viewcontroller and then access the tabbar from it.
Sorry if I misunderstood what you are trying to do.

EXC_BAD_ACCESS at removeFromSuperview - using ARC

In one of my ViewControllers (ViewController A), I have the following code:
AlertViewController *aViewController = [[AlertViewController alloc] initWithNibName:#"AlertViewController" bundle:nil];
[self.view addSubview:[aViewController view]];
[self.view bringSubviewToFront:[aViewController view]];
And in AlertViewController, I have a button and when the user clicks on it, I have:
[self.view removeFromSuperview];
Whenever I click the button, the result is EXC_BAD_ACCESS.
I'm unable to figure out the problem. My project is using ARC and ViewController A is part of a navigation controller stack if that info helps.
The problem here is that the UIView doesn't own its UIViewController. In the first block of code you held the UIView around by adding it to a subview, but let the UIViewController go away. The UIView from a UIViewController is special, you can't let this happen.
Make sure the UIViewController that created the UIView lives as long as the view does.