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.

  • 相关阅读:
    [go]go addressable 详解
    [go]灵活的处理json与go结构体
    [django]django内置的用户模型
    [go]文件读写&io操作
    *2.3.2_加入env
    UVM_INFO
    uvm_config_db在UVM验证环境中的应用
    *2.2.4 加入virtual interface
    *2.2.3 加入objection机制
    2.2.2 加入factory机制
  • 原文地址:https://www.cnblogs.com/welhzh/p/4323057.html
Copyright © 2011-2022 走看看