UI04_Target-Action
2015-09-21 18:53
453 查看
AppDelegate.h
AppDelegate.m
RootViewController.h
RootViewController.m
MyButton.h
MyButton.m
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
AppDelegate.m
#import "AppDelegate.h" #import "RootViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (void)dealloc { [_window release]; [super dealloc]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; [_window release]; RootViewController *rootVC = [[RootViewController alloc] init]; self.window.rootViewController = rootVC; [rootVC release]; return YES; }
RootViewController.h
#import <UIKit/UIKit.h> @interface RootViewController : UIViewController @end
RootViewController.m
#import "RootViewController.h" #import "MyButton.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor whiteColor]; UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; [self.view addSubview:button]; button.frame = CGRectMake(100, 100, 150, 50); button.layer.borderWidth = 1; // 给按钮边框加上颜色 button.layer.borderColor = [UIColor purpleColor].CGColor; [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; // 创建MyButton MyButton *myButton = [[MyButton alloc] initWithFrame:CGRectMake(100, 200, 150, 50)]; [self.view addSubview:myButton]; [myButton release]; myButton.layer.borderWidth = 1; // 第六步: 调用addNewTarget: action:方法 [myButton addNewTarget:self action:@selector(test)]; } - (void)test { NSLog(@"测试"); } - (void)click:(UIButton *)button { }
MyButton.h
#import <UIKit/UIKit.h> @interface MyButton : UIView // 1. 写一个自定义的方法 - (void)addNewTarget:(id)target action:(SEL)action; // 2. 用两条属性保存事件的委托者是谁, 委托的事是什么 @property(nonatomic, assign)id target; @property(nonatomic, assign)SEL action; @end
MyButton.m
#import "MyButton.h" @implementation MyButton - (void)addNewTarget:(id)target action:(SEL)action { // 3. 用属性来保存两个参数的内容 self.target = target; self.action = action; } // 4. 触发的方法 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 5. 让myButton执行相应的方法 [self.target performSelector:self.action withObject:self]; } @end
相关文章推荐
- CALayer的常用属性(笔记与分享)
- UI_Touch
- mapreduce中的sequenceFile类,MapFile解析
- UINavigationController (导航控制器)
- UIWindow & UIWindowLevel笔记
- 励精图治---Concurrency---GUI设计
- UItableView一些小方法
- poj3581 Sequence(后缀数组sa的运用+离散化)
- ThreadPoolExecutor使用和思考-线程池大小设置与BlockingQueue的三种实现区别
- Opencv3.0+opencv_contrib_lib +VS2013(编译)+CMake-gui(最近的版本都可以)
- 判断 Fragment 的 UI 是否可见
- UML建模之时序图(Sequence Diagram)
- 【UIFramework】前端UI框架—导航条
- java中queue的使用
- requestWindowFeature()的应用
- android NotificationCompat.Builder 使用
- 15.Android 异步更新UI 技巧
- DBCP针对不同数据库的validationQuery
- 递归-快速排序quickSort
- 字符串 intValue、floatValue、doubleValue、longLongValue 方法可以正确转换的位数或者大小