开发者

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
{
    progressViewContro开发者_JAVA百科ller = [[ProgressViewController alloc] initWithNibName:@"ProgressViewController" bundle:nil];
    [self.view addSubview:progressViewController.view];
    sleep(4);
    [progressViewController.view removeFromSuperview];

    [super viewDidLoad];
}
@end


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