在窗口子视图中添加和删除图像视图

发布于 2024-11-23 17:03:52 字数 527 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

攒眉千度 2024-11-30 17:03:52

当您想要从其超级视图调用中删除 imageView 时:

[imageView removeFromSuperview];

请记住,imageView 在删除时会收到 release,因此您需要确保它如果您打算重用它,则已正确保留。换句话说,您需要将 ivar 添加到您的控制器类中,您可以在其中保留 imageView 以供重用。实际上,您将它分配给一个局部变量,并在您释放的方法结束时;该变量不应该是本地的,而是控制器的 ivar,以便具有持久性,并且您需要在控制器的 dealloc 中释放它。

编辑:

我想您在某处有一个 UIViewController ,可以根据需要管理添加和删除子视图。

在这个类中(我不知道如何调用它,因为你没有说),我会声明一个成员来存储子视图:

@interface MyViewController {
    ...
    UIImageView* imageView;
    ...
}
....
@end

这里是实现:

@implementation MyViewController;
...

-(void)viewDidLoad {
    [super viewDidLoad];
    ....
    [self createImageView];
    ....
}

- (void) createImageView {
  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);
}

- (void)addImageView {
    [self.parentViewController.view.window addSubview:imageView];
}

- (void) removeImageView {
   [imageView removeFromSuperview];
}

-(void)dealloc {
    ...
    [imageView release];
    ...
}

所以,在viewDidLoad中你创建了imageView并保存在内部以备后用;当需要的时候,通过调用addImageView来添加;完成后,您可以使用 removeImageView 将其删除。

如果您的类不是视图控制器,您应该能够对其应用相同的更改。

When you want to remove imageView from its superview call:

[imageView removeFromSuperview];

keep in mind that imageView receives a release when removed, so you need to ensure that it is properly retained 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's dealloc.

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:

@interface MyViewController {
    ...
    UIImageView* imageView;
    ...
}
....
@end

Here the implementation:

@implementation MyViewController;
...

-(void)viewDidLoad {
    [super viewDidLoad];
    ....
    [self createImageView];
    ....
}

- (void) createImageView {
  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);
}

- (void)addImageView {
    [self.parentViewController.view.window addSubview:imageView];
}

- (void) removeImageView {
   [imageView removeFromSuperview];
}

-(void)dealloc {
    ...
    [imageView release];
    ...
}

So, in viewDidLoad you create the imageView and store it internally for later use; When you need it, you add it by calling addImageView; when you are done with it, you remove it with removeImageView.

If your class is not a view controller, you should be able to apply those same changes to it.

剪不断理还乱 2024-11-30 17:03:52

塞尔吉奥的建议在这里很好而且正确。我要补充的另一件事是,在大多数情况下,您的窗口应该只有一个子视图。这是因为窗口仅与最近添加的视图的视图控制器协调以管理自动旋转。引用 iOS 视图控制器编程指南:

在 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:

In an iOS application, the window object does much of the work
associated with changing the current orientation. However, it works in
conjunction with the application’s view controllers to determine
whether an orientation change should occur at all, and if so, what
additional methods should be called to respond to the change.
Specifically, it works with the view controller whose root view was
most recently added to, or presented in, the window. In other words,
the window object works only with the frontmost view controller whose
view was displayed using one of the mechanisms described in
“Presenting a View Controller’s View.”

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文