4

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

​​3210

我能夠添加與此,但我失去了我的導航控制器:

ProfileConnection *profile = [ProfileConnection alloc]; 
[self.window addSubview:profile.view]; 

你可以看到我有tr有幾種方法,但我很困惑自己試圖使用故事板方法。

另外,如果有幫助,ProfileConnection視圖在空白時只有一個標籤。

+0

你如何創建你的navController? – ader

+0

也請顯示您的ProfileConnection代碼。 – ader

+0

你的第三個代碼片段來自哪裏?在你的應用程序委託中,'pushViewController'就是你如何獲得一些東西到堆棧中。 – Walter

回答

2

你的代碼看起來不錯[self.navController pushViewController:profile animated:YES];是你應該怎麼做的。

您應該確保您已將導航控制器添加到窗口。也許這應該已經由故事板完成了,但是如果不使用該窗口的rootviewcontroller屬性(其優於addSubview)。

self.window.rootViewContorller = self.navController; 

現在做了仔細的檢查,以確保沒有什麼是零(profilenavController)。

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

有幫助嗎?

+0

啊是的... self.navController是零。這是否意味着我沒有鏈接故事板中的某些東西?再次感謝您的幫助。非常感謝! – TheTC

+0

這是我第一次遇到這麼多麻煩......所以我認爲它與故事板有關(因爲這是我第一次使用故事板)。只是不知道我錯過了什麼。也許在IB中連接一些東西? = \ – TheTC

+0

爲了記錄,我能夠通過開始一個新項目而不是使用故事板來通過這個問題。現在一切都很好。我想我還沒準備好開始使用故事板。 =) – TheTC

相關問題