将ActivityIndi​​cator添加到Webview选项卡应用程序

时间:2012-09-25 19:27:01

标签: iphone xcode

我正在尝试在选择每个选项卡并加载初始选项卡后在屏幕中央添加“加载”指示符。我发现很多帖子显示了完成此操作的代码,但它似乎与我的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个ViewControllers。

谢谢, 杰森

2 个答案:

答案 0 :(得分:0)

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

UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityView.center = myWebView.center;
[myWebView addSubview: activityView];
[activityView startAnimating];

答案 1 :(得分: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];

您需要适应您实际想要查看的方式,但这应该让您指向正确的方向。