わかりました。あなたがやろうとしていたように、UIWebViewを手動でそれ自体に追加する単純なViewControllerを作成しました。また、プライベートUIWebViewです。UIWebViewに渡す単純なボタンを作成しました。これはalert()
毎回機能するので、ここにコード全体を示して、何が起こっているかを確認できるようにします。
.hファイル
#import <UIKit/UIKit.h>
@interface SOFViewController : UIViewController
// This is linked as action thru nib.
- (IBAction)hitMe:(UIButton *)sender;
@end
.mファイル
ここで、最初にjavascriptを呼び出した場所がにあるwebViewに配置されていることに注意してくださいViewDidAppear
。他の先行する関数は、シミュレーターで最初に起動したときに不正な動作を引き起こします。Xcode4.3を使用しています
#import "SOFViewController.h"
@interface SOFViewController ()
{
int idNumber;
UIWebView *webViewMain;
}
@end
@implementation SOFViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
webViewMain = [[UIWebView alloc] initWithFrame:CGRectMake(50.0, 50.0, 50.0, 50.0)];
[self.view addSubview:webViewMain];
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
idNumber = idNumber + 1;
NSString * js = [NSString stringWithFormat:@"alert('%d');",idNumber];
[webViewMain stringByEvaluatingJavaScriptFromString:js];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)hitMe:(UIButton *)sender {
idNumber = idNumber + 1;
NSString * js = [NSString stringWithFormat:@"alert('%d');",idNumber];
[webViewMain stringByEvaluatingJavaScriptFromString:js];
}
@end
これがある程度役に立ったことを願っています。