1

我需要使用 View Controller 1(称为 PracticeViewController)中的按钮更改 WebView(位于 WebViewViewController 中)的 URL。我完全理解当 Web View 与按钮位于同一个 View Controller 中时如何更改 Web View,但我不明白当 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

空格(顺便说一句,除了为每一行代码按空格 4 次之外,还有更简单的方法可以在 StackOverFlow 上发布吗?我错过了什么吗?)

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
4

3 回答 3

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 您的属性。

于 2013-07-17T03:52:50.790 回答
1

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

这是在 WebViewController 中注册到通知中心的示例

[[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];

这是苹果文档。

于 2013-07-16T23:00:28.713 回答
0

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

希望这有效:)

于 2013-07-17T05:44:37.827 回答