初始加载视图控制器不会响应Web视图

时间:2015-04-05 13:50:21

标签: ios objective-c uiviewcontroller webview

我有两个视图控制器,一个是informationViewController,另一个是InformationDetailedViewController。按钮动作有两个按钮我正在加载各自的url pdf文件

// InformationViewController.m
       // About Us button clicked
        -(void)aboutUsAction:(id)sender{
            [informationDetailedViewController showInformation:0];
            [self.navigationController pushViewController:informationDetailedViewController animated:YES];
        }

        // Terms and condition button clicked
        -(void)termsAndConditionAction:(id)sender{
            [informationDetailedViewController showInformation:1];
            [self.navigationController pushViewController:informationDetailedViewController animated:YES];
        }

  // InformationDetailedViewController.m    
    - (void)viewDidLoad{

        [super viewDidLoad];    
        // create a webview
        webInfoDetailed = [[UIWebView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)];
        webInfoDetailed.scalesPageToFit = YES;
        [webInfoDetailed setBackgroundColor:[UIColor whiteColor]];
        [self.view addSubview:webInfoDetailed];    
    }

    -(void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:YES];
    }


    -(void)viewDidAppear:(BOOL)animated{
        [super viewDidAppear:YES];
    }

        // InInformationDetailedViewController.1
        -(void)showInformation:(int )informationId {

            NSString *informationType = @"";
            switch (informationId) {
                case 0:
                    self.title = @"About Us";
                    informationType = KHABOUTUS;
                    break;
                case 1:
                    self.title = @"Disclaimer";

                    informationType = KHDISCLAIMER;
                    break;
                default:
                    break;
            }

            NSURL *targetURL = [NSURL URLWithString:informationType];
            NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
            [webInfoDetailed loadRequest:request];
        }

问题出在初始加载(第一次加载)url未在webView上加载。 单击后退,然后单击“关于”或“条款和条件”,它的工作正常。这是我在这里失踪的小事。 @All先前谢谢

1 个答案:

答案 0 :(得分:1)

扩展Jassi的评论,这是一个可能对您有帮助的代码段。

  

InformationDetailedViewController.h

@property int informationId; // make it as a member of VC.

更改showInformation方法,使其使用informationId成员而不是

中的参数
  

在InformationDetailedViewController.m

-(void)showInformation { //you can omit the parameter, and do not forget to change the declaration in header file
  NSString *informationType = @"";
  switch (self.informationId) {
    case 0:
      self.title = @"About Us";
      informationType = KHABOUTUS;
    break;
    case 1:
      self.title = @"Disclaimer";
      informationType = KHDISCLAIMER;
    break;
    default:
    break;
  }

  NSURL *targetURL = [NSURL URLWithString:informationType];
  NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
  [webInfoDetailed loadRequest:request];
}

最后,替换

中对showInformation方法的调用
  

InformationViewController.m

        // About Us button clicked
        -(void)aboutUsAction:(id)sender{
            [informationDetailedViewController setInformationId:0];
            [self.navigationController pushViewController:informationDetailedViewController animated:YES];
        }

        // Terms and condition button clicked
        -(void)termsAndConditionAction:(id)sender{
            [informationDetailedViewController setInformationId:1];
            [self.navigationController pushViewController:informationDetailedViewController animated:YES];
        }

希望它有所帮助。