2015-04-28 15:57:39 u011759478 阅读数 1200
Return Key 修改成done

Auto-enable Return Key 勾选 没有输入的现实disabled

uitextfield 绑定

-(IBAction)uidDidEndOnExit:(id)sender;


-(IBAction)uidDidEndOnExit:(id)sender{

    [self connectFunction];

    [username  becomeFirstResponder];

}



2016-05-10 23:57:30 l2i2j2 阅读数 3115

场景:当你封装了一个控件类,这个类中封装进去了UITextField,这时获取不到textField属性,怎么办。

我以前都是这么写:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.textField resignFirstResponder];
}

当把UITextField封装进去时,如果把属性声明到.m文件中,你从外边拿不到属性,这个办法就不好使了。。。

其实有一个更好的实现方法,可以说是简单粗暴:

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

这个方法不仅可以解决在封装时拿不到textField属性的问题,还能简化代码,如果有多个textField,上面的方法就要添加多个resignFirstResponder方法,下面的方法,永远只要一行代码就能搞定。

2013-08-20 16:24:55 beautiful0101 阅读数 93

ios数字键盘添加完成按钮,示代码如下(附件中有效果图和示例工程代码):

 

//
//  ViewController.h
//  KeyboardTest
//  自定义数字键盘,添加完成按钮
//  Created by Dwen on 12-10-24.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
//定义完成按钮
@property (weak,nonatomic) UIButton *doneInKeyboardButton;
//普通键盘
- (IBAction)normalKeyboard:(id)sender;
//自定义键盘
 - (IBAction)defineKeyboard:(id)sender;

@end
//
//  ViewController.m
//  KeyboardTest
//  
//  Created by  on 12-10-24.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize doneInKeyboardButton;

- (void)viewDidLoad
{
    [super viewDidLoad];
    //注册键盘显示通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keywordboardShow) name:UIKeyboardDidShowNotification object:nil];
    //注册键盘隐藏通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keywordboardHide) name:UIKeyboardDidHideNotification object:nil];
}

//键盘显示
- (void)keywordboardShow{
    if (doneInKeyboardButton == nil){
        //初始化完成按钮
        doneInKeyboardButton = [UIButton buttonWithType:UIButtonTypeCustom];
        //获取主屏高度
        CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
        if(screenHeight==568.0f){//iphone5
            doneInKeyboardButton.frame = CGRectMake(0, 568 - 53, 106, 53);
        }else{//iphone4 3.5寸
            doneInKeyboardButton.frame = CGRectMake(0, 480 - 53, 106, 53);
        }
        //在按钮被禁用时,图像会被画的颜色深一些
        doneInKeyboardButton.adjustsImageWhenHighlighted = NO;
        //根据按钮不同状态设图片
        [doneInKeyboardButton setImage:[UIImage imageNamed:@"btn_done_up@2x.png"] forState:UIControlStateNormal];
        [doneInKeyboardButton setImage:[UIImage imageNamed:@"btn_done_down@2x.png"] forState:UIControlStateHighlighted];
        [doneInKeyboardButton addTarget:self action:@selector(finishAction) forControlEvents:UIControlEventTouchUpInside];
    }
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    if (doneInKeyboardButton.superview == nil){
        //完成按钮添加到window
        [tempWindow addSubview:doneInKeyboardButton];
    }
}

//键盘隐藏
- (void)keywordboardHide{
    if (doneInKeyboardButton.superview){
        //从视图中移除掉
        [doneInKeyboardButton removeFromSuperview];
    }
}

//点击完成按键
-(void)finishAction{
    //隐藏完成按钮
    doneInKeyboardButton.hidden = YES;
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];//关闭键盘
}

#pragma 文本
//普通键盘
- (IBAction)normalKeyboard:(id)sender {
    doneInKeyboardButton.hidden = NO;
}

//自定义键盘
- (IBAction)defineKeyboard:(id)sender {
    doneInKeyboardButton.hidden = YES;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}


@end

 

 

 

 

 

 

IOS开发经验分享

阅读数 38

ios reading

阅读数 23

没有更多推荐了,返回首页