#import <UIKit/UIKit.h> @interface FirstViewController : UIViewController<UITextFieldDelegate> { UITextField *textField1; UITextField *textField2; UISegmentedControl *_prevNext; } - (UIToolbar *)createActionBar; @end
// // FirstViewController.m // MyTableViewDemo // // Created by Chocolate on 13-9-12. // Copyright (c) 2013年 Chocolate. All rights reserved. // #import "FirstViewController.h" @interface FirstViewController () @end @implementation FirstViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; textField1 = [[UITextField alloc]initWithFrame:CGRectMake(10, 20, 300, 40)]; [textField1 setBorderStyle:UITextBorderStyleRoundedRect]; [textField1 setReturnKeyType:UIReturnKeyNext]; [textField1 setDelegate:self]; textField2.inputAccessoryView = [self createActionBar]; [textField1 setTag:1]; [textField1 setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter]; [self.view addSubview:textField1]; textField2 = [[UITextField alloc]initWithFrame:CGRectMake(10, 80, 300, 40)]; [textField2 setBorderStyle:UITextBorderStyleRoundedRect]; [textField2 setReturnKeyType:UIReturnKeyDone]; [textField2 setDelegate:self]; textField2.inputAccessoryView = [self createActionBar]; [textField2 setTag:2]; [textField2 setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter]; [self.view addSubview:textField2]; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { switch (textField.tag) { case 1: [textField2 becomeFirstResponder]; break; case 2: [textField2 resignFirstResponder]; break; default: break; } return YES; } -(UIToolbar *)createActionBar { UIToolbar *actionBar = [[UIToolbar alloc] init]; actionBar.translucent = YES; [actionBar sizeToFit]; actionBar.barStyle = UIBarStyleBlackTranslucent; UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done", @"") style:UIBarButtonItemStyleDone target:self action:@selector(handleActionBarDone:)]; _prevNext = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:NSLocalizedString(@"Previous", @""), NSLocalizedString(@"Next", @""), nil]]; _prevNext.momentary = YES; _prevNext.segmentedControlStyle = UISegmentedControlStyleBar; _prevNext.tintColor = actionBar.tintColor; [_prevNext addTarget:self action:@selector(handleActionBarPreviousNext:) forControlEvents:UIControlEventValueChanged]; UIBarButtonItem *prevNextWrapper = [[UIBarButtonItem alloc] initWithCustomView:_prevNext]; UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; [actionBar setItems:[NSArray arrayWithObjects:prevNextWrapper, flexible, doneButton, nil]]; return actionBar; } - (void)handleActionBarPreviousNext:(UISegmentedControl *)control { const BOOL isNext = control.selectedSegmentIndex == 1; if (isNext) { NSLog(@"next"); } else { NSLog(@"previous"); } [control setSelectedSegmentIndex:UISegmentedControlNoSegment]; } - (BOOL)handleActionBarDone:(UIBarButtonItem *)doneButton { return NO; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end