iOS - adding/removing a subview programmatically(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 - 以编程方式添加/删除子视图
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01