简体   繁体   English

如何查看带有活动指示器的子视图?

[英]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

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: 首先,您不应该在代码中使用sleep(4)-它弄乱了iOS运行的应用程序引擎以支持用户输入,屏幕刷新等。您的代码可以轻松更改为:

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

and have removeFromSuperview in your removeMyProgressView: function. 并在removeMyProgressView:函数中具有removeFromSuperview

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. 否则,您不会调用setter函数(@sythesized属性),并且不会保留该对象。 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. -viewDidLoad方法中的所有内容都在一个运行循环中发生。 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. 4秒钟的睡眠无济于事。 Those just make the runloop take longer to finish. 这些只会使运行循环花费更长的时间才能完成。

- (void)viewDidLoad方法中的任何内容之前调用[super viewDidLoad]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM