iPhone: Create a way to respond to events that cross class

Normally an event, which is nothing but a message is fixed (set and implemented) in the same class or context, the function or procedure "caller". o in un UIViewController . For example if we add a button UIButton via code (programmatically), we can find our class in a UIView or a UIViewController . In both cases the task allocation and initialization of the button will be followed by the setting of target that should receive a message when it "clicks" the button, type:

1
2
3
4
5
6
7
8
9
[ UIButton buttonWithType : UIButtonTypeRoundedRect ] ; UIButton * button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
10 , 180 , 300 , 30 ) ; bottone.frame = CGRectMake (10, 180, 300, 30);
@ "Press me" forState : UIControlStateNormal ] ; [Button setTitle: @ "Press me" Forst: UIControlStateNormal];
/ / Decide who should receive the message UIControlEventTouchUpInside
self action : @selector ( onButtonClicked ) forControlEvents : UIControlEventTouchUpInside ] ; [AddTarget button: self action: @ selector (onButtonClicked) forControlEvents: UIControlEventTouchUpInside];
/ / ...
void ) onButtonClicked { - (Void) {onButtonClicked
/ / ...
}

Line 5 decides who the code shown above (the object) and what (method) "call" when our button is pressed. In the example above is also known that the pressure setting of the message is sent to the method onButtonClick implemented below, then part of the same context (or class). potremmo inviare il nostro messaggio ad un qualsiasi altro oggetto, posto quindi al difuori del contesto in uso. The first consideration is obvious that we can do, then, is that by altering the parameters self and action we can send our message to any other object, then place in the context difuori in use. : Here's an example: a class UIApplicationDelegate create a UIViewController :

1
2
3
4
5
/ /
/ / MyAppDelegate.m
/ /
SplashScreenController alloc ] ; splashScreenController = [SplashScreenController alloc];
splashScreenController.view ] ; [Window addSubview: splashScreenController.view];

associata al UIViewController stesso: The SplashScreenController exposes a method that allows you to animate the UIView associated with UIViewController same:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/ /
/ / SplashScreenController.m
/ /
void ) animateBackgroundDown { - (Void) {animateBackgroundDown
nil context : nil ] ; [UIView beginAnimations: nil context: nil];
0.75 ] ; [UIView setAnimationDuration: 0.75];
UIViewAnimationCurveEaseInOut ] ; [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
self ] ; [UIView setAnimationDelegate: self];
@selector ( onAnimationFinished ) ] ; [UIView setAnimationDidStopSelector: @ selector (onAnimationFinished)];
CGRect ) { 0 , 480 , 320 , 480 } ; self.view.frame = (CGRect) {0, 480, 320, 480};
; [UIView commitAnimations];
}
/ / ...
void ) onAnimationFinished { - (Void) {onAnimationFinished
"Animazione terminata" ) ; NSLog (@ "Animation is finished");
}

. The above code shows a method defined inside the class SplashScreenController type UIViewController . ) onAnimationFinished definito più sotto, facente parte sempre della classe SplashScreenController . It does nothing but animate the UIView animating it down, and when the animation is complete, call (send a message to itself - hence self ) onAnimationFinished defined below, is part of the class always SplashScreenController . , non saremo informati della fine dell'animazione: It follows that in our myAppDelegate , when we invoke the method animateBackgroundDown , we will not be informed of the end of the animation:

1
2
3
4
/ /
/ / MyAppDelegate.m
/ /
; [SplashScreenController animateBackgroundDown];

What we might want, instead, is to create a new version of animateBackgroundDown that tell me where to send the message at the end of animation, and which method to call. In practice we want to make sure to write in our class myAppDelegate :

1
2
3
4
5
6
7
8
/ /
/ / MyAppDelegate.m
/ /
self selector : @selector ( onAnimationFinished ) ] ; [SplashScreenController animateBackgroundDown: self selector: @ selector (onAnimationFinished)];
/ / ...
void ) onAnimationFinished { - (Void) {onAnimationFinished
"Animazione terminata" ) ; NSLog (@ "Animation is finished");
}

, bensì in myAppDelegate . This time the method onAnimationFinished is not in the UIViewController , but in myAppDelegate . nel modo seguente: To do this simply change the method animateBackgroundDown in UIViewController as follows:

1
2
3
4
5
6
7
8
9
10
11
12
/ /
/ / SplashScreenController.m
/ /
void ) animateBackgroundDown : ( id ) target selector : ( SEL ) selector { - (Void) animateBackgroundDown: (id) target selector: (SEL) selector {
nil context : nil ] ; [UIView beginAnimations: nil context: nil];
0.75 ] ; [UIView setAnimationDuration: 0.75];
UIViewAnimationCurveEaseInOut ] ; [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
target ] ; [UIView setAnimationDelegate: target];
selector ] ; [UIView setAnimationDidStopSelector: selector];
CGRect ) { 0 , 480 , 320 , 480 } ; self.view.frame = (CGRect) {0, 480, 320, 480};
; [UIView commitAnimations];
}

). Now we have a method that takes the "context" ( target ) and the method to call ( selector ). a qualsiasi altro “oggetto” / classe in grado di riceverlo. Now, when the animation ends, the message AnimationDidStop will be sent to myAppDelegate any other "object" / class that can receive it.

There are no comments for this post

Leave a comment

XHTML TAG PERMIT: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL