我正在为 iPhone 开发一个简单的测试应用程序。我想点击一个按钮来改变URL
的 webview
,然后加载新的 URL。这是我的代码:
NSString *nurl = [NSString stringWithFormat: @"http://www.abc.com/abc.aspx?SN=%@&fdate=%@&tdate=%@", SNstr, fDate, tDate];
NSLog(@"################: %@", nurl);
NSURL *url = [NSURL URLWithString:nurl];
NSLog(@"################: %@", url);
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[Logs loadRequest:req];
它在
viewDidLoad
内部运行良好.我将它移动到一个按钮中:
-(IBAction)CheckLog:(id)sender
{
// codes here
}
当我按下按钮时,它会给出
nurl
的值正确,但 url
的值为空。所以 UIWebView 上没有任何改变 Logs
.为什么会这样,当代码在
viewDidLoad
中正确运行时?
最佳答案
您的按钮操作将是这样的......
-(IBAction)CheckLog:(id)sender
{
[[NSURLCache sharedURLCache] removeAllCachedResponses];
NSString *nurl = [NSString stringWithFormat: @"http://www.abc.com/abc.aspx?SN=%@&fdate=%@&tdate=%@", SNstr, fDate, tDate];
NSURL *url = [NSURL URLWithString: [nurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ];
// NSURL *theURL = [NSURL URLWithString:@"https://www.google.co.in/"]; // Your new URL
[self.myWebView loadRequest:[NSURLRequest requestWithURL:url]];
}
关于ios - 按下 UIButton 时加载 UIWebview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17084168/