从不同 class 调用 UIWebView
Calling UIWebView from different class
我有 ViewController1.m 和 ViewController2.m
在 ViewController1.m 中我有一个 UIWebView
并且我还创建了一个方法来加载 URL.
如果我从 ViewController1.m 调用该方法,它会起作用。
但是当我尝试从 ViewController2.m 调用该方法时,我不起作用。
我意识到这是因为我创建了 ViewController1.m Class 的新实例。
所以我的问题是,如果不是 ViewController1 的活动实例,我该如何调用该方法 Class?
此致,
我能够通过使用通知来完成我需要它做的事情。
我不知道这是否是最好的方法,因为它对我有用。
我希望这对其他人有用。
在ViewController1.m
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveTestNotification:)
name:@"TestNotification"
object:nil];
}
- (void) receiveTestNotification:(NSNotification *) notification
{
[YourWebView stringByEvaluatingJavaScriptFromString:@"alert('It works');"];
}
在ViewController2.m
-(void) someMethod {
[[NSNotificationCenter defaultCenter]
postNotificationName:@"TestNotification"
object:self];
[self dismissViewControllerAnimated:YES completion:nil];
}
我有 ViewController1.m 和 ViewController2.m
在 ViewController1.m 中我有一个 UIWebView
并且我还创建了一个方法来加载 URL.
如果我从 ViewController1.m 调用该方法,它会起作用。 但是当我尝试从 ViewController2.m 调用该方法时,我不起作用。
我意识到这是因为我创建了 ViewController1.m Class 的新实例。
所以我的问题是,如果不是 ViewController1 的活动实例,我该如何调用该方法 Class?
此致,
我能够通过使用通知来完成我需要它做的事情。 我不知道这是否是最好的方法,因为它对我有用。
我希望这对其他人有用。
在ViewController1.m
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveTestNotification:)
name:@"TestNotification"
object:nil];
}
- (void) receiveTestNotification:(NSNotification *) notification
{
[YourWebView stringByEvaluatingJavaScriptFromString:@"alert('It works');"];
}
在ViewController2.m
-(void) someMethod {
[[NSNotificationCenter defaultCenter]
postNotificationName:@"TestNotification"
object:self];
[self dismissViewControllerAnimated:YES completion:nil];
}