好的,我对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
:
我能够添加与此,但我失去了我的导航控制器:
ProfileConnection *profile = [ProfileConnection alloc];
[self.window addSubview:profile.view];
你可以看到我有tr有几种方法,但我很困惑自己试图使用故事板方法。
另外,如果有帮助,ProfileConnection视图在空白时只有一个标签。
你如何创建你的navController? – ader
也请显示您的ProfileConnection代码。 – ader
你的第三个代码片段来自哪里?在你的应用程序委托中,'pushViewController'就是你如何获得一些东西到堆栈中。 – Walter