1

在选择每个选项卡并加载初始选项卡后,我试图在屏幕中央添加一个“加载”指示器。我发现许多帖子显示了完成此操作的代码,但它似乎不适用于我的 webview 应用程序的设置方式。

我是一个新手,我的所有代码都来自不同的教程,所以如果你能解释一下把代码放在哪里会有帮助。

这是我当前的 .h 文件

 #import <UIKit/UIKit.h>

@interface ViewController1 : UIViewController

{
    IBOutlet UIWebView *myWebView;
}

@end

这是我当前的 .m 文件

#import "ViewController1.h"

@interface ViewController1 ()

@end

@implementation ViewController1


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Home", @"Home");
        self.tabBarItem.image = [UIImage imageNamed:@"birdhouse"];
    }
    return self;
}



- (void)viewDidLoad {

    [super viewDidLoad];


    {
        UIImage *navigationBarBackground = [[UIImage imageNamed:@"navbar.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

        [[UINavigationBar appearance] setBackgroundImage:navigationBarBackground forBarMetrics:UIBarMetricsDefault];

    }


    // Do any additional setup after loading the view from its nib.

    NSURL *myURL = [NSURL URLWithString:@"http://jbirdmedia.org/rcwc/iphone/home.html"];

    NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];

    [myWebView loadRequest:myRequest];

}





- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end

我已从失败的尝试中取出所有代码,因此该代码目前是我用于 viewcontroller1 的工作应用程序,并且我的 4 个选项卡有 4 个 ViewController。

谢谢,杰森

4

2 回答 2

0

那么你的代码现在没有提到关于 UIActivityIndi​​catorView 的任何内容。在你的 viewDidLoad 方法中,你需要这样的东西:

UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityView.center = myWebView.center;
[myWebView addSubview: activityView];
[activityView startAnimating];
于 2012-09-25T20:16:09.007 回答
0

这是非常粗糙且未经测试的,但它应该看起来像这样:

    UIView *loadingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];
    loadingView.center = self.view.center;
    [loadingView setBackgroundColor:[UIColor blackColor]];
    [loadingView setAlpha:0.75f];

    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activityIndicator.frame = CGRectMake(0,0,40,40);
    activityIndicator.center = loadingView.center;
    [loadingView addSubview:activityIndicator];
    [activityIndicator startAnimating];

    UILabel *loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, loadingView.bounds.size.width, 50)];
    [loadingLabel setText:@"Loading"];
    [loadingLabel setBackgroundColor:[UIColor clearColor]];
    [loadingLabel setTextAlignment:NSTextAlignmentCenter];
    [loadingLabel setTextColor:[UIColor whiteColor]];
    [loadingView addSubview:loadingLabel];

    [self.webView addSubview:loadingView];

您需要适应您实际希望视图看起来的方式,但这应该会让您指向正确的方向。

于 2012-09-26T02:59:58.623 回答