1

I try to create an app with different views. There are two views with one view controller each. The first view is loaded via the rootViewController in the AppDelegate class. Now I want to load the other view as an result of clicking on a button.

I tried to create an instance of the second view controller in my first controller and use the pushViewController method but it didn't worked. The project was build but nothing happend after clicking the button.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Nils Voß
  • 15
  • 1
  • 5
  • how did you instanciate the second view controller ? could you post the pice of code concerned ? –  Sep 14 '11 at 17:17
  • @vincent you need to alloc and initWithNibName on the view controller class you want to push. This answer has some good points: http://stackoverflow.com/questions/1644293/how-to-push-viewcontroller-view-controller – nykash Sep 14 '11 at 18:51
  • @nykash - thanks but I already know that ? –  Sep 14 '11 at 18:53
  • @vincent See my answer below... the question I referenced has examples. – nykash Sep 14 '11 at 18:56

3 Answers3

1

Updated my answer:

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 
PasswordHandling *aPasswortHandler = [[PasswordHandling alloc] init]; 
self.passwordHandling = aPasswortHandler; [aPasswortHandler release]; 
UINavigationController *navigationController;
if ([passwordHandling passwordInKeyChain] == TRUE) { 
    LogInView *logInView = [[[LogInView alloc] initWithNibName: @"Log In View" bundle: nil] autorelease]; 
    navigationController = [[UINavigationController alloc] initWithRootViewController:logInView];
    //[navigationController pushViewController: logInView animated:NO];
} else { 
    CreateNewPasswordView *createNewPasswordView = [[CreateNewPasswordView alloc] initWithNibName:@"CreateNewPassword" bundle: nil]; 
    navigationController = [[UINavigationController alloc] initWithRootViewController:createNewPasswordView];
    //[navigationController pushViewController: createNewPasswordView animated:NO]; 
}

[self.window addSubview:navigationController.view]; 
[self.window makeKeyAndVisible]; return YES; 
Saran
  • 6,274
  • 3
  • 39
  • 48
  • This is my appDelegate Code. PasswortHandler is a class comunicating with the keychain `- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; PasswordHandling *aPasswortHandler = [[PasswordHandling alloc] init]; self.passwordHandling = aPasswortHandler; [aPasswortHandler release]; if ([passwordHandling passwordInKeyChain] == TRUE) { //Passwort in Keychain` – Nils Voß Sep 14 '11 at 19:25
  • `LogInView *logInView = [[[LogInView alloc] initWithNibName: @"Log In View" bundle: nil] autorelease]; [navigationController pushViewController: logInView animated:NO]; } else { CreateNewPasswordView *createNewPasswordView = [[CreateNewPasswordView alloc] initWithNibName:@"CreateNewPassword" bundle: nil]; [navigationController pushViewController: createNewPasswordView animated:NO]; } [self.window setRootViewController:navigationController]; [self.window makeKeyAndVisible]; return YES; }` after starting the iPhone Screen stays white. Whats wrong? – Nils Voß Sep 14 '11 at 19:25
  • couldn't see all code. can you update your question by adding the code? - ignore this comment, as i couldn't delete it. – Saran Sep 14 '11 at 19:28
  • still it should be all in this methode. And this is not my first problem. This is the problem coused by changing to navigation controller – Nils Voß Sep 14 '11 at 19:29
  • thanks. but now i get an error. In the main.m file is an SIGABRT signal. The consol says that "Could not load NIB in bundle 'NSBUNDLE .... with name 'Log In View'" Never got this massage before. ah ok fixed it by changing the string. But the screen stays white. The View Controller works. He worked as rootViewController – Nils Voß Sep 14 '11 at 19:55
  • Does this problem occur on both views? – Saran Sep 14 '11 at 21:02
  • yeah i noticed that the MainWindow gets loaded – Nils Voß Sep 14 '11 at 21:14
  • Usually nib name and viewController name matches, in second case it is CreateNewPasswordView and CreateNewPassword, are you sure they are right? – Saran Sep 14 '11 at 21:20
  • 1
    solved it. I hat to delete this line: `self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; ` – Nils Voß Sep 14 '11 at 21:42
  • Glad you resolved it. In the project in which i tested it doesn't have MainWindow.xib, hence the conflict. – Saran Sep 14 '11 at 21:54
0

You have to put your first viewController in a UINavigationController and set this navigationController as the rootViewController. Then when you click on your button, call (assuming you're in your first controller) :

[self.navigationController pushViewController:secondViewController animated:YES];
Zoleas
  • 4,869
  • 1
  • 21
  • 33
0

Check to make sure your view's button has its target/action setup correctly by right/control-clicking the button to see its connections popup.

Beltalowda
  • 4,558
  • 2
  • 25
  • 33