2013-05-06 72 views
0

我正在構建一個iPhone應用程序,其中有兩個視圖一個用於登錄視圖,另一個用於Web視圖。當用戶輸入登錄憑證時,它將重定向到Web視圖。而從登錄視圖加載web視圖太多的時間正在加載。因此我使用了alertview來顯示加載圖像。停止動畫加載圖片後Webview內容不可讀

@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的內容。任何人都可以幫助我解決這個問題...在此先感謝。

+0

重要的你或許應該把代碼中的問題,而不是一個引擎收錄。 – nil 2013-05-06 06:45:40

回答

0

我建議表明從視圖控制器具有的WebView活動指示燈和實施的WebView委託方法

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

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

這將讓您的WebView互動,也表明了指標,直到頁面完全加載

0

你可以嘗試這樣的事情。該代表是在.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]; 
}