iOS - 以编程方式添加/删除子视图

iOS - adding/removing a subview programmatically(iOS - 以编程方式添加/删除子视图)

本文介绍了iOS - 以编程方式添加/删除子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我想添加一个 UIImageView 作为子视图,然后在几秒钟后以启动屏幕的工作方式将其删除.我找到了三种不同的方法来做到这一点,但根据 Objective-C 和 Apple,我不明白哪一种是最好的方法.

Ok I want to add a UIImageView as a subview and then remove it after a couple of seconds in the way a splash screen works. I found three different approaches to do it but I can not understand which one is the best approach according to Objective-C and Apple.

以下是三种不同的方法:

Below are the three different approaches:

1)在我的 MyAppDelegate.h

1) In my MyAppDelegate.h

@interface MyAppDelegate : NSObject <UIApplicationDelegate> {

    MyViewController *myViewController;
    UIImageView *myImageView;
}


@property (nonatomic, retain) IBOutlet MyViewController *myViewController;
@property (nonatomic, retain) IBOutlet UIWindow *window;

@end

在 MyAppDelegate.m 中

and in MyAppDelegate.m

   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    myImageView =[[UIImageView alloc] initWithFrame:CGRectMake(0.0,0.0,self.window.frame.size.width,self.window.frame.size.height)];

        myImageView.image=[UIImage imageNamed:@"Yoga.png"];


    [self.window addSubview:myImageView ];
    [self.window bringSubviewToFront:myImageView];

    [self performSelector:@selector(removeImage) withObject:nil afterDelay:2.5];

    return YES;
}

-(void) removeImage
{
    [myImageView removeFromSuperview];

    [myImageView release];

    [self.window addSubview:myViewController.view];
    [self.window makeKeyAndVisible];
}

2) 在第二种方法中:

2) in the second approach:

In my MyAppDelegate.h


@interface MyAppDelegate : NSObject <UIApplicationDelegate> {

    MyViewController *myViewController;
    UIImageView *myImageView;
}

@property (nonatomic, retain) IBOutlet UIImageView *myImageView;

@property (nonatomic, retain) IBOutlet MyViewController *myViewController;
@property (nonatomic, retain) IBOutlet UIWindow *window;

@end

在 MyAppDelegate.m 中

and in MyAppDelegate.m

@synthesize myImageView;

@synthesize myImageView;

   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    myImageView =[[UIImageView alloc] initWithFrame:CGRectMake(0.0,0.0,self.window.frame.size.width,self.window.frame.size.height)];

        myImageView.image=[UIImage imageNamed:@"Yoga.png"];


    [self.window addSubview:myImageView ];
    [self.window bringSubviewToFront:myImageView];

    [self performSelector:@selector(removeImage) withObject:nil afterDelay:2.5];

    return YES;
}

-(void) removeImage
{
    [myImageView removeFromSuperview];

    [myImageView release];

    [self.window addSubview:myViewController.view];
    [self.window makeKeyAndVisible];
}

- (void)dealloc
{
    [myViewController release];
    [myImageView release];
 }

3) 在第三种方法中:

3) in the third approach:

In my MyAppDelegate.h


@interface MyAppDelegate : NSObject <UIApplicationDelegate> {

    MyViewController *myViewController;

}

@property (nonatomic, retain) IBOutlet MyViewController *myViewController;
@property (nonatomic, retain) IBOutlet UIWindow *window;

@end

在 MyAppDelegate.m 中

and in MyAppDelegate.m

   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIImageView *myImageView =[[UIImageView alloc] initWithFrame:CGRectMake(0.0,0.0,self.window.frame.size.width,self.window.frame.size.height)];

    myImageView.image=[UIImage imageNamed:@"Yoga.png"];
    myImageView.tag=22;    

    [self.window addSubview:myImageView ];

    [myImageView release];

    [self.window bringSubviewToFront:myImageView];

    [self performSelector:@selector(removeImage) withObject:nil afterDelay:2.5];

    return YES;
}

-(void) removeImage
{

    for (UIView *subview in [self.view subviews]) {

    if (subview.tag == 22){

        [subview removeFromSuperview];

    }

}
    [self.window addSubview:myViewController.view];
    [self.window makeKeyAndVisible];
}

- (void)dealloc
{
    [myViewController release];

 }

总结一下..第一种方法不使用 UIImage 的属性,仅使用一个变量,第二种方法使用属性,第三种方法只是创建 UIImage 并将其添加为子视图,然后根据删除它它的标签..

So to sum up.. The first approach does not use a property for the UIImage only a variable, the second one uses a property and the third one just creates the UIImage and adds it as a subview and then removes it based on its tag..

这是正确的方法.我相信所有三个选项听起来都是正确的.但是我应该遵循什么特定的方法.这些选项在内存和性能方面是否更好?

Which is the right approach to follow..I believe that all three options sound right.. But is there any certain way I should follow. Is any of these options better in terms of memory and performance?

提前致谢,

安德烈亚斯

推荐答案

如果不打算再次使用该图像,则无需保留指向它的指针.此外,如果您使用 IBOutlet,您还需要在 IB 中添加视图.在这个具体的例子中,我会说选项 3 最有意义,特别是考虑到通过这个选择,您可以从标准的基于视图的应用程序"模板开始,只需添加有关图像视图的位并离开剩下的一个人.选项 3 的最后一个观察结果;到窗口的 2 条消息;

If you are not going to use the image again, there is no need to keep a pointer to it. Further, if you use IBOutlet, you need to add the view in IB as well. In this specific example I would say option 3 makes the most sence, especially considering that with this choice you can began with a standard "view based application" template and just add the bits about the image view and leave the rest alone. One last observation of choice 3 though; the 2 messages to window;

 [self.window addSubview:myViewController.view];

 [self.window makeKeyAndVisible];

似乎超出了任何方法的范围.这可能只是一个复制和粘贴错误,但请注意它们应位于didFinishLaunchingWithOptions:"中

Appear to be outside the scope of any method. This is likely just a copy and paste error, but make note that they should located within "didFinishLaunchingWithOptions:"

这篇关于iOS - 以编程方式添加/删除子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:iOS - 以编程方式添加/删除子视图

基础教程推荐