zoukankan      html  css  js  c++  java
  • objective-c里的方法指针IMP的用法

    SGPopSelectView.h

    @interface SGPopSelectView : UIView

    @property (nonatomic, assign) SEL selector;

    @property (nonatomic, assign) IMP functionIMP;

    @end

     

    ------------------------------------------------------------------------------------------------------------------------

    SGPopSelectView.m

    @interface SGPopSelectView () <UITableViewDelegate, UITableViewDataSource>

    @property (nonatomic, strong) UITableView *tableView;

    @end 

    @implementation SGPopSelectView

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    {  

        _selectedIndex = indexPath.row;

        [tableView reloadData];    

        self.functionIMP(self, _selector, indexPath.row);//当选择菜单的某一项时,调用方法指针指向的方法

    }

    ------------------------------------------------------------------------------------------------------------------------

    UIHotWordViewController.h

    #import <UIKit/UIKit.h>

    #import "REMenu.h"

    #import "SGPopSelectView.h"

     

    @interface UIHotWordViewController : UIViewController

    @property (strong,nonatomic) REMenu *menu;

    @property (retain, nonatomic) IBOutlet UIButton *btnHotWord;

    @property (nonatomic, strong) SGPopSelectView *popView;

    @property (nonatomic, strong) NSArray *selections;

    @property (nonatomic, retainNSString *r;

     @end

    ------------------------------------------------------------------------------------------------------------------------

    UIHotWordViewController.m

    - (void)viewDidLoad {

        [super viewDidLoad];    

        self.selections = @[@"句子",@"作品",@"作者"];

        self.popView = [[SGPopSelectView alloc] init];

        self.popView.selector = @selector(rows_selected:);//指定selector

        self.popView.functionIMP = [self methodForSelector:@selector(rows_selected:)];//指定函数指针

    }

    -(void) rows_selected:(NSInteger) idx {

        NSLog(@"选择了项%d", idx);//选择菜单后会调用此方法

    }

  • 相关阅读:
    前后端交互, 安装drf, restful接口规范, pycharm断点调试
    django中文设置, axios, CORS, 全局js配置, Vue配置jq + bs
    js原型, Vue项目环境搭建, Vue项目目录结构, Vue项目生命周期, 小组件使用, 全局样式, 路由跳转, 组件的生命周期钩子, 路由传参
    Vue组件
    Vue表单指令, 条件指令, 循环指令, 成员
    question1 赋值运算操作符
    CH15 面向对象程序设计
    CH12 动态内存
    CH11 关联容器
    CH10 泛型算法
  • 原文地址:https://www.cnblogs.com/yuanxiaoping_21cn_com/p/4362241.html
Copyright © 2011-2022 走看看