开发者

Switch between views in iOS

开发者 https://www.devze.com 2023-04-05 02:00 出处:网络
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 开发

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 开发者_JAVA技巧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.


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; 


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];


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号