【问题标题】:Trying to access UINavigationController from within AppDelegate尝试从 AppDelegate 中访问 UINavigationController
【发布时间】:2011-12-20 11:49:22
【问题描述】:

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

【问题讨论】:

  • 如何创建导航控制器?
  • 请同时显示您的 ProfileConnection 代码。
  • 你的第三个代码 sn-p 来自哪里。在您的 App Delegate 中,pushViewController 是您将某些内容放入堆栈的方式。
  • 第三个 sn-p 是一段有效的代码(因为它实际上将视图放在堆栈顶部)但我丢失了导航控制器。
  • 导航控制器是在 IB 中使用情节提要创建的。这是我第一次尝试使用故事板,所以我可能会在尝试使用它时感到困惑。

标签: ios iphone uinavigationcontroller appdelegate pushviewcontroller


【解决方案1】:

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

您应该确保已将导航控制器添加到窗口中。也许这应该由情节提要已经完成,但如果不使用窗口的rootviewcontroller 属性(它比addSubview 更好)。

self.window.rootViewContorller = self.navController;

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

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

这些有帮助吗?

【讨论】:

  • 啊是的...... self.navController 是零。这是否意味着我没有在故事板中链接某些内容?再次感谢您的帮助。非常感谢!
  • 这是我第一次遇到这么多麻烦......所以我认为这与故事板有关(因为这是我第一次使用故事板)。只是不确定我错过了什么。也许在 IB 中连接一些东西? =\
  • 只是为了记录,我能够通过开始一个新项目而不使用情节提要来通过这个问题。现在一切都很好。我想我只是还没准备好开始使用故事板。 =)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多