4

好的,我对iOS开发还很陌生,所以我很抱歉如果这是一个愚蠢的问题。试图从AppDelegate访问UINavigationController

但是,我有一个AlertView,我从AppDelegate调用,然后单击警报中的按钮时作出响应。我可以做一个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([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 

这是AppDelegate.h

​​3210

我能够添加与此,但我失去了我的导航控制器:

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

你可以看到我有tr有几种方法,但我很困惑自己试图使用故事板方法。

另外,如果有帮助,ProfileConnection视图在空白时只有一个标签。

+0

你如何创建你的navController? – ader

+0

也请显示您的ProfileConnection代码。 – ader

+0

你的第三个代码片段来自哪里?在你的应用程序委托中,'pushViewController'就是你如何获得一些东西到堆栈中。 – Walter

回答

2

你的代码看起来不错[self.navController pushViewController:profile animated:YES];是你应该怎么做的。

您应该确保您已将导航控制器添加到窗口。也许这应该已经由故事板完成了,但是如果不使用该窗口的rootviewcontroller属性(其优于addSubview)。

self.window.rootViewContorller = self.navController; 

现在做了仔细的检查,以确保没有什么是零(profilenavController)。

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

有帮助吗?

+0

啊是的... self.navController是零。这是否意味着我没有链接故事板中的某些东西?再次感谢您的帮助。非常感谢! – TheTC

+0

这是我第一次遇到这么多麻烦......所以我认为它与故事板有关(因为这是我第一次使用故事板)。只是不知道我错过了什么。也许在IB中连接一些东西? = \ – TheTC

+0

为了记录,我能够通过开始一个新项目而不是使用故事板来通过这个问题。现在一切都很好。我想我还没准备好开始使用故事板。 =) – TheTC

相关问题