iPhone - UIActivityIndi​​catorView 停止时不会隐藏

发布于 2024-11-03 02:15:05 字数 2137 浏览 2 评论 0原文

我有一个像这样编写的自定义 UIWebView :

.h

@interface MiniWebViewController : UIViewController {
    NSString* destinationURL;

    UIWebView* webView;
    UIActivityIndicatorView* activityIndicatorView;
}

@property (nonatomic, retain) NSString* destinationURL;

@property (nonatomic, retain) IBOutlet UIWebView* webView;
@property (nonatomic, retain) IBOutlet UIActivityIndicatorView* activityIndicatorView;

- (void) run;

@end

.m

@synthesize destinationURL;
@synthesize webView;
@synthesize activityIndicatorView;

- (id) initWithFrame:(CGRect)frame {
    if (self = [super initWithNibName:@"MiniWebView" bundle:nil]) {
        self.destinationURL = @"";

        self.view.frame = frame;
        self.activityIndicatorView.center = self.webView.center;
    }

    return self;
}

- (void) run {
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.destinationURL]]];
}

它被称为从另一个 ViewController 启动:

- (void) aFunction {
    MiniWebViewController* oneViewController = [[MiniWebViewController alloc] initWithFrame:CGRectMake(/*Some Rect*/];
            oneViewController.webView.tag = i;
            oneViewController.destinationURL = /*SomeURL*/;
            oneViewController.webView.delegate = self;

            [self.view addSubview:oneViewController.view]; /* the Web view is inside this one */
            [oneViewController run];
    }

    - (void) webViewDidFinishLoad:(UIWebView *)webView {
        int webViewID = webView.tag;
        MiniWebViewController* webViewController = [self.webViews objectAtIndex:webViewID];
        [webViewController.activityIndicatorView stopAnimating];
    }

    - (void)webViewDidStartLoad:(UIWebView *)webView {
        int webViewID = webView.tag;
        MiniWebViewController* webViewController = [self.webViews objectAtIndex:webViewID];
        [webViewController.activityIndicatorView startAnimating];
    }

还检查了 Into IB hidesWhenStopped 。一切都是相连的。 IB 中指标的样式设置为“大白色”。

运行时,指标不是大,而是小。
它已成功启动和停止(触发委托调用),但停止时不会隐藏。

有什么问题吗?我没看到...

I have a custom UIWebView written like this :

.h

@interface MiniWebViewController : UIViewController {
    NSString* destinationURL;

    UIWebView* webView;
    UIActivityIndicatorView* activityIndicatorView;
}

@property (nonatomic, retain) NSString* destinationURL;

@property (nonatomic, retain) IBOutlet UIWebView* webView;
@property (nonatomic, retain) IBOutlet UIActivityIndicatorView* activityIndicatorView;

- (void) run;

@end

.m

@synthesize destinationURL;
@synthesize webView;
@synthesize activityIndicatorView;

- (id) initWithFrame:(CGRect)frame {
    if (self = [super initWithNibName:@"MiniWebView" bundle:nil]) {
        self.destinationURL = @"";

        self.view.frame = frame;
        self.activityIndicatorView.center = self.webView.center;
    }

    return self;
}

- (void) run {
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.destinationURL]]];
}

It is called an initied from another ViewController :

- (void) aFunction {
    MiniWebViewController* oneViewController = [[MiniWebViewController alloc] initWithFrame:CGRectMake(/*Some Rect*/];
            oneViewController.webView.tag = i;
            oneViewController.destinationURL = /*SomeURL*/;
            oneViewController.webView.delegate = self;

            [self.view addSubview:oneViewController.view]; /* the Web view is inside this one */
            [oneViewController run];
    }

    - (void) webViewDidFinishLoad:(UIWebView *)webView {
        int webViewID = webView.tag;
        MiniWebViewController* webViewController = [self.webViews objectAtIndex:webViewID];
        [webViewController.activityIndicatorView stopAnimating];
    }

    - (void)webViewDidStartLoad:(UIWebView *)webView {
        int webViewID = webView.tag;
        MiniWebViewController* webViewController = [self.webViews objectAtIndex:webViewID];
        [webViewController.activityIndicatorView startAnimating];
    }

Into IB hidesWhenStopped is also checked. Everything is linked. The style of the indicator is set to "Large White" into IB.

When running, the indicator is not large, but small.
It is successfully started and stopped (delegate calls are triggered), but it doesn't hide when stopped.

What's the problem ? I don't see...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不奢求什么 2024-11-10 02:15:05

仔细查看代码后,您将在 init 方法中修改 UIActivityView。更改它们,以便它们位于您的 viewDidLoad 中。在初始化时,您的视图尚未加载,因此,控制器中尚未创建这些对象的实例。

这些是需要移动的语句:

self.activityIndicatorView.hidesWhenStopped = YES;

self.view.frame = frame;
self.activityIndicatorView.center = self.webView.center;

这可以追溯到 Objective-C 的基础:向 nil 对象发送消息...返回 nil。有时,这是一个烦人的功能,因为没有编译时警告,也没有任何运行时异常——这是该语言的一个功能

After a careful look in your code, you are modifying the UIActivityView in your init method. Change those so that they are in your viewDidLoad. At init, you view is not yet loaded, therefore, there is not an instance of those objects yet created in your controller.

These are the statement that need to be moved:

self.activityIndicatorView.hidesWhenStopped = YES;

self.view.frame = frame;
self.activityIndicatorView.center = self.webView.center;

This goes back to a fundamental of Objective-C: Sending a message to a nil object...returns nil. This is an annoying feature at times because there is no compile time warning, nor is there any runtime exception--a feature of the language.

溇涏 2024-11-10 02:15:05

我很愚蠢,XCode/cocoa 确实没有帮助。

我加载了不存在的错误 XIB 名称。
所以我要求加载不存在的东西,应用程序仍然可以工作,甚至显示和使用不存在的对象。直到一个调用不能按预期工作。没有崩溃...
这是无稽之谈。

Stupid I am, and XCode/cocoa really doesn't help.

I had loaded a wrong XIB name that DOES NOT exist.
So I ask to load something that does not exist, and the app stil works, even showing and using objects that does not exist. Untill one call does not work as expected. No crash...
It's nonsense.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文