2009-07-02 2 views
3

UIWebView 컨트롤러가있는보기 컨트롤러가 있습니다. 내 목적은 C 코드에 일부 정보를 전달하는 웹보기의 HTML 콘텐츠 안에 자바 스크립트를 얻으려고 노력하고있어. 여러 예제를 통해 온라인에서 javascript 함수로 window.location을 설정 한 다음 뷰 컨트롤러를 웹 뷰의 대리자로 설정하고 shouldStartLoadWithRequest 함수에서 catch하는 이벤트를 잡았습니다. 불행히도 나는 나를 위해 shouldStartLoadWithRequest 웹 뷰의 대리자를 설정 해요 호출 된 적이 있기 때문에 그것을 작동시킬 수 없습니다. 다음과 같이shouldStartLoadWithRequest가 내 iPhone 앱에서 호출되지 않았습니다.

내 코드는 다음과 같습니다

인터페이스 : 인터페이스에서

@interface StoryTextViewController : UIViewController <UIWebViewDelegate> 
{ 
    IBOutlet UIWebView *storyWebView; 
    NSString *information; 
} 

@property (nonatomic, retain) IBOutlet UIWebView *storyWebView; 
@property (nonatomic, retain) NSString *information; 

@end 

의 * 정보 변수가 StoryTextViewController를 호출하는 이전보기에 의해 설정된다.

구현 :

#import "StoryTextViewController.h" 

@implementation StoryTextViewController 

@synthesize storyWebView; 
@synthesize information; 

- (NSString *) contructHTMLText 
{ 
    NSString *HTMLText = @""; 

    NSArray *words = [information componentsSeparatedByString:@" "]; 
    NSString *header = 
    @"<html>\n" 
    "<head>\n" 
    "<script type=\"text/javascript\">\n" 

    "function marktext(theSpanID)\n" 
    "{\n" 
    " var theSpan=document.getElementById(theSpanID);\n" 
    " if (theSpan.className == \"noHilite\")\n" 
    " {\n" 
    "  theSpan.className = \"hilite\";\n" 
    "  window.location = \"true\";\n" 
    " }\n" 
    " else\n" 
    " {\n" 
    "  theSpan.className = \"noHilite\";\n" 
    "  window.location = \"false\";\n" 
    " }\n" 
    "}\n" 

    "</script>\n" 

    "<style type=\"text/css\">\n" 

    ".hilite\n" 
    "{\n" 
    " background-color:red;\n" 
    " color:white;\n" 
    " font: bold 60px arial,sans-serif\n" 
    "}\n" 

    ".noHilite\n" 
    "{\n" 
    " background-color:white;\n" 
    " color:black;\n" 
    " font: 60px arial,sans-serif\n" 
    "}\n" 

    "</style>\n" 

    "</head>\n" 
    "<body>\n" 
    "<div class=\"storyText\">\n"; 

    NSString *tailer = 
    @"</div>\n" 
    "</body>\n" 
    "</html>\n"; 

    HTMLText = [HTMLText stringByAppendingString:header]; 
    for (NSInteger i = 0; i < [words count]; i ++) 
    { 
     NSString *tag = [NSString stringWithFormat:@" <span id=\"mytext%d\" class=\"noHilite\" onclick=\"marktext(\'mytext%d\')\">%@</span>&nbsp;\n", i, i, (NSString *)[words objectAtIndex:i]]; 
     HTMLText = [HTMLText stringByAppendingString:tag]; 
    } 
    HTMLText = [HTMLText stringByAppendingString:tailer]; 
    NSLog(HTMLText); 
    return HTMLText; 
} 

// The designated initializer. Override to perform setup that is required before the view is loaded. 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { 
     storyWebView.delegate = self; 
    } 
    return self; 
} 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSString *HTMLData = [self contructHTMLText]; 
    [storyWebView loadHTMLString:HTMLData baseURL:[NSURL URLWithString: @""]]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview 
    // Release anything that's not essential, such as cached data 
} 

- (void)dealloc { 
    [super dealloc]; 
} 

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    // handle event here  
    return true; 
} 

@end 

