看完以后觉得使用还是很方便的.
同时代码中还有UITextField的leftView与rightView示例.
代码:
//
// main.m
// ControlDemo
//
// Created by watsy0007 on 12-6-3.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#define BARBUTTONITEM(title,act) [[[UIBarButtonItem alloc] initWithTitle:title \
style:UIBarButtonItemStylePlain \
target:self \
action:act] autorelease];
@interface KeyboardAccessoryView : UIView <UIInputViewAudioFeedback> {
}
@end
@implementation KeyboardAccessoryView
- (BOOL) enableInputClicksWhenVisible {
return YES;
}
- (void) playClickForCustomKeyTap {
[[UIDevice currentDevice] playInputClick];
}
@end
//--------------------------------------------------------------------------------------------
#pragma mark -
#pragma mark ViewController()
@interface ViewController : UIViewController <UITextFieldDelegate,UIWebViewDelegate> {
UITextField *_txtField;
UIWebView *_webView;
}
@property (nonatomic,retain) UIView *inView;
@end
@implementation ViewController
@synthesize inView;
- (void) dealloc {
[_txtField release];
if ([_webView isLoading]) {
[_webView stopLoading];
[_webView release];
}
[super dealloc];
}
- (void) inputOrAccessory: (id) sender {
if (_txtField.inputView == nil) {
_txtField.inputView = [self inView];
_txtField.inputAccessoryView = nil;
} else {
_txtField.inputView = nil;
_txtField.inputAccessoryView = [self inView];
}
[_txtField reloadInputViews];
if ([_txtField isFirstResponder]) {
[_txtField resignFirstResponder];
[_txtField becomeFirstResponder];
}
}
- (void) viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"demo";
self.navigationItem.rightBarButtonItem = BARBUTTONITEM(@"input/Accessory", @selector(inputOrAccessory:));
_txtField = [[UITextField alloc] initWithFrame:CGRectMake(10, 25, 300, 35)];
_txtField.delegate = self;
_txtField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:_txtField];
_webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 65, 300, 400)];
[self.view addSubview:_webView];
[_webView setDelegate:self];
[_webView loadHTMLString:@"<input type=\"text\" size=\"30\" autocorrect=\"off\" autocapitalization=\"on\">default keyboard</input><br><input type=\"tel\">tel keyboard</input>" baseURL:nil];
_txtField.inputView = [self inAView];
}
- (void) completeCurrentWord:(id) sender {
[[UIDevice currentDevice] playInputClick];
}
- (UIView *)inAView {
if (!inView) {
CGRect accessFrame = CGRectMake(0.0, 0.0, 320.0, 50.0);
inView = [[KeyboardAccessoryView alloc] initWithFrame:accessFrame];
inView.backgroundColor = [UIColor blueColor];
UIButton *compButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
compButton.frame = CGRectMake(80, 10.0, 160, 30.0);
[compButton setTitle: @"watsy0007" forState:UIControlStateNormal];
[compButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[compButton addTarget:self action:@selector(completeCurrentWord:)
forControlEvents:UIControlEventTouchUpInside];
[inView addSubview:compButton];
}
return inView;
}
- (void) viewDidUnload {
if ([_webView isLoading]) {
[_webView stopLoading];
[_webView release];
}
[super viewDidUnload];
}
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
- (void) showWeb:(id) sender {
[_txtField resignFirstResponder];
NSString *sURL = nil;
if (![_txtField.text hasPrefix:@"http://"]) {
sURL = [NSString stringWithFormat:@"http://%@",_txtField.text];
} else {
sURL = _txtField.text;
}
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:sURL]]];
}
#pragma mark -
#pragma mark field delegate
- (void) textFieldDidBeginEditing:(UITextField *)textField {
if (textField == _txtField) {
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 45, 21)];
lbl.backgroundColor = [UIColor clearColor];
lbl.textColor = [UIColor lightGrayColor];
lbl.text = @"url:";
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.leftViewMode = UITextFieldViewModeAlways;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.leftView = lbl;
[lbl release];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[button setTitle:@"Go" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
textField.rightView = button;
[button addTarget:self action:@selector(showWeb:) forControlEvents:UIControlEventTouchUpInside];
textField.rightViewMode = UITextFieldViewModeAlways;
[button release];
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
if (textField == _txtField) {
textField.leftView = nil;
textField.rightView = nil;
}
// remainder of implementation....
}
#pragma mark -
#pragma mark web view delegate
- (void) webViewDidFinishLoad:(UIWebView *)webView {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void) webViewDidStartLoad:(UIWebView *)webView {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(@"error :%@",error);
}
@end
//-----------------------------------------------------------------------------------------------------
#pragma mark -
#pragma mark AppDelegate
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIViewController *viewController;
@end
@implementation AppDelegate
@synthesize window = _window;
@synthesize viewController = _viewController;
- (void) dealloc {
[_window release];
[_viewController release];
[super dealloc];
}
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
srand(time(NULL));
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[ViewController alloc] init];
UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = controller;
[controller release];
[self.window makeKeyAndVisible];
return YES;
}
@end
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}