Trying to access the UINavigationController from AppDelegate

Ok, I'm still pretty new to iOS development, so I'm sorry if this is a dumb question.

But I have an AlertView that I call from AppDelegate , then respond when I click a button in the message. I can do an NSLog and see the methods being called. But this does not push the view onto the stack. Here is an example of what I have (I'm sure this is wrong):

This is in AppDelegate.m :

 #import "AppDelegate.h" #import "ProfileConnection.h" @implementation AppDelegate @synthesize window = _window; @synthesize navController; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. return YES; } -(void)switchToController:(NSString *)controller animated:(BOOL)animated{ NSLog(@"switching to controller %@", controller); // maybe we can do a check to see if a subview exists...and then push or pop accordingly. // switch to the "TableView" view if( [controller isEqualToString:@"ProfileConnection"]){ NSLog(@"switching to the ProfileConnection view"); ProfileConnection *profile = [[ProfileConnection alloc] initWithNibName:@"ProfileConnection" bundle:nil]; [self.navController pushViewController:profile animated:YES]; } } -(void)showConnectionFoundAlert { NSString *connectFoundMsg = [[NSString alloc] initWithFormat:@"We found someone we'd think you would like to meet: Tony Davis"]; UIAlertView *connectionFoundAlert = [[UIAlertView alloc] initWithTitle:@"Connection Found" message:connectFoundMsg delegate:self cancelButtonTitle:@"Decline" otherButtonTitles:@"Connect", @"View Profile", @"Save For Later", nil]; [connectionFoundAlert show]; //[connectionFoundAlert release]; } -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; NSString *alertString = [[NSString alloc] initWithFormat:@""]; if([title isEqualToString:@"Decline"]) { alertString = @"Declied"; } else if([title isEqualToString:@"Connect"]) { alertString = @"Connected"; } else if([title isEqualToString:@"View Profile"]) { //alertString = @"Profile Viewed"; //NSLog(@"View Profile is being called"); [self switchToController:@"ProfileConnection" animated:YES]; //UIViewController *profile = [[UIViewController alloc] initWithNibName:@"ProfileConnection" bundle:nil]; //ProfileConnection *profile = [[ProfileConnection alloc] initWithNibName:@"ProfileConnection" bundle:[NSBundle mainBundle]]; //UINavigationController *nav = [[UINavigationController alloc] init]; //[nav pushViewController:profile animated:NO]; /*UIViewController *profile = [[UIViewController alloc] initWithNibName:@"ProfileConnection" bundle:nil]; UINavigationController *navigation = [[UINavigationController alloc] init]; [navigation pushViewController:profile animated:YES];*/ /* ProfileConnection *profile = [ProfileConnection alloc]; //UIView *current = self.window; [self.window addSubview:profile.view]; */ /* [window addSubview:view1.view]; [window makeKeyAndVisible]; - (void)goToNextPage { view2 = [ViewController2 alloc]; UIView *current = self.window; [self.window addSubview:view2.view]; */ } else if ([title isEqualToString:@"Save For Later"]) { alertString = @"Saved It"; } UIAlertView *alertStr = [[UIAlertView alloc] initWithTitle:@"" message:alertString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; if ([alertString isEqualToString:@""]) { } else { [alertStr show]; } } @end 

This is AppDelegate.h :

 #import <UIKit/UIKit.h> #import "ProfileConnection.h" @interface AppDelegate : UIResponder <UIAlertViewDelegate, UIApplicationDelegate, UINavigationControllerDelegate> { UINavigationController *navController; } @property (strong, nonatomic) UIWindow *window; @property (nonatomic, retain) UINavigationController *navController; -(void)showConnectionFoundAlert; -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex; -(void)switchToController:(NSString *)controller animated:(BOOL)animated; @end 

I can add a view with this, but I lost my navigation controller:

 ProfileConnection *profile = [ProfileConnection alloc]; [self.window addSubview:profile.view]; 

You can see that I tried several approaches, but I'm confusing myself when trying to use the storyboard approach.

Also, the ProfileConnection view is empty with a single label at the moment, if that helps.

+4
source share
1 answer

The code looks fine [self.navController pushViewController:profile animated:YES]; how you should do it.

You must make sure that you have added a navigation controller to the window. Perhaps this should already be done by storyboard, but if not the user, the window's rootviewcontroller property (better than addSubview ).

 self.window.rootViewContorller = self.navController; 

Now do a health check to make sure that nothing is zero ( profile or navController ).

 NSLog(@"%@ %@",self.navController, profile); 

Does it help?

+2
source

Source: https://habr.com/ru/post/1379342/


All Articles