13

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) 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

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) 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

and in 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 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

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];

 }

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?

Thanks in advance,

Andreas

andreasv
  • 689
  • 3
  • 11
  • 26
  • 1
    Technical question aside Having splash screens that artifically delay the user getting to use your application is poor mobile design and is not what's suggested by the iPhone HIG. – Nick Sep 10 '11 at 18:19
  • 3
    the question is not about splash screen. It is about choosing the right way to implement something..This example could be a question to something completely irrelevant with splash screens! It is about the right way to add a subview and later remove it.. – andreasv Sep 10 '11 at 18:28
  • First approach seems right but you don't need to initialize your UIImageView since it is an Outlet, just fire the method after the delay time you want and remove it from the superView which can be your viewController – Herz Rod Sep 10 '11 at 18:59

2 Answers2

8

You could use an animation attached to the view's layer. Code below fades the view out - but there are many other ways to remove it. (you need to attach the QuartzCore framework)

myImageView.layer.opacity = 0.0;
// this is the state the view will be in after the animation (e.g. invisible)

CABasicAnimation *theFade;

theFade = [CABasicAnimation animationwithKeyPath:@"opacity"];
theFade.duration = 10.0; 
theFade.fromValue = [NSNumber numberWithFloat:1.0]; // i.e. visible
theFade.toValue = [NSNumber numberWithFloat:0.0]; // i.e. invisible 
[myImageView.layer addAnimation:theFade forKey:@"animateOpacity"]; 
TCR Dave
  • 81
  • 1
  • 2
6

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];

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

DShah
  • 9,768
  • 11
  • 71
  • 127
OutlawAndy
  • 316
  • 2
  • 4