试图从AppDelegate中访问UINavigationController

时间:2011-11-02 16:25:23

标签: ios iphone uinavigationcontroller appdelegate pushviewcontroller

好的,我还是iOS开发的新手,所以如果这是一个愚蠢的问题我会道歉。

但是,我从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

#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视图是空白的,目前只有一个标签。

1 个答案:

答案 0 :(得分:2)

您的代码看起来不错[self.navController pushViewController:profile animated:YES];就是您应该这样做的。

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

self.window.rootViewContorller = self.navController;

现在进行健全性检查以确保没有任何内容为零(profilenavController)。

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

有没有帮助?