JS与UIWebView交叉编程

来源:互联网 发布:甲醛挥发温度曲线 知乎 编辑:程序博客网 时间:2024/06/03 13:59

主要用到UIWebView的  – stringByEvaluatingJavaScriptFromString:方法

如图:


上端UINavigationBar里有个Button,绑定selectCity事件

[cpp] view plaincopy
  1. UIBarButtonItem *selectBtn = [[UIBarButtonItem alloc] initWithTitle:@"Select" style:UIBarButtonItemStyleBordered target:self action:@selector(selectCity)];  
  2.     self.navigationItem.rightBarButtonItem = selectBtn;  
  3.     [selectBtn release];  

[cpp] view plaincopy
  1. - (void)selectCity  
  2. {  
  3.     NSString *result = [self.webView stringByEvaluatingJavaScriptFromString:@"select();"];  
  4.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You select city:" message:result delegate:self cancelButtonTitle:@"Sure" otherButtonTitles:@"Cancel", nil];  
  5.     [alert show];  
  6.     [alert release];  
  7. }  

html代码(使用jquery mobile)

[html] view plaincopy
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>city</title>  
  5.     <meta charset="utf-8">  
  6.     <meta name="viewport" content="width=device-width, initial-scale=1">  
  7.     <link rel="stylesheet" href="jquery.mobile-1.0.css"/>  
  8.     <script type="text/javascript" src="jquery.js"></script>  
  9.     <script type="text/javascript" src="jquery.mobile-1.0.js"></script>  
  10.     <script>  
  11.         function select()  
  12.         {  
  13.             var cities = document.getElementsByName("city");  
  14.             for(i=0; i<cities.length; i++)  
  15.             {  
  16.                 if(cities.item(i).checked)  
  17.                 {  
  18.                     var result = cities.item(i).getAttribute("value");  
  19.                     return result;  
  20.                 }  
  21.             }  
  22.         }  
  23.     </script>  
  24. </head>  
  25. <body>  
  26. <div data-role="page">  
  27. <div data-role="content">  
  28.         <fieldset data-role="controlgroup">  
  29.             <legend>Choose a city:</legend>  
  30.             <input type="radio" name="city" id="radio-choice-1" value="广州市" checked="checked" />  
  31.             <label for="radio-choice-1">广州市</label>  
  32.             <input type="radio" name="city" id="radio-choice-2" value="深圳市"  />  
  33.             <label for="radio-choice-2">深圳市</label>  
  34.             <input type="radio" name="city" id="radio-choice-3" value="珠海市"  />  
  35.             <label for="radio-choice-3">珠海市</label>  
  36.             <input type="radio" name="city" id="radio-choice-4" value="东莞市"  />  
  37.             <label for="radio-choice-4">东莞市</label>  
  38.             <input type="radio" name="city" id="radio-choice-5" value="茂名市"  />  
  39.             <label for="radio-choice-5">茂名市</label>  
  40.             <input type="radio" name="city" id="radio-choice-6" value="湛江市"  />  
  41.             <label for="radio-choice-6">湛江市</label>  
  42.             <input type="radio" name="city" id="radio-choice-7" value="阳江市"  />  
  43.             <label for="radio-choice-7">阳江市</label>  
  44.             <input type="radio" name="city" id="radio-choice-8" value="中山市"  />  
  45.             <label for="radio-choice-8">中山市</label>  
  46.             <input type="radio" name="city" id="radio-choice-9" value="佛山市"  />  
  47.             <label for="radio-choice-9">佛山市</label>  
  48.             <input type="radio" name="city" id="radio-choice-10" value="惠州市"  />  
  49.             <label for="radio-choice-10">惠州市</label>  
  50.             <input type="radio" name="city" id="radio-choice-11" value="梅州市"  />  
  51.             <label for="radio-choice-11">梅州市</label>  
  52.             <input type="radio" name="city" id="radio-choice-12" value="汕头市"  />  
  53.             <label for="radio-choice-12">汕头市</label>  
  54.         </fieldset>  
  55.     <a data-role="button" onclick="submit()" data-theme="b">Submit</a>  
  56.     </div>  
  57. </div>  
  58. </div>  
  59. </body>  
  60. </html>  
效果:

0 0
原创粉丝点击