Creating and Removing UIViews Programmatically

喜你入骨 提交于 2020-01-06 04:02:48

问题


I am learning how to manipulate view displays programmatically, I manage to display a new view in my appDelegate with the following block of code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

UINavigationController *navController = [[UINavigationController alloc] init];
loginController = [[LoginController alloc] init];
[navController pushViewController:loginController animated:NO];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];

return YES; }

I have added a button in this view that will remove this current view and supposedly programmatically display a new view, however, I only managed to remove the view and not display the new view.

my code to display the second view is the following:

HomeController *homeView = [[HomeController alloc] init];
        [self.window addSubview:homeView.view];
        [homeView.view release];

Please advise.. I've been searching for hours to no avail, using Switch Views Programmatically, iPhone Views, removeSuperview..

Basically I want to create a simple login flow, at app start I will display my first view (login form), after successful login I want to discard the old view and display the second view which is my home page.


回答1:


You are on the right track with using the UINavigationController. In fact, you are almost there.

You already have two view controllers - one for the login page, and one for the home page. In the didFinishLaunchingWithOptions:, push both controllers onto the UINavigationController's stack: first the "home" controller, then the "login" one. Once the login controller detects that the login has been successful, call popViewControllerAnimated: or popToRootViewControllerAnimated: to get to the home page.



来源:https://stackoverflow.com/questions/8711482/creating-and-removing-uiviews-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!