zoukankan      html  css  js  c++  java
  • ios 给键盘上面加上“完成”

    #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
  • 相关阅读:
    1295. 统计位数为偶数的数字『简单』
    1281. 整数的各位积和之差『简单』
    697. 数组的度『简单』
    748. 最短完整词『简单』
    832. 翻转图像『简单』
    1446. 连续字符『简单』
    1455. 检查单词是否为句中其他单词的前缀『简单』
    1160. 拼写单词『简单』
    1304. 和为零的N个唯一整数『简单』
    1103. 分糖果 II『简单』
  • 原文地址:https://www.cnblogs.com/lihaibo-Leao/p/3316907.html
Copyright © 2011-2022 走看看