導航:首頁 > 知識科普 > ios怎麼調用一個方法調用方法調用

ios怎麼調用一個方法調用方法調用

發布時間:2025-03-30 22:38:51

1. 如何在IOS平台上使用js直接調用OC方法

本例子是為了讓大家能快速開發出OC調用JS功能的一個簡單的例子。

1、准備一個本地化的html網頁,如jsIOS.html

<script type="text/javaScript">
function postStr(){
return document.getElementById("text1").value;
//return "javaScript返回值啦";
}
</script>

2、將此html文件放到項目代碼目錄裡面,如圖:

3、拖一個UIWebView控制項和UIButton控制項到xxxViewController對應的.xib或.storyboard視圖的UIView上;
在xxxViewController的.h文件中分別聲明UIWebView類型變數和UIButton類型的變數,以及一個按鈕點擊事件(並且跟視圖裡面的控制項連線),
並且添加一個UIWebViewDelegate類型的委託。<喎�"http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA++CjxwIGNsYXNzPQ=="p1">
#import

@interface ViewController : UIViewController
@property(nonatomic,retain) IBOutlet UIWebView *webview;
@property(nonatomic,retain) IBOutlet UIButton *button;
-(IBAction)IOS_JS:(id)sender;
@end

4、在xxxViewController.m文件中實現通過點擊事件,調用javaScript的方法並取得返回值。
代碼如下:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize webview;

- (void)viewDidLoad
{
[super viewDidLoad];
//設置webView
webview.backgroundColor = [UIColor clearColor];
//webview.scalesPageToFit =YES;
webview.delegate =self;
//找到jsIOS.html文件的路徑
NSString *basePath = [[NSBundle mainBundle]bundlePath];
NSString *helpHtmlPath = [basePath :@"jsIOS.html"];
NSURL *url = [NSURL fileURLWithPath:helpHtmlPath];
//載入本地html文件
[webview loadRequest:[NSURLRequest requestWithURL:url]];
}

/*
* 點擊事件
* 調用javaScript的方法postStr()並取得返回值
* 輸出返回值到控制台
*/
-(IBAction)IOS_JS:(id)sender
{
NSString *str = [self.webview :@"postStr();"];
NSLog(@"JS返回值:%@",str);
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end

閱讀全文

與ios怎麼調用一個方法調用方法調用相關的資料

熱點內容
蜜蠟的鑒別方法圖片 瀏覽:280
產後媽媽消除妊娠紋的方法有哪些 瀏覽:82
腳踝扭傷固定方法圖片 瀏覽:1002
南方冬天釣羅非最佳方法 瀏覽:906
韓國盤發器使用方法 瀏覽:567
權健面膜使用方法 瀏覽:396
科學分析最好的方法 瀏覽:779
生膠快速溶解方法 瀏覽:936
可食用唇部磨砂膏製作方法 瀏覽:267
手指頭上出汗怎麼解決方法 瀏覽:302
橋梁波紋管漏氣檢測方法 瀏覽:540
怎麼方法磨刀使刀更快 瀏覽:22
敲豬方法視頻 瀏覽:590
微量移液管的使用方法 瀏覽:809
小學做閱讀理解技巧與方法 瀏覽:773
海釣桿的安裝方法 瀏覽:966
手機清理電腦垃圾方法 瀏覽:957
在層析技術中常用的顯色方法有 瀏覽:201
手機隱私保護方法視頻 瀏覽:343
蒲公英的根作用及食用方法 瀏覽:255