objective-c里的方法指针IMP的用法
2015-03-24 11:21
239 查看
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, retain) NSString *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);//选择菜单后会调用此方法
}
@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, retain) NSString *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);//选择菜单后会调用此方法
}
相关文章推荐
- Objective-C之foundation框架中的NSString的使用方法、指针基本用法、读取本地和远程文件
- iOS 关于Objective-C方法的IMP
- Objective-C(二、类与isa指针,类的封装,set&get,类方法,对象方法)——iOS开发基础
- Objective-C类成员变量的作用域、self的用法、私有方法
- Objective-C 和 C++中指针的格式和.方法 和内存分配
- Objective-C获取类方法和实例方法的IMP
- Objective-C 方法交换实践(二) - 方法指针交换
- Objective-C之Foundation框架中的NSDictionary的用法以及它的遍历方法
- 动态映射objective-c的对象方法修改空指针
- Objective-C方法注释的用法
- 09-Objective-C特有语法:万能指针id及构造方法
- NUnit详细使用用法(补充)--在.NET中如何利用NUnit测试Private和Protected方法
- ASP生成静态网页,学习CASE的用法,以及“权限”的一种控制方法
- 关于C++中函数指针的使用(包含对typedef用法的讨论)
- SetTimeOut方法用法示例
- 解析Linux内核获取当前进程指针的方法
- 间接调用指针所指的类成员的方法
- IsDbNull 方法的用法
- 关于获得MFC窗口其它类指针的方法(CSDN)
- ASP.NET的常用方法,用法。