ios – 尝试从AppDelegate中访问UINavigationController

好吧,我对iOS开发还很陌生,所以如果这是一个愚蠢的问题我会道歉.

但是,我有一个我从AppDelegate调用的AlertView然后在单击警报中的按钮时响应.我可以做NSLog并看到方法被调用.但是,它并没有将视图推向堆栈.这是我的样本(我确定这是错的):

这是在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(

) { alertString = @"Declied"; } else if(

) { alertString = @"Connected"; } else if(

) { //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 (

) { alertString = @"Saved It"; } UIAlertView *alertStr = [[UIAlertView alloc] initWithTitle:@"" message:alertString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; if ([alertString isEqualToString:@""]) { } else { [alertStr show]; } } @end

这是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

我可以用这个添加视图,但是我丢失了导航控制器:

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

你可以看到我尝试了一些方法,但是我在试图使用故事板方法时感到困惑.

此外,如果有帮助,ProfileConnection视图是空白的,目前只有一个标签.

最佳答案 你的代码看起来很好[self.navController pushViewController:profile animated:YES];你应该怎么做.

您应该确保已将导航控制器添加到窗口中.也许这应该已经由故事板完成,但如果不是用户窗口的rootviewcontroller属性(它比addSubview更好).

self.window.rootViewContorller = self.navController;

现在进行健全性检查以确保没有任何内容为nil(profile或navController).

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

这有什么帮助吗?

点赞