【问题标题】:Webview content is not readable after stop animating loading image停止动画加载图像后,Webview 内容不可读
【发布时间】:2013-05-06 06:31:04
【问题描述】:

我正在构建一个 iPhone 应用程序,其中有两个视图,一个用于登录视图,另一个用于 Web 视图。当用户输入登录凭据时,它将重定向到 Web 视图。从登录视图加载 webview 时,加载时间过长。因此我使用 alertview 来显示加载图像。

@interface FocusViewController ()
@end

@implementation FocusViewController

@synthesize txtsecurecode;
@synthesize webView;
@synthesize OrganizationCode;

UIActivityIndicatorView *indicator;
UIAlertView *alert;

- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = (FocusAppDelegate *)[[UIApplication sharedApplication]delegate];

if(appDelegate.data.strOrganizationCode == nil || appDelegate.data.strAccessCode == nil)
{
    NSLog(@"YOUR FIRST SCREEN");
    _loginView.hidden = NO;
     webView.hidden = YES;
}
else
{
    NSLog(@"LOAD WEBVIEW DIRECTLY\n");
    NSLog(@"Organization Code -> %@ \n Secure Access Code -> %@",appDelegate.data.strOrganizationCode,appDelegate.data.strAccessCode);

    OrganizationCode.text = appDelegate.data.strOrganizationCode ;
    txtsecurecode.text = appDelegate.data.strAccessCode;
    [self loginClicked:nil];

     webView.hidden = NO;
    _loginView.hidden = YES;

   }
  }

- (IBAction)loginClicked:(id)sender {

@try {

    if([[txtsecurecode text] isEqualToString:@""]  || [[OrganizationCode text] isEqualToString:@""] ) {
        [self alertStatus:@"Please enter Access code" :@"Login Failed!":0];
    }
    else
    {
        NSString *post =[[NSString alloc] initWithFormat:@"txtsecurecode=%@ @&password=%@",[txtsecurecode text],[OrganizationCode text]];

       NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];

        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://myexample.com/AccountService/security/ValidateAccess?accesscode=%@&companycode=%@&type=1", txtsecurecode.text, OrganizationCode.text]];

        NSString *responseData = [[NSString alloc]initWithData:[NSData dataWithContentsOfURL:url] encoding:NSUTF8StringEncoding];

        if([responseData isEqualToString:@""]){
            [self alertStatus:@"Please enter valid Access Code" :@"Login Failed !" :0];
        }
        else
        {
            appDelegate.data.strOrganizationCode = OrganizationCode.text;
            appDelegate.data.strAccessCode = txtsecurecode.text;

            [FocusAppDelegate addCustomObjectToUserDefaults:appDelegate.data key:kCredentails];

            //Updated
            _loginView.hidden = YES;
             webView.hidden = NO;

            responseData = [responseData stringByReplacingOccurrencesOfString:@" "" " withString:@""];
            NSString* encodedString = [responseData stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

            NSLog(@"Response ==> %@" ,encodedString);

            alert = [[[UIAlertView alloc] initWithTitle:@"Loading\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease];
             [alert show];

            UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
            indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50);
                            [indicator startAnimating];
                            [alert addSubview:indicator];
                            [indicator release];

            webView.backgroundColor = [UIColor clearColor];
            webView.opaque = NO;
            webView.delegate = self;
            webView.frame = self.view.bounds;

            NSString* urlTwo = [[encodedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
                                stringByReplacingOccurrencesOfString:@"%22" withString:@""];

            NSURL *url2;

            if([urlTwo hasPrefix:@"http://"]){
                url2 = [NSURL URLWithString:urlTwo];
            }else{
                url2 = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@" , urlTwo]];
            }

            NSLog(@"url2:%@", url2);

            NSURLRequest *requestObj = [NSURLRequest requestWithURL:url2];

            [webView loadRequest:requestObj];

            [[self view] addSubview:webView];

        }
    }

}

@catch (NSException * e) {
    NSLog(@"Exception: %@", e);
    [self alertStatus:@"Login Failed." :@"Login Failed!" :0];
}
}

 -(void)webViewDidStartLoad:(UIWebView *)webView{
NSLog(@"webViewDidStartLoad");
[self.view addSubview:self.indicator];
 alert.hidden = NO;

 }

 - (void) webViewDidFinishLoad:(UIWebView *)webView {
 NSLog(@"webViewDidFinishLoad");
[self.indicator removeFromSuperview];
[self.alert dismissWithClickedButtonIndex:1 animated:NO] ;

}

- (IBAction)backgroundClick:(id)sender
{
[txtsecurecode resignFirstResponder];
[OrganizationCode resignFirstResponder];
}
@end

在 webview 中的内容显示之前,加载图像正在显示并且运行良好。但是 webview 的内容不可编辑并且保持静态,因为我认为我使用了“web view.hidden = YES”。当我评论警报方法并运行时,webview 的内容也是可编辑的,并且可以根据需要正常工作。加载图像停止加载后,我需要加载 url 的内容。

【问题讨论】:

  • 您可能应该将代码放在问题中,而不是粘贴箱。

标签: iphone objective-c cocoa


【解决方案1】:

我建议从具有 WebView 的 Viewcontroller 中显示活动指示器并实现 WebView 委托方法

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [actvityIndicator startAnimating];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [actvityIndicator stopAnimating];
}

这将使您的 webview 保持互动,并在页面完全加载之前显示指示器

【讨论】:

    【解决方案2】:

    你可以试试这样的。 Delegate 在 .h 文件中很重要

    FocusViewController.h

    @interface FocusViewController : UIViewController <UIWebViewDelegate> {
        UIActivityIndicatorView *indicator;
    }
    

    FocusViewController.m

    - (void)viewDidLoad
    {
        // Loading Indicator
        indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    
        [indicator setColor:[UIColor colorWithRed:50.0/255.0 green:120.0/255.0 blue:200.0/255.0 alpha:1.0]];
    
    }
    
    - (void)webViewDidStartLoad:(UIWebView *)webView
    {
        [indicator startAnimating];
    }
    
    - (void)webViewDidFinishLoad:(UIWebView *)webView
    {
        [indicator stopAnimating];
    }
    

    【讨论】:

    • 委托也可以在实现文件中表示。如果您喜欢警告,也可以省略它... ;)
    猜你喜欢
    • 2020-01-22
    • 2014-02-22
    • 2011-09-24
    • 1970-01-01
    • 2019-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-07
    相关资源
    最近更新 更多