以编程方式创建和删除UIViews [英] Creating and Removing UIViews Programmatically
问题描述
我正在学习如何以编程方式操作视图显示,我设法使用以下代码块在appDelegate中显示一个新视图:
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];
请指教..我一直在寻找数小时无济于事,使用Switch Views编程,iPhone视图,removeOperview ..
Please advise.. I've been searching for hours to no avail, using Switch Views Programmatically, iPhone Views, removeSuperview..
基本上我想创建一个简单的登录流程,在app start我将显示我的第一个视图(登录表单),成功登录后我想要丢弃旧视图并显示第二个视图,这是我的主页。
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.
推荐答案
你在使用的UINavigationController
。事实上,你几乎就在那里。
You are on the right track with using the UINavigationController
. In fact, you are almost there.
你已经有两个视图控制器 - 一个用于登录页面,一个用于主页。在 didFinishLaunchingWithOptions:
中,将两个控制器推送到 UINavigationController
的堆栈:首先是home控制器,然后是登录一个。登录控制器检测到登录成功后,调用 popViewControllerAnimated:
或 popToRootViewControllerAnimated:
进入主页。
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.
这篇关于以编程方式创建和删除UIViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!