일어나는 것은 기본적 constructHTML 기능은 * 정보 변수 안의 텍스트를 취하여 어떤 HTML 및 JavaScript와 같은 단어를 클릭 할 때마다 그와 텍스트의 모든 단어를 감싸는 것을 뭐죠 , 그것에 CSS 하이 라이팅이 적용됩니다. 내가하고 싶은 것은 밝은 빛의 단어의 수를 세는 것입니다. 나는 단어가 클릭 될 때마다 호출되는 함수에서 무언가를 전달하려고 시도하지만 이렇게 말한 것처럼 shouldStartLoadWithRequest 메서드는 실행되지 않을 것이라고 결코 실행되지 않습니다.

나는 많은 사람들을 본 적이

는 이런 종류의 작업을 수행하지만 나를

답변

0
  • 는 당신이 대상 URL의 값이 될 수 없습니다 window.location해야 내가 그가 실행되지 않을 이유를 알아낼 질수 가는거야? marktext 함수에서 true 또는 false으로 설정하고 있습니다. shouldStartLoadWithRequest은 URL 요청이 수신되어 어딘가에 가려고 할 때만 호출됩니다. 위치를 부울로 설정하는 것이 탐색 이벤트를 시작하는 데 충분하지 않은지 확인하십시오. 마법 URL로 설정 한 다음 shouldStartLoadWithRequest 루틴에서 FALSE를 반환하면 특정 마법 URL로 이동하여 실제로 어딘가에 가지 않도록 할 수 있습니다. 유효한 URL은 통과 할 수 있습니다.

  • 또한 this SO question에 대한 응답을보고 싶을 수도 있습니다. window.location에는 몇 가지 타이밍 문제가있을 수 있습니다.

+0

실제로 window.locaton을 "true"또는 "false"문자열로 설정하려고합니다. 내 코드가 어떤 문자열에도 작동하지 않기 때문에 중요하지 않습니다. 나는 또한 당신이 보낸 링크를 보았지만 그것이 내가 필요로하는 것이 아니었다. 그의 문제는 shouldStartLoadWithRequest가 저를 위해 너무 잘 작동한다는 것입니다. 제 경우에는 그것이 전혀 호출되지 않습니다! –

+0

위치 값을 'true'또는 'false'로 설정하려는 이유가 확실하지 않습니다. 이는 URL 표시 줄에 "http : // true"를 입력하는 것과 같습니다. http://www.stackoverflow.com과 같은 유효한 URL을 사용하면 어떻게됩니까? 제 요점은 shouldStartLoadWithRequest가 호출 할 올바른 URL을 지정하지 않으면 호출되지 않는다고 생각했습니다. – Ramin

4

당신은 위임

if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { 
    storyWebView.delegate = self; 
} 

설정에 중단 점을 넣을 수 있습니다 나는 storyWebView 그 순간에 무기 호입니다 확신 해요.

당신은이 방법으로 문제를 해결할 수 있습니다 : 당신이 더 나은 코드를 확인하려는

articleWeb.userInteractionEnabled = NO;` 

를 설정했기 때문에, 나는이 문제를 볼

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSString *HTMLData = [self contructHTMLText]; 
    // empty statement 
    // the trick is that if the view is not loaded from the .nib yet 
    // it will be loaded and all connections established 

    if (self.view); 

    // here is setting delegate 
    storyWebView.delegate = self; 
    [storyWebView loadHTMLString:HTMLData baseURL:[NSURL URLWithString: @""]]; 
} 
+0

나는 당신이 제안한대로했지만 아무런 효과가 없었습니다. 어쨌든 자기는 아무래도 시작하지 못했습니다. 당신이 제안 할만한 다른 건 없나요? –

+0

자기가 반드시 nil이 아니며 storyWebView는 어떻습니까? –

+0

스토리 뷰는 html 과금도 표시합니다. 완전히 우물 우물쭈물. 원한다면 내가 코딩 한 것을 보낼 수 있겠 니? 내 상사가 조금 참을성이 없어지고 메신저가 불만 해지고있다. –

0

articleWeb.userInteractionEnabled = YES;` 
설정
1

initWithNibName:bundle:이 아닌 viewDidLoad에 대리인을 설정해야합니다. 또는, nib 파일을 열고 웹보기에서 File 's Owner로 드래그 앤 드롭하고 드롭 다운에서 "delegate"를 선택하십시오.

관련 문제