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> \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 메서드는 실행되지 않을 것이라고 결코 실행되지 않습니다.
나는 많은 사람들을 본 적이는 이런 종류의 작업을 수행하지만 나를
실제로 window.locaton을 "true"또는 "false"문자열로 설정하려고합니다. 내 코드가 어떤 문자열에도 작동하지 않기 때문에 중요하지 않습니다. 나는 또한 당신이 보낸 링크를 보았지만 그것이 내가 필요로하는 것이 아니었다. 그의 문제는 shouldStartLoadWithRequest가 저를 위해 너무 잘 작동한다는 것입니다. 제 경우에는 그것이 전혀 호출되지 않습니다! –
위치 값을 'true'또는 'false'로 설정하려는 이유가 확실하지 않습니다. 이는 URL 표시 줄에 "http : // true"를 입력하는 것과 같습니다. http://www.stackoverflow.com과 같은 유효한 URL을 사용하면 어떻게됩니까? 제 요점은 shouldStartLoadWithRequest가 호출 할 올바른 URL을 지정하지 않으면 호출되지 않는다고 생각했습니다. – Ramin