我正在尝试
[webLoad loadRequest:[NSURLRequest requestWithURL:url]];
当从另一个 ViewController 中单击时在 ViewController 中
- (IBAction)actionClick:(id)sender { }
导航图标。
我使用了下面的代码,但它不起作用
=========================AnotherViewController============================
- (IBAction)actionClick:(id)sender {
[[NSNotificationCenter defaultCenter] postNotificationName:@"Notification" object:self];
[self performSegueWithIdentifier:@"toURL" sender:self];
}
=============================ViewController============================
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(Notification:)
name:@"Notification"
object:nil];
}
- (void) Notification:(NSNotification *) notification
{
progressBar.progress = 0;
progressBar.hidden = false;
NSString *urlString = [[NSUserDefaults standardUserDefaults]stringForKey:@"urlSearch"];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding];
NSURL *url = [NSURL URLWithString:urlString];
[webLoad loadRequest:[NSURLRequest requestWithURL:url]];
}
最佳答案
问题出在这段代码中:
[[NSNotificationCenter defaultCenter] postNotificationName:@"Notification" object:self];
[self performSegueWithIdentifier:@"toURL" sender:self];
您甚至在初始化 ViewController 之前就发布了一个通知。 我看不到您在代码中使用了任何 NSNotification。 因此,如果您只是希望 ViewController 在打开时加载一个 url,那么您可以简单地执行此操作。
- (void)viewDidLoad {
[super viewDidLoad];
[self loadUrl];
}
- (void)loadUrl
{
progressBar.progress = 0;
progressBar.hidden = false;
NSString *urlString = [[NSUserDefaults standardUserDefaults]stringForKey:@"urlSearch"];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding];
NSURL *url = [NSURL URLWithString:urlString];
[webLoad loadRequest:[NSURLRequest requestWithURL:url]];
}
关于ios - 从另一个 View Controller 导航栏加载 View Controller 的 UIWebView 中的请求 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57556405/