iOS_target-action的使用

来源:互联网 发布:max内功版数据库转换 编辑:程序博客网 时间:2024/05/21 23:45


Target-Action的说明
    
常用的UIButton"addTarget:self action:@selector"的点击事件。
    
由于UIButton继承于UIControl, 所以UIControl下面的子类都有"addTarget:self action:@selector"的点击事件功能。
               
 
                |-- UIButton
                |-- UITextField
    UIControl---|-- UISlider
                |-- UISwitch
                |-- UIDatePicker
                |-- UIPageControl
                |-- UISegmentedControl
 
    Target-Action
的作用就是其他类使用类似Button的点击事件。
 
    
使用的方法:
    1
、自定义一个类
    2
、在自定义的类中,加入两个属性:
        @property (nonatomic, assign) id target;
        @property (nonatomic, assign) SEL action;
    3
、自定义的类的.m文件中。编写touch方法(相当于Button addTarget语句中的"forControlEvents:UIControlEventTouchUpInside" 语句,所有一般在touchesEnded事件中编写代码
 
        - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
        {
            // 
targetAction关联起来
            [self.target performSelector:self.action withObject:self];

        }
 
    4
、在要调用自定义类的ViewController中,实例化自定义的类(以下面的例子为例),然后设置自定义类的两个属性。其中的touchV.action = @selector(touchClicked:),相当于Button addtarget中的"action:@selector(clicked:)" 语句功能。
 
        touchV.target = self;
        touchV.action = @selector(touchClicked:);
 
    5. 
ViewController中定义touchClicked:事件。
    


0 0
原创粉丝点击