我需要使用 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