4

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

4

1 回答 1

2

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

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

self.window.rootViewContorller = self.navController;

现在做一个健全性检查以确保没有任何东西是 nil (profile或 the navController)。

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

这些有帮助吗?

于 2011-11-03T22:37:54.523 回答