zoukankan      html  css  js  c++  java
  • UIButton set touch handler in code

    One option is to set the button up using

    [myButton addTarget:yourOtherClass action:@selector(mySelector:) forControlEvents:UIControlEventTouchUpInside];

    but this is a bit dangerous because target is not retained so you could send the message to a deallocated object.

    You could instead set up a protocol

    PopupViewController.h
    #import <UIKit/UIKit.h>

    @class PopupViewController;

    @protocol PopupViewControllerDelegate
    - (void)viewController:(PopupViewController *)controller viewTapped:(UIView *)view;
    @end

    @interface PopupViewController : UIViewController

    @property (nonatomic, assign) NSObject<PopupViewControllerDelegate> *delegate;

    - (IBAction)viewTapped:(UIView *)view;

    @end

    Then in your implementation

    PopupViewController.m
    - (IBAction)viewTapped:(UIView *)view {
        if (!self.delegate) return;
        if ([self.delegate respondsToSelector:@selector(viewController:viewTapped:)]) {
            [self.delegate viewController:self viewTapped:view];
        }
    }

    As the method defined in the protocol was not optional I could have instead done a check for (self.delegate) to make sure it is set instead of respondsToSelector.

  • 相关阅读:
    Jmeter之断言处理
    JMeter工具简单介绍
    TCP的三次握手和四次挥手
    浅谈cookie、session
    浅谈HTTP中Get与Post的区别
    DNS原理入门
    互联网协议简介
    测试与部署
    部分代码片段
    apache和php扩展问题
  • 原文地址:https://www.cnblogs.com/welhzh/p/4323057.html
Copyright © 2011-2022 走看看