View Controller 1 (PracticeViewController라고 함)의 단추를 사용하여 WebView (WebViewViewController에 있음)의 URL을 변경해야합니다. WebView가 단추와 동일한 View Controller에있을 때 웹보기를 변경하는 방법을 완전히 이해하지만 WebView가 단추와 다른 ViewController에있을 때 WebView에 영향을주는 단추를 얻는 방법을 이해하지 못합니다. 아래는 제가 현재 가지고있는 코드 :개별 View Controller의 단추로 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 코드의 모든 라인을위한 공간 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
도움을 주셔서 감사합니다. 그러나 이것은 효과가없는 것 같습니다. 나는 (IBAction) pass go go 버튼에 정확히 말한 것을 넣었지만, 버튼을 누르면 다음 View Controller로 바뀌고 WebView는 그냥 비어 있습니다. –
스토리 보드를 사용하고 있습니까? 그건 분명히 URL을 통과해야합니다. 다음 문제는 webView를 구현하는 데있을 수 있습니다. 당신이 nib/storyboard를위한 콘센트 인 '@property UIWebView'입니까? 그렇지 않다면 다시는'webViewViewController에 UIWebView를 생성하지 않는다. – WCByrne
당신은 또한 당신의 viewDidLoad에'NSLog (@ "URLL % @", webSiteURL);를 추가하여 url이 전달되고 있는지 확인할 수 있습니다; – WCByrne