好的,我對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