4

좋아, 아직 iOS 개발에 익숙하지 않으므로 바보 같은 질문 인 경우 사과드립니다.AppDelegate에서 UINavigationController에 액세스하려고 시도합니다.

그러나 에서 전화를 걸어서 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]; 

내가 볼 수있는 tr 몇 가지 접근 방식을 사용했지만 스토리 보드 방식을 사용하려고 혼란 스럽습니다.

또한 ProfileConnection보기는 비어 있으며 현재 도움이되는 경우 단일 레이블이 비어 있습니다.

+0

navController를 어떻게 만듭니 까? – ader

+0

ProfileConnection 코드도 함께 표시하십시오. – ader

+0

세 번째 코드 발췌 문장의 출처는 어디입니까? App Delegate에서'pushViewController'는 스택에 무언가를 얻는 방법입니다. – Walter

답변

2

코드가 정상적으로 보입니다. [self.navController pushViewController:profile animated:YES];은 어떻게해야할까요?

창에 탐색 컨트롤러를 추가했는지 확인해야합니다. 아마도 이것은 스토리 보드에 의해 이미 이루어져야하지만 사용자가 아니라면 rootviewcontroller 윈도우의 속성입니다 (이 값은 addSubview보다 낫습니다).

self.window.rootViewContorller = self.navController; 

지금 없는지 확인하기 위해 온 전성 검사를 할 무기 호입니다 (profile 또는 navController).

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

그 중 하나가 도움이됩니까?

+0

Ah yes ... self.navController는 nil입니다. 그게 내가 스토리 보드에 뭔가를 연결시키지 않았다는 의미인가? 도움에 다시 한번 감사드립니다. 매우 감사드립니다! – TheTC

+0

내가 처음으로 스토리 보드를 사용했기 때문에 스토리 보드와 관련이 있다고 생각합니다. 내가 무엇을 놓치고 있는지 확실하지 않습니다. 어쩌면 IB에서 뭔가를 연결하고 있을까요? = \ – TheTC

+0

기록을 위해 스토리 보드를 사용하지 않고 새 프로젝트를 시작하여이 문제를 해결할 수있었습니다. 모든 것이 잘되고 있습니다. 스토리 보드를 사용할 준비가되지 않은 것 같습니다. =) – TheTC

관련 문제