您的位置:首页 > 移动开发 > IOS开发

IOS之简单代理

2015-12-07 21:40 369 查看
#import "AppDelegate.h"

//要使用输入框的代理方法

//1.导入代理文字

//2.挂上代理

//3.实现代理的方法

//导入代理的名字

@interface
AppDelegate ()<UITextFieldDelegate>

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

/*

UITextField :->UIControl的子类->UIView的子类

enable tag background color

文本输入框

text:可以获得
或者改变 输入框的文字内容

placeholder:提示文字

textColor:文字颜色

fon:文字大小

textAlignment:对齐方式

adjustFontSizeToFitWidth:让文字根据宽度适配

minimumFontSize:设置
文字的最小字号

delegate:代理

clearButtonMode:设置清楚按钮
什么时候显示

UITextFieldViewModeNever,永远不显示

UITextFieldViewModeWhileEditing,当编辑的时候显示

UITextFieldViewModeUnlessEditing,不再编辑的时候显示

UITextFieldViewModeAlways
永远显示

leftView:左侧视图->输入框左侧视图

rightView:左侧视图->输入框左侧视图

并不是设置了
左侧右侧视图 就可以显示出来
需要配合使用下面属性

leftViewMode:设置什么情况下显示左侧视图

rightViewMode:设置什么情况下显示右侧视图

imputView: 键盘上面的视图

imputAccessoryview:键盘区域的视图

输入框的方法
(代理方法)

开始编辑的时候调用

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.

- (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder

结束编辑的时候调用

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end

- (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called

当文字内容发生改变的时候调用

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text

点击清除按钮的时候调用

- (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications)

点击return键的时候调用

- (BOOL)textFieldShouldReturn:(UITextField *)textField; // called when 'return' key pressed. return NO to ignore.

专为输入框准备的响应事件

UIControlEventEditingDidBegin = 1 << 16, // UITextField

UIControlEventEditingChanged = 1 << 17,

UIControlEventEditingDidEnd = 1 << 18,

UIControlEventEditingDidEndOnExit = 1 << 19, // 'return key' ending editing

borderstyle:输入框的样式

UITextBorderStyleNone,
没有样式

UITextBorderStyleLine,
黑线边框

UITextBorderStyleBezel,黑线阴影

UITextBorderStyleRoundedRect
圆角

*/

/*

注意点:

1、如果想使用代理方法
必须先导入代理

2、如果代理方法没有触发
看是否挂上了代理

在使用左右侧视图的时候
要配合左右视图的model来使用

*/

/*

keyboardType:键盘的样式

UIKeyboardTypeDefault, // Default type for the current input method.

UIKeyboardTypeASCIICapable, // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active

✮✮✮✮✮数字符号

UIKeyboardTypeNumbersAndPunctuation, // Numbers and assorted punctuation.

UIKeyboardTypeURL, // A type optimized for URL entry (shows . / .com prominently).

UIKeyboardTypeNumberPad, // A number pad (0-9). Suitable for PIN entry.

UIKeyboardTypePhonePad, // A phone pad (1-9, *, 0, #, with letters under the numbers).

, // A type optimized for entering a person's name or phone number.

UIKeyboardTypeEmailAddress,
邮箱地址类型 // A type optimized for multiple email address entry (shows space @ . prominently).

UIKeyboardTypeDecimalPad NS_ENUM_AVAILABLE_IOS(4_1), // A number pad with a decimal point.

UIKeyboardTypeTwitter NS_ENUM_AVAILABLE_IOS(5_0), // A type optimized for twitter text entry (easy access to @ #)

UIKeyboardTypeWebSearch NS_ENUM_AVAILABLE_IOS(7_0),网页搜索 // A default keyboard type with URL-oriented addition (shows space . prominently).

UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated

*/

[self.window
makeKeyAndVisible];

UITextField *accTextField=[[UITextField
alloc]initWithFrame:CGRectMake(10,
40, [UIScreen
mainScreen].bounds.size.width-20,
40)];

accTextField.backgroundColor=[UIColor
redColor];

accTextField.borderStyle=UITextBorderStyleRoundedRect;

//提示文字

accTextField.placeholder=@"请输入账号";

//清除按钮

accTextField.clearButtonMode=UITextFieldViewModeWhileEditing;

UIImageView *leftImageView=[[UIImageView
alloc]initWithFrame:CGRectMake(0,
0, 40,
40)];

leftImageView.image=[UIImage
imageNamed:@"1.jpg"];

accTextField.leftView=leftImageView;

accTextField.leftViewMode=
UITextFieldViewModeUnlessEditing;

UIView *inputV=[[UIView
alloc]initWithFrame:CGRectMake(0,
0, CGRectGetWidth([UIScreen
mainScreen].bounds),
40)];

inputV.backgroundColor=[UIColor
lightGrayColor];

// accTextField.inputView=inputV;

// accTextField.inputAccessoryView=inputV;

//想使用代理方法
必须挂上代理

accTextField.delegate=self;

accTextField.tag=101;

[self.window
addSubview:accTextField];

return
YES;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: