Showing view controller from AppDelegate when notification arrived - ios

I am tring to show UIViewController which is inside the UIStoryboard. There isn't any problem about poping up the view. But the navigation is not working in the popup viewcontroller; such as when I touch back button
[self.navigationController popViewControllerAnimated:YES];
I am using that code in AppDelegate --> didReceiveRemoteNotification method
UIStoryboard *mystoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
PFProfileInfoVC *viewcontroller = [mystoryboard instantiateViewControllerWithIdentifier:#"SBID_ProfileVC"];
viewcontroller.strAuthorID = [userInfo objectForKey:#"aps"][#"targetId"];
[self.window.rootViewController presentViewController:viewcontroller animated:YES completion:nil];
Thanks,

When you use presentViewController, the viewController is not pushed onto the navigation stack. Normally, it is presented modally. So if you want to dismiss it, use
[self dismissViewControllerAnimated:true completion:nil];

Related

Present a ViewController that is embedded in Navigation Controller

I have a situation wherein a storyboard made VC(embedded in NavController) should be presented programmatically.
SomeVC -> presents NavController(rootVC) -> rootVC -> pushes subVC
on this representation, subVC should have a back button to go back to rootVC, but I can't implement it this way. Will be providing sample codes that I have already tried.
this pushes the rootVC directly:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"storyBoard" bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"rootVC"];
[self.navigationController presentViewController:vc animated:YES completion:nil];
also tried pushing the navigationController itself, ID has been set on storyboard:
UINavigationController *navController = [storyboard instantiateViewControllerWithIdentifier:#"navigationController"];
[self presentViewController:navController animated:YES completion:nil];
EDIT: storyboard implementation looks like this
I'm not sure if I understand you correctly. You want to present a navigation controller and have those rootVC and subVC already in it, right? So after presenting, you want the subVC to be presented with the back button right away.
If that's the case you need to tell the navigation controller to push the subVC before presenting it
UINavigationController *navController = [storyboard instantiateViewControllerWithIdentifier:#"navigationController"];
UIViewController *subVC = [storyboard instantiateViewControllerWithIdentifier:#"subVC"];
[navController pushViewController:subVC animated:NO];
[self.navigationController presentViewController:vc animated:YES completion:nil];

Warning “attempt to present ViewController whose view is not in the window hierarchy - Objective C

I'm trying to call a controller, if has response error, and redirect the user for login controller.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"EventManagerStoryboard" bundle:[NSBundle mainBundle]];
LoginViewController *loginController = [storyboard instantiateViewControllerWithIdentifier:#"loginID"];
[loginController viewDidLoad];
loginController.showOnlyLoginForm = YES;
[self presentViewController:loginController animated:YES completion:Nil];
and I facing this warning -
Attempt to present Attempt to present LoginViewController: 0x7fc958201130 on ProfileController: 0x7fc9583118e0 whose view is not in the window hierarchy!
Don't run this code in viewDidLoad try it in viewDidAppear
[self presentViewController:loginController animated:YES completion:Nil];
Edit:
use in appDelegate if you implement navigationController
[(UINavigationController *)self.window.rootViewController pushViewController:vc animated:YES];

Dismissing (or popping) a NavigationController I added manually does not work

In my AppDelegate I have the following code which is executed after receiving a notification:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *navigationController = (UINavigationController *)[storyboard instantiateViewControllerWithIdentifier:#"VideoPlayback"];
VideoPlaybackViewController *videoPlaybackViewController = (VideoPlaybackViewController *)[navigationController topViewController];
videoPlaybackViewController.publishing = YES;
[(UINavigationController*)self.window.rootViewController pushViewController:navigationController animated:NO];
That successfully brings up the new ViewController and apparently adds it to the navigation stack, since I can use the back button on the navigation bar to go back and subsequently dismiss the view controller.
The problem is, I don't want to use the navigation bar. In fact, I would like to hide the back button. Unfortunately, when I try to dismiss the viewcontroller using the method(s) it should use, it does nothing. I've tried using both of these to dismiss the view controller:
[self dismissViewControllerAnimated:YES completion:nil];
[self.navigationController popViewControllerAnimated:YES];
What am I doing wrong? Thanks.
You're trying to push a navigation controller into a navigation controller, which won't end well.
[(UINavigationController*)self.window.rootViewController pushViewController:navigationController animated:NO];
probably needs to be changed to:
[(UINavigationController*)self.window.rootViewController pushViewController:videoPlaybackViewController animated:NO];

How to dismiss view controller if it presented from app delegate?

So I'm trying to figure out deeplinking. I have successfully gotten my app to recognize and run code I want it to depending on the path in the URL. Now when I present a View, it shows but can't get the back button or any way to dismiss it to work. Here's the code in my appdelegate:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
BuyPremiumViewController *premiumViewController = [storyboard instantiateViewControllerWithIdentifier:#"BuyPremium"];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:premiumViewController];
[self.window makeKeyAndVisible];
[self.window.rootViewController presentViewController:navigationController animated:YES completion:NULL];
The reason I use a UINavigationController is to have the nav title and back button show in the presented view. Thing is, it is not working. Here is the code that runs when the back button is tapped in the presented view:
- (void)backButton {
NSLog(#"this ran"); //check if actually ran
[self.navigationController popToRootViewControllerAnimated:YES];
}
Being at it for a hours now, help?
Try to use this code:
- (void)backButton
{
NSLog(#"this ran"); //check if actually ran
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

Removing ViewController after showing modally a second ViewController

Guys in my app I have some code in the app delegate method application:didFinishLaunchingWithOptions: that determines if the initial View Controller should be the LoginViewController or the MainViewController.
If the LoginViewController is showed first and the user logs in successfully I show the MainViewController modally with this piece of code:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
FSMainViewController *vc = (MainViewController *)[storyBoard instantiateViewControllerWithIdentifier:#"MainViewController"];
vc.loginViewController = self;
[self presentViewController:vc animated:YES completion:nil];
What I want to do next, after the MainController is showed on the screen, is remove the LoginViewController from memory so in the viewWillApper:animated: method of the MainViewController I use this code to remove (or at least try to) the LoginViewController:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (self.loginViewController) {
[self.loginViewController dismissViewControllerAnimated:NO completion:nil];
}
}
Problem is that this code leads to strange behaviors like the MainViewController being removed from the screen and this error message showing up in the console.
Unbalanced calls to begin/end appearance transitions for <LoginViewController: 0xb06e350>
I also tried calling [self dismissViewControllerAnimated:NO completion:nil] in the completion block of the presentViewController:animated:completion method but still no luck, it didn't work.
What am I doing wrong? How can I remove from memory the underlying LoginViewController when the MainViewController is presented modally?
Don't present your main view controller if you want the login controller to go away, just make it the window's root view controller.
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
FSMainViewController *vc = (MainViewController *)[storyBoard instantiateViewControllerWithIdentifier:#"MainViewController"];
Self.window.rootViewController = VC;
You can't dismissViewController after presenting another one on it or its presentingViewController. At here, you should dismiss LoginViewController first, then present MainViewController.
Otherwise, if you'd like pushViewController, you can call [self.navigationController setViewControllers: animated:] to remove LoginViewController.
If you think presentingViewController is just what you want, try something like this in application:didFinishLaunchingWithOptions:
if (self.loginViewController) { //Define loginViewController in appDelegate.h
[self dismissViewControllerAnimated:NO completion:^{
[self presentViewController:mainViewController animated:YES completion:nil];
}];
}
else{
[self presentViewController:mainViewController animated:YES completion:nil];
}

Resources