ios - 初始加载 View Controller 不会响应 web View

标签 ios objective-c uiviewcontroller webview

我有两个 View Controller ,一个是 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 提前致谢

最佳答案

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

InformationDetailedViewController.h

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

更改 showInformation 方法,使其使用 informationId 成员而不是 information 中的参数

In 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];
        }

希望对您有所帮助。

关于ios - 初始加载 View Controller 不会响应 web View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29457997/

相关文章:

iphone - 按下按钮后显示图像

ios - `<Value>` 中的 `enum Result<Value> { ... }` 是什么?

ios - 当应用程序处于后台状态时如何从UIPasteboard获取数据(字符串,URL,图像等)

objective-c - 从另一个数组中减去一个 NSArray 中的对象

ios - 以编程方式添加的 ViewController 看起来已移动/转换(iOS、swift、 Storyboard)

objective-c - 在 View Controller 中使用导航 Controller

ios - Objective-C 如何将 C 结构体初始化为属性?

ios - Google+ iOS SDK - 获取关注 "me"的人列表

ios - 如何从动态 UITableView 单元格中获取 UILabel 的大小?

objective-c - 如何从我的应用程序启动 Mac 地址簿并选择特定人员?