在窗口子视图中添加和删除图像视图
我正在使用 iPhone 应用程序(LandscapeRight 模式),我在第一页上添加了一个名为 imageView 的图像视图,但我必须在第三页和第四页上再次删除它。请记住,我必须在第五页上再次添加此图像。
我在第一页上添加了带有以下代码的图像:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_logo_medium.png"]];
imageView.frame = CGRectMake(123, 200, 250, 66);
imageView.tag = 800;
imageView.transform = CGAffineTransformMakeRotation(M_PI / 2);
//imageView.window.
[self.parentViewController.view.window addSubview:imageView];
[imageView release];
如何删除并重新添加?
I am working with iPhone app(LandscapeRight Mode), I have added a Image view named imageView on my first page, but I have to delete it again on Third and fourth page. Please remember that I have to add this image again on Fifth page.
I added image with following code on first page :
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_logo_medium.png"]];
imageView.frame = CGRectMake(123, 200, 250, 66);
imageView.tag = 800;
imageView.transform = CGAffineTransformMakeRotation(M_PI / 2);
//imageView.window.
[self.parentViewController.view.window addSubview:imageView];
[imageView release];
How this can be removed and re-added ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您想要从其超级视图调用中删除
imageView
时:请记住,
imageView
在删除时会收到release
,因此您需要确保它如果您打算重用它,则已正确保留
。换句话说,您需要将 ivar 添加到您的控制器类中,您可以在其中保留 imageView 以供重用。实际上,您将它分配给一个局部变量,并在您释放的方法结束时;该变量不应该是本地的,而是控制器的 ivar,以便具有持久性,并且您需要在控制器的dealloc
中释放它。编辑:
我想您在某处有一个 UIViewController ,可以根据需要管理添加和删除子视图。
在这个类中(我不知道如何调用它,因为你没有说),我会声明一个成员来存储子视图:
这里是实现:
所以,在
viewDidLoad
中你创建了imageView并保存在内部以备后用;当需要的时候,通过调用addImageView
来添加;完成后,您可以使用removeImageView
将其删除。如果您的类不是视图控制器,您应该能够对其应用相同的更改。
When you want to remove
imageView
from its superview call:keep in mind that
imageView
receives arelease
when removed, so you need to ensure that it is properlyretain
ed if you plan to reuse it. In other words, you will need to add an ivar to your controller class where you can retain the imageView for reuse. Actually you assign it to a local variable and at the end of the method you release; the variable should not be local, but a controller's ivar so to have persistence, and you will need to release it in your controller'sdealloc
.EDIT:
I suppose that you have a
UIViewController
somewhere that can manage adding and removing subviews as you need it.In this class (I don't know how to call it because you did not say it), I would declare a member to store the subview:
Here the implementation:
So, in
viewDidLoad
you create the imageView and store it internally for later use; When you need it, you add it by callingaddImageView
; when you are done with it, you remove it withremoveImageView
.If your class is not a view controller, you should be able to apply those same changes to it.
塞尔吉奥的建议在这里很好而且正确。我要补充的另一件事是,在大多数情况下,您的窗口应该只有一个子视图。这是因为窗口仅与最近添加的视图的视图控制器协调以管理自动旋转。引用 iOS 视图控制器编程指南:
Sergio's advice is good and correct here. The one other thing I'd add is that in most cases you should only have a single subview of your window. This is because the window coordinates only with the view controller for the most recently added view to manage autorotation. Quoting from the View Controller Programming Guide for iOS: