我有两个 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/