1、内容回顾内容回顾 代码示例:这样的代码会不会产生内存泄露?-(void)viewDidUnload self.statusText=nil;将self.statusTextz的引用计数减一(发送releas),-将Nil的引用计数加一!-(void)dealloc statusText release;super dealloc;内容回顾内容回顾 看一下属性声明:#import interface Button_FunViewController:UIViewController UILabel*statusText;property(nonatomic,retain)IBOutlet UIL
2、abel*statusText;-(IBAction)buttonPressed:(id)sender;end内容回顾内容回顾 代码示例:void setStatusText:(UILabel*)aLabel aLabel retain;statusText release;statusText=aLabel;本章授课内容本章授课内容vTarget-ActionTarget-Actionv课课堂堂实验实验Target-ActionvTarget-Action:Target-Action:Target-Action是一种设计模式,在这个设计模式中,一个对象保留了一些必要信息,当某种事件发生时将消
3、息发送给另外一个对象。这些必要的信息包含两部分的内容:一部分是一个Selector,用来标识要调用的Action;另一部分是Target,也就是接受消息的对象。Target-ActionTarget-Action 按钮包含了target和actionNSButtonaction=seed:Foo-(void)seed:(id)sendertargetTarget-Action 当用户和控件交互时,控件就会给它们的target发送action消息。例如:点击一个按钮,将会给它的target发送action消息:Action方法接受一个参数:发送者。该参数可以让接收者知道是哪一个控件发送了这个ac
4、tion消息。NSButtonaction=seed:Foo-(void)seed:(id)sendertargetseed:-(IBAction)toggleFoo:(id)sender BOOL isOn=sender state;Target-Action Target-Action是UIControl类中定义的一种机制,凡是UIControl子类的对象都可以通过设置target和action来通过消息触发某个操作。Target-Action UIControl类的声明interface UIControl:UIView NSMutableArray*_targetActions;CGP
5、oint _previousPoint;CFAbsoluteTime _downTime;-(void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;-(void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;-(NSArray*)actionsForTarget:(id)target forControlEvent:(UICont
6、rolEvents)controlEvent;-(void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent*)event;-(void)sendActionsForControlEvents:(UIControlEvents)controlEvents;endTarget-ActionNSButtonNSMutableArray*_targetActions;targetactionidmethodidmethodidmethodidmethodobjobjobjobjTarget-Action UIControlEventsenu
7、m UIControlEventTouchDown =1 0,UIControlEventTouchDownRepeat =1 1,UIControlEventTouchDragInside =1 2,UIControlEventTouchDragOutside =1 3,UIControlEventTouchDragEnter =1 4,UIControlEventTouchDragExit =1 5,UIControlEventTouchUpInside =1 6,UIControlEventTouchUpOutside =1 7,UIControlEventTouchCancel =1
8、8,;Target-ActionNSButtonaction=buttonPressed:Button_FunViewController-(void)seed:(id)sendertargetNSButtonaction=buttonPressed:targetNSLabeltextTarget-Action 通过代码设置Target和Action 可以通过UIControl的方法触发操作以响应事件:SEL sel;sel=selector(buttonPressed:);btn addTarget:someObjectWithbuttonPressedMethod Action:sel f
9、orEvent:UIControlEventTouchUpInside;(void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent*)event;target PerformSelector:action withObject:self;本章授课内容本章授课内容vTarget-ActionTarget-Actionv课课堂堂实验实验课堂实验课堂实验v实验实验目的:目的:了解通过代码创建对象的流程 理解Target-Action模式课堂实验课堂实验v创创建工程:建工程:创建基于视图的工程 将工程名命名为DyButton课堂实验课堂实验
10、v修改代修改代码码:打开DyButtonViewController.cpp修改修改viewDidLoad方法:viewDidLoad方法会在viewController将与之关联的视图加载到内存种后调用,负责进一步的初始化工作。-(void)viewDidLoad super viewDidLoad;UIButton*btn=UIButton buttonWithType:UIButtonTypeRoundedRect;btn.frame=CGRectMake(110,225,100,30);btn setTitle:Click forState:UIControlStateNormal;s
11、elf.view addSubview:btn;课堂实验课堂实验课堂实验课堂实验 当button被手动创建出来后,无法执行任何自定义操作。所以,我们必须设定button的target和action.对于应用程序来说,控件的target一般都是与控件所在视图的viewController的子类的对象,但也可以是其他的自定义类型的对象。课堂实验课堂实验 添加操作方法:打开文件DyButtonViewController.h文件,添加操作方法的声明#import interface DyButtonViewController:UIViewController-(IBAction)buttonPre
12、ssed:(id)sender;end课堂实验课堂实验 打开DyButtonViewController.cpp实现操作函数:-(IBAction)buttonPressed:(id)sender int x=arc4random()%220;int y=arc4random()%450;UIButton*btn=UIButton buttonWithType:UIButtonTypeRoundedRect;btn.frame=CGRectMake(x,y,100,25);btn setTitle:Click forState:UIControlStateNormal;self.view ad
13、dSubview:btn;btn addTarget:self action:selector(buttonPressed:)forControlEvents:UIControlEventTouchUpInside;课堂实验课堂实验课堂实验课堂实验v继续修改当前的应用程序使控件能够响应拖动事件。事件就是一个对象用来保存硬件监测到的用户输入。当系统监测到用户输入后就将用户输入的信息封装成UIEvent的对象,发送给当前处于活动状态的应用程序。课堂实验课堂实验课堂实验课堂实验 UIEvent种又包含了好多UITouch对象,用来存储触摸事件的详细内容课堂实验课堂实验 继续修改DyButtonVie
14、wController.h文件,添加新的操作函数的声明:#import interface DyButtonViewController:UIViewController-(IBAction)buttonPressed:(id)sender;-(IBAction)buttonMoved:(id)sender forEvent:(UIEvent*)event;end课堂实验课堂实验 修改DyButtonViewController.cpp文件实现新的操作函数:-(IBAction)buttonMoved:(id)sender forEvent:(UIEvent*)event NSSet*set=
15、event allTouches;UITouch*touch=set allObjects objectAtIndex:0;CGPoint currentPoint=touch locationInView:self.view;CGPoint lastPoint=touch previousLocationInView:self.view;UIButton*btn=(UIButton*)sender;CGRect frame=btn.frame;btn.frame=CGRectOffset(frame,currentPoint.x-lastPoint.x,currentPoint.y-last
16、Point.y);课堂实验课堂实验 修改viewDidLoad函数:-(void)viewDidLoad super viewDidLoad;UIButton*btn=UIButton buttonWithType:UIButtonTypeRoundedRect;btn.frame=CGRectMake(110,225,100,30);btn setTitle:Click forState:UIControlStateNormal;self.view addSubview:btn;btn addTarget:self action:selector(buttonPressed:)forCont
17、rolEvents:UIControlEventTouchDownRepeat;btn addTarget:self action:selector(buttonMoved:forEvent:)forControlEvents:UIControlEventTouchDragInside;课堂实验课堂实验 修改buttonPressed函数:-(IBAction)buttonPressed:(id)sender int x=arc4random()%220;int y=arc4random()%450;UIButton*btn=UIButton buttonWithType:UIButtonTy
18、peRoundedRect;btn.frame=CGRectMake(x,y,100,30);btn setTitle:Click forState:UIControlStateNormal;self.view addSubview:btn;btn addTarget:self action:selector(buttonPressed:)forControlEvents:UIControlEventTouchDownRepeat;btn addTarget:self action:selector(buttonMoved:forEvent:)forControlEvents:UIControlEventTouchDragInside;Thanks