Articles Tagged 'message'

How to create your own protocol with delegated

Even in the most simple tutorial you can find the use of protocols. It will certainly happen to many of your view controller used in a protocol, inputting, next to the definition of the interface, a statement similar to:

1
2
3
UIViewController <uiwebviewdelegate> { @ Interface myViewController: UIViewController {<uiwebviewdelegate>
...
}

More ...

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" forState: 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 of the code shown above decides who (subject) and what (method) "call" when our button is pressed. In the example illustrated above is also known that the setting pressure of the message is sent to the method onButtonClick implemented below, and then forming 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 also outside of the context 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 terminated");
}

. 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 want, instead, is to create a new version of animateBackgroundDown as to tell him where to send the message end animation and which method to call. In practice we want to make sure you can 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 terminated");
}

, 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 accepts 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.

More ...


Stop SOPA