[IOS笔记]常见控件的监听事件

来源:互联网 发布:asp网站源码修改 编辑:程序博客网 时间:2024/05/18 20:35


////  ViewController.m//  常见控件的监听////  Created by cdj on 17/9/10.//  Copyright © 2017年 ue. All rights reserved.//#import "ViewController.h"@interface ViewController () <UIScrollViewDelegate, UITextFieldDelegate>@property (weak, nonatomic) IBOutlet UITextField *textFiled;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];//    1按键的监听    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];    button.frame = CGRectMake(100, 100, 150, 40);    [button setBackgroundColor:[UIColor redColor]];    //    因为继承了ivewControll,所以可以用addTarget    [button addTarget:self action:@selector(bClick:) forControlEvents:UIControlEventTouchUpInside];        [self.view addSubview:button];        //    2scrollView的监听    UIScrollView *scrollView = nil;    scrollView.delegate = self;    //    3.分段按键,按键栏    UISegmentedControl *s = [[UISegmentedControl alloc] initWithItems:@[@"111",@"222",@"333"]];    s.center = CGPointMake(self.view.center.x, 200);    s.selectedSegmentIndex = 0; //默认选择第一个        [s addTarget:self action:@selector(sClick:) forControlEvents:UIControlEventValueChanged];        [self.view addSubview:s];    //    4文本输入框:手动拖入到故事版中了:代理和addTarget都可以监听    self.textFiled.delegate = self;/*    [self.textFiled addTarget:self action:@selector(tfEditingDidBegin) forControlEvents:UIControlEventEditingDidBegin];    [self.textFiled addTarget:self action:@selector(tfEditingDidEnd) forControlEvents:UIControlEventEditingDidEnd];    [self.textFiled addTarget:self action:@selector(tfEditingChanged:) forControlEvents:UIControlEventEditingChanged];*/    }/** *  按键的监听事件 * *  @param btn <#btn description#> */-(void)bClick:(UIButton *)btn{    NSLog(@"btnClick");}/** *  分段按键的监听事件 * *  @param s <#s description#> */-(void)sClick:(UISegmentedControl *)s{    NSLog(@"sClick %d", s.selectedSegmentIndex);}/** *  textFiled输入框的代理监听事件 * *  @param textField <#textField description#> */-(void)textFieldDidBeginEditing:(UITextField *)textField{    NSLog(@"开始编辑");}-(void)textFieldDidEndEditing:(UITextField *)textField{    NSLog(@"结束编辑");}/** *  //代理状态下,文字改变将调用 * *  @param textField <#textField description#> *  @param range     <#range description#> *  @param string    输入的单个字符 * *  @return YES允许输入,NO禁止输入 */-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{        NSLog(@"shouldChangeCharactersInRange=%@", string);    if ([string isEqualToString:@"1"]) {        return NO;    }    return YES;}-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//    退出键盘的三种方式,最后一种最常用//    [self.textFiled endEditing:YES];//结束编辑,退出编辑//    [self.textFiled resignFirstResponder];//不再是第一响应者    [self.view endEditing:YES]; //让父控件结束编辑}/** *  textFiled输入框的addTarget事件 */-(void)tfEditingDidBegin{    NSLog(@"开始编辑2");}-(void)tfEditingDidEnd{    NSLog(@"结束编辑2");}-(void)tfEditingChanged:(UITextField *)tf{    NSLog(@"改变编辑2=%@", tf.text);}@end














































原创粉丝点击