iOS - 如何在加载新视图后删除以前的视图?
我正在练习多视图,但遇到了一个小问题。
当我的新视图加载时,我仍然可以看到以前的视图。
如何让之前的视图不可见?
这是我的代码:-
- (IBAction)changeView:(id)sender
{
RedClass *red = [[RedClass alloc] initWithNibName:@"RedClass" bundle:nil];
self.redClass = red;
[red release];
//[self.view removeFromSuperview];
[self.view addSubview:redClass.view];
}
这是屏幕截图:-
当我取消注释 [self .view removeFromSuperview]
那么我的红色视图甚至没有被加载。
任何帮助将不胜感激。
谢谢
I am practicing multiview but having a small problem.
When my new view gets loaded, I can still see previous view.
How to make previos view invisible??
Here is my code :-
- (IBAction)changeView:(id)sender
{
RedClass *red = [[RedClass alloc] initWithNibName:@"RedClass" bundle:nil];
self.redClass = red;
[red release];
//[self.view removeFromSuperview];
[self.view addSubview:redClass.view];
}
and here is a screenshot :-
And when I uncomment [self.view removeFromSuperview]
then my red view is not even getting loaded.
Any help would be appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你的红色视图的颜色可能是透明的。
所以请在你的 redclass 的 nib 文件中检查它。还要检查红色视图的 alpha 值,它必须为 1。
您无法在此处删除第一个视图。因为它可能会在窗口或其他控制器上加载。因此,如果您在视图 1 中添加 view2 并删除 view1 ,那么 view2 会自动被删除,因为它已经在 view1 中。所以你不能写
[self.view removeFromSuperview];
I think color of your red view may be transparant .
so please check it in your nib file of redclass. also check alpha for your red view it must be 1.
You can not remove your first view here. because it may be loaded at window or other controller. so if you are adding view2 in view 1 and removing view1 then view2 automatically get removed ,because it is already in view1 . so you can not write
[self.view removeFromSuperview];
您可以在 .h 文件中声明视图变量。并在 viewDidLoad 中分配它,并且不要在那里释放它。每当你想添加视图时,
[self.view addSubview:red];
,当你想删除时,你可以使用[red removeFromSuperview];
You can declare view variable in .h file. and alloc it in
viewDidLoad
and don't release it there. Whenever you want to add view use,[self.view addSubview:red];
and when you want to remove you can use[red removeFromSuperview];
[self.view removeFromSuperview]
删除您显示的 uiview。因此,您需要为先前加载的视图调用removefromsuperview,并为新加载的视图调用addsubview。
[self.view removeFromSuperview]
removes the uiview which you is displayed.So you need to call removefromsuperview for previously loaded view and call addsubview for new one.