2013-07-16 3 views
1

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 

답변

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에 도달하면 이미 전달한 문자열을 사용하고 해당 콘텐츠가 포함 된 웹보기를로드합니다. 희망이 도움이됩니다.

또한 .m 파일에서 @synthesize 속성을 확인하십시오.

+0

도움을 주셔서 감사합니다. 그러나 이것은 효과가없는 것 같습니다. 나는 (IBAction) pass go go 버튼에 정확히 말한 것을 넣었지만, 버튼을 누르면 다음 View Controller로 바뀌고 WebView는 그냥 비어 있습니다. –

+0

스토리 보드를 사용하고 있습니까? 그건 분명히 URL을 통과해야합니다. 다음 문제는 webView를 구현하는 데있을 수 있습니다. 당신이 nib/storyboard를위한 콘센트 인 '@property UIWebView'입니까? 그렇지 않다면 다시는'webViewViewController에 UIWebView를 생성하지 않는다. – WCByrne

+0

당신은 또한 당신의 viewDidLoad에'NSLog (@ "URLL % @", webSiteURL);를 추가하여 url이 전달되고 있는지 확인할 수 있습니다; – WCByrne

1

nsnotificationcenter를 사용하여이 작업을 수행 할 수 있습니다. 먼저 WebViewController의 notificationcenter에 등록한 다음 다른 컨트롤러에서 알림을 보냅니다.

여기 이것은 당신이 통지를받을 때 논리를 실행하는 코드 WebViewController

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

에 notificationcenter에 등록하는 샘플입니다. (WebViewController에서)

마지막으로 알림을 트리거하는 코드입니다.

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

다음은 Apple 문서입니다.

+0

이 중 하나가 작동하지 않는 것 같습니다. - (IBAction) passGoogleButton에 '트리거'코드를 넣은 다음 '로직'에 대해 - (void) nMessageOpenURL에 "[myWebView loadRequest : [NSURLRequest requestWithURL : [NSURL URLWithString : @"http : // www. google.com "]]];" WebViewController의 viewDidLoad에 NSNotificationCenter 등록을 넣습니다. 나는 무엇을 잘못 할 수 있 었는가? –

+0

여기 샘플 애플리케이션이 있습니다. https://dl.dropboxusercontent.com/u/10062261/sotest.zip –

+0

Nvm @WCByrne의 방법을 사용하여 얻었습니다. 그래도 도와 ​​줘서 고마워 !! NSNotificationCenter에 대해 알고 있습니다! –

0

두 개의보기 컨트롤러간에 url 문자열의 값을 전달하려고 시도했음을 알게되었지만 제대로 표시되지 않았습니다! 스토리 보드를 사용하는 경우 Segue를 사용하여이 값을 전달해볼 수 있습니다. 예 : prepareForSegue를 사용하여 if-else 구문을 사용하여 다른 값을 설정할 수 있습니다. segue를 사용하지 않는 경우 해당 값을 전달하는 가장 좋은 방법은 대리인을 사용하는 것입니다. 대리자 메서드를 만들고 url 문자열을 인수로 전달하기 만하면됩니다.이 작품

희망 :

관련 문제