【发布时间】:2011-11-14 11:18:02
【问题描述】:
好的,我想添加一个 UIImageView 作为子视图,然后在几秒钟后以启动屏幕的工作方式将其删除。我找到了三种不同的方法来做到这一点,但根据 Objective-C 和 Apple,我不明白哪一种是最好的方法。
以下是三种不同的方法:
1) 在我的 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 中
- (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) 在第二种方法中:
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 中
@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) 在第三种方法中:
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 中
- (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 并将其添加为子视图,然后根据删除它它的标签..
这是正确的方法。我相信所有三个选项听起来都是正确的。但是有什么我应该遵循的特定方法吗?这些选项在内存和性能方面是否更好?
提前致谢,
安德烈亚斯
【问题讨论】:
-
技术问题不谈 人为地延迟用户使用您的应用程序的启动屏幕是糟糕的移动设计,也不是 iPhone HIG 所建议的。
-
问题不在于启动画面。这是关于选择正确的方式来实现某些东西。这个例子可能是一个与启动屏幕完全无关的问题!这是关于添加子视图并稍后将其删除的正确方法..
-
第一种方法似乎是正确的,但您不需要初始化 UIImageView,因为它是一个 Outlet,只需在您想要的延迟时间后触发该方法并将其从可以是您的 viewController 的 superView 中删除/跨度>
标签: iphone objective-c ios uiimageview subview