4

Ok, je suis encore jeune dans le développement iOS, alors je m'excuse si c'est une question idiote.Essayer d'accéder à UINavigationController à partir de AppDelegate

Mais, j'ai un AlertView que j'appelle du AppDelegate puis réponds en cliquant sur un bouton dans l'alerte. Je peux faire un NSLog et voir que les méthodes sont appelées. Mais, ce n'est pas pousser la vue dans la pile. Voici un échantillon de ce que je (je suis sûr que c'est mal):

Ceci est dans le 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 

C'est le 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 

Je suis en mesure d'ajouter la vue avec cela, mais je perds mon contrôleur de navigation:

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

Vous pouvez voir que j'ai tr Il y a quelques approches, mais je m'embrouille en essayant d'utiliser l'approche du storyboard.

En outre, la vue ProfileConnection est vide avec une seule étiquette pour l'instant, si cela vous aide.

+0

Comment créez-vous votre navController? – ader

+0

veuillez également montrer votre code ProfileConnection. – ader

+0

D'où provient votre troisième extrait de code. Dans votre délégué d'application, le 'pushViewController' est comment vous obtenez quelque chose sur la pile. – Walter

Répondre

2

Vous codez l'apparence ok [self.navController pushViewController:profile animated:YES]; est comment vous devriez le faire.

Vous devez vous assurer que vous avez ajouté le contrôleur de navigation à la fenêtre. Peut-être que cela devrait déjà être fait par le storyboard, mais sinon user la propriété rootviewcontroller de la fenêtre (c'est mieux que addSubview).

self.window.rootViewContorller = self.navController; 

Maintenant, faites une vérification de bon sens pour assurer que rien est nul (profile ou navController).

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

Est-ce que cela aide?

+0

Ah oui ... self.navController est nul. Cela signifie-t-il que je n'ai pas lié quelque chose dans le storyboard? Merci encore pour votre aide. C'est très apprécié! – TheTC

+0

C'est la première fois que j'ai autant de problèmes ... donc je pense que ça a quelque chose à voir avec le storyboard (puisque c'est la première fois que j'utilise le storyboard). Je ne sais pas trop ce que je manque. Peut-être relier quelque chose dans IB? = \ – TheTC

+0

Juste pour le compte rendu, j'ai réussi à passer ce problème en démarrant un nouveau projet et en n'utilisant pas le storyboard. Tout fonctionne bien maintenant. Je suppose que je ne suis pas prêt à commencer à utiliser des storyboards. =) – TheTC

Questions connexes