如何使用单独的View Controller中的按钮更改WebView

时间:2013-07-16 22:50:23

标签: iphone ios objective-c uiwebview

我需要使用View Controller 1中的按钮(称为PracticeViewController)更改WebView(位于WebViewViewController中)的URL。我完全理解当它与按钮在同一个视图控制器中时如何更改Web视图,但我不明白当WebView位于不同的ViewController而不是按钮时,如何让按钮影响WebView。以下是我目前的代码:

PracticeViewController.h

#import <UIKit/UIKit.h>

@interface PracticeViewController : UIViewController

- (IBAction)passGoogleButton:(id)sender;

- (IBAction)passYahooButton:(id)sender;

- (IBAction)passBingButton:(id)sender;

/* ok I tried to make this as clear as possible, and you can look at the other files if    you don't understand what I mean, but at its core I want to change the web site loaded by     the Web View by clicking on the buttons in the other View Controller */

@end

空间(BTW是否有一种更简单的方式在StackOverFlow上发布除了每行代码按空格4次?我错过了什么吗?)

PracticeViewController.m

 #import "PracticeViewController.h"
 #import "WebViewViewController.h"

@interface PracticeViewController ()

@end

@implementation PracticeViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)passGoogleButton:(id)sender {
WebViewViewController.webSiteURL = @"http://www.google.com";

/* This supposedly works on a tutorial I saw, but Xcode flags it with the error     'Property webSiteURL not found on object of type 'WebViewViewController'' */
}

- (IBAction)passYahooButton:(id)sender {
WebViewViewController.webSiteURL = @"http://www.yahoo.com";
//same error as above
}

- (IBAction)passBingButton:(id)sender {
WebViewViewController.webSiteURL = @"http://www.bing.com";
//same error as above
}
@end

空间

WebViewViewController.h

#import <UIKit/UIKit.h>

@interface WebViewViewController : UIViewController
{
UIWebView *myWebView;
NSString *webSiteURL;
}
@property (weak, nonatomic) IBOutlet UIWebView *myWebView;
@property (strong, nonatomic) NSString *webSiteURL;

/* I don't entirely understand what 'strong' means but I know it has to do with memory   retention and that it (supposedly) should be used when I wan to transfer the value of the   string 'webSiteURL' to the other ViewController */

@end

空间

WebViewViewController.m

#import "WebViewViewController.h"

@interface WebViewViewController ()

@end

@implementation WebViewViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:webSiteURL]]];

/* As you can see above, the WebView loads from the string 'webSiteURL' but I can't figure out how to assign different values to it based on what button is clicked in the other View Controller so that it will run in ' - (void)viewDidLoad.' I already know how to transfer the value of the string to other things within another view controller (like into a label or a text field) but what I really need to know is how to get the string 'webSiteURL' to change before the view is loaded */

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

3 个答案:

答案 0 :(得分:1)

您可以使用nsnotificationcenter来完成此任务。 首先在WebViewController中注册到notificationcenter,然后从另一个控制器发送通知。

以下是在WebViewController中注册到notificationcenter的示例

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nMessageOpenURL:) name:@"nMessageOpenURL" object:nil];

这是收到通知时运行逻辑的代码。 (在WebViewController中)

- (void)nMessageOpenURL:(NSNotification *)note {
    //run the logic in here.
}

最后,这是您触发通知的代码。

[[NSNotificationCenter defaultCenter] postNotificationName:@"nMessageOpenURL" object:nil];

这是Apple文档。

答案 1 :(得分:1)

你有正确的想法,但你在这里错过了一步。您永远不会创建webViewController

的实例

你需要的是......

 webViewcontroller *MyWebController = [[webViewController alloc] init];

这取决于您呈现WebViewController的方式。如果您只是希望按钮以模态方式弹出WebViewController,则可以使用以下命令:

- (IBAction)passGoogleButton:(id)sender {
    webViewController *myWebVC = [[webViewController alloc] init];
    [myWebVC setWebSiteURL:@"http://www.google.com"];
    [self presentViewController:myWebVC animated:YES completion:nil];
}

所以,你可以在这里看到。您创建名为myWebVC的webViewController实例。然后将字符串传递给它。因此,当myWebVC加载并且它命中viewDidLoad时,它将使用您已经传入的字符串并使用该内容加载Web视图。希望有所帮助。

另外,请确保在.m文件中@synthesize您的属性。

答案 2 :(得分:0)

我知道你试图在两个视图控制器之间传递url字符串的值,但是你没有把它弄好!  因此,如果您使用的是故事板,您可以尝试使用Segue传递此值。对于Eg:您可以使用prepareForSegue使用if-else构造设置不同的值。  如果您不使用segue,那么传递这些值的最佳做法将是使用委托。 只需创建一个委托方法并将url字符串作为参数传递。

希望这有效:)