实现某个View的点击事件:
//单击的 Recognizer
UITapGestureRecognizer * singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SingleTap:)];
singleRecognizer.numberOfTapsRequired = 1; //tap次数
[self.itemView addGestureRecognizer:singleRecognizer];
然后再实现方法:
#pragma mark singleRecognizerDelegateMethod
- (void)SingleTap:(UITapGestureRecognizer *)recognizer
{
if (!self.urlField.text.length == 0) {
//处理单击操作
self.itemView.hidden = YES;
}
//取消textField响应
[self.urlField resignFirstResponder];
}
代理实现:
在头文件中声明代理
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIWebViewDelegate,UITextFieldDelegate>
在Controller中
- (void)viewDidLoad {
[super viewDidLoad];
self.urlField.delegate = self;
self.webView.delegate = self;
然后实现代理方法。
取消textField 的焦点:
[self.urlField resignFirstResponder];
webView加载页面:
//获得field的text并请求该URL
NSString * urla = self.urlField.text;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urla]];
//webview加载请求
[self.webView loadRequest:request];
弹出窗口:
NSString *title = [NSString stringWithFormat:@"AAA"];
NSString *message = [NSString stringWithFormat:@"消息"];
[[[UIAlertView alloc]initWithTitle:title message:message delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil]show];