Dany
Dany

Reputation: 2290

How can I view subview with an activity indicator?

I need to view a subview with an activity indicator. This is my code but the subview doesn't appear:

@interface ProgressViewController : UIViewController {
    IBOutlet UIActivityIndicatorView *myActivityIndicator;
}
@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *myActivityIndicator;
@end

@implementation ProgressViewController
@synthesize myActivityIndicator;

- (void)viewDidLoad {
   [myActivityIndicator startAnimating];
   [super viewDidLoad]; 
}
- (void)viewWillDisappear:(BOOL)animated {
   [myActivityIndicator stopAnimating];
}
@end


#import "ProgressViewController.h"

@interface MyViewController : UIViewController {
    ProgressViewController *progressViewController;
}

@property (nonatomic, retain) ProgressViewController *progressViewController;
@end

@implementation MyViewController

@synthesize progressViewController

- (void)viewDidLoad
{
    progressViewController = [[ProgressViewController alloc] initWithNibName:@"ProgressViewController" bundle:nil];
    [self.view addSubview:progressViewController.view];
    sleep(4);
    [progressViewController.view removeFromSuperview];

    [super viewDidLoad];
}
@end

Upvotes: 0

Views: 788

Answers (3)

Johan Kool
Johan Kool

Reputation: 15927

Everything in your -viewDidLoad method happens in one runloop. This means that you add and remove the activity indicator without giving the system a chance to actually draw it. The 4 seconds of sleep don't help. Those just make the runloop take longer to finish.

Upvotes: 1

Oded Ben Dov
Oded Ben Dov

Reputation: 10397

There could be several causes, and it's still a bit unclear from the code you sent, which one it is.

First, you shouldn't use sleep(4) in your code - it messes up the application engine iOS runs to support user input, screen refresh, etc. Your code could easily be changed to:

[self performSelector:@selector(removeMyProgressView:) withObject:progressViewController.view afterDelay:4.0];

and have removeFromSuperview in your removeMyProgressView: function.

Also, this line of code is buggy:

progressViewController = [[ProgressViewController alloc] initWithNibName:@"ProgressViewController" bundle:nil];

It should be

self.progressViewController = [[ProgressViewController alloc] initWithNibName:@"ProgressViewController" bundle:nil];

Otherwise you don't call the setter function (@sythesized property), and the object isn't retained. It could be that it is released, and therefore you don't see it.

If this none of this is right, we'll keep pounding at it :)

Good luck!

Oded.

Upvotes: 2

remy_jourde
remy_jourde

Reputation: 300

call [super viewDidLoad] before anything in - (void)viewDidLoad methods

Upvotes: 0

Related Questions