Answer to the question of ILeW with an article about what the real, attaching such as notifications and delegates work. Using a pattern we see first how the delegate pattern:

An object in search of a delegate
Answer to the question of ILeW with an article about what the real, attaching such as notifications and delegates work. Using a pattern we see first how the delegate pattern:

An object in search of a delegate
Xcode 4 allows you to develop applications for iPhone or iPad with a target lower than today's iPhone OS 4.3. However the same code that comes with templates "sin" of arrogance, assuming that our application will target iPhone OS 4 or higher. Delegated in the application, for example, Xcode inserts the following lines of code:
1 2 3 4 5 6 7 | BOOL ) application : ( UIApplication * ) application didFinishLaunchingWithOptions : ( NSDictionary * ) launchOptions - (BOOL) application: (UIApplication *) application didFinishLaunchingWithOptions: ( NSDictionary *) launchOptions { / / Override point for customization after application launch. self.window.rootViewController = self.viewController; ; [Self.window makeKeyAndVisible]; ; return YES; } |
è estremamente versatile, come già visto in setAnimationDidStopSelector: usi diversi ed avanzati . The setting of the end of animation, executed with setAnimationDidStopSelector , a class of UIView is extremely versatile, as seen in setAnimationDidStopSelector: different uses and advanced .
Let me now show you a technique very convenient when we have multiple animations and / or want to manage the queues (queue) of animations.
In Very short snippet: Remove scroll from a UIWebView object we saw how it was possible to remove the scroll from an object UIWebView :
The messages (methods) defined in the application (Application delegated) main can be invoked quickly by using performSelector :
1 2 | delegate ] [[[UIApplication sharedApplication] delegate] ( applicationMessage ) ] ; performSelector: @ selector (applicationMessage)]; |
Documented or undocumented, a feature, sometimes, you are forced to implement by force! Useful in some situations, the scroll is to remove an object from UIWebView . : Interestingly, in general, see how you can access the components "inside" part of the object in question: in this example retrieves the object pointer UIScrollView present in the subject UIWebView :
Objective-C is a wonderful language that allows you to do amazing things. One of the most interesting aspects is its dynamic invocation of methods (messages). You can, in fact, get the address of a message from a string.
In most cases, or because we are used or because we have seen in tutorials and in some texts, we use the setAnimationDidStopSelector in this manner:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | nil context : NULL ] ; [UIView beginAnimations: nil context: NULL]; 1.5 ] ; [UIView setAnimationDuration: 1.5]; UIViewAnimationCurveEaseInOut ] ; [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut]; self ] ; [UIView setAnimationDelegate: self]; @selector ( removeView ) ] ; [UIView setAnimationDidStopSelector: @ selector (removeView)]; ; myView.alpha = 0; ; [UIView commitAnimations]; / / void ) removeView { - (Void) {removeView ; [MyView removeFromSuperview]; } |
come delegato e tramite la setAnimationDidStopSelector gli invia un messaggio removeView quando l'animazione è terminata. In the code above the setAnimationDelegate set self as delegate and through setAnimationDidStopSelector sends a message removeView when the animation is finished. The code itself is correct, however, makes use of a message definition ( removeView ) that could be omitted. Now, here is the same code, with the same effect, without the message removeView :
1 2 3 4 5 6 7 8 9 | nil context : NULL ] ; [UIView beginAnimations: nil context: NULL]; 1.5 ] ; [UIView setAnimationDuration: 1.5]; UIViewAnimationCurveEaseInOut ] ; [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut]; myView ] ; [UIView setAnimationDelegate: myView]; @selector ( removeFromSuperview ) ] ; [UIView setAnimationDidStopSelector: @ selector (removeFromSuperview)]; ; myView.alpha = 0; ; [UIView commitAnimations]; |
! The interesting thing about this approach is that myView could be a subclass of UIView ! It may therefore be a custom class with our own messages and, as stated, easily callable from setAnimationDidStopSelector . In addition, the setAnimationDidStopSelector selectors agree with parameters:
1 2 3 4 5 6 7 8 9 | nil context : NULL ] ; [UIView beginAnimations: nil context: NULL]; 1.5 ] ; [UIView setAnimationDuration: 1.5]; UIViewAnimationCurveEaseInOut ] ; [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut]; myView ] ; [UIView setAnimationDelegate: myView]; @selector ( myMessage : param1 : ) ] ; [UIView setAnimationDidStopSelector: @ selector (myMessage: param1:)]; ; myView.alpha = 0; ; [UIView commitAnimations]; |
This example can be extended to all cases here where we set a delegate, Atro is not a pointer to an instance of any object.
Even in the most simple tutorial you can find the use of protocols. It will certainly happen to many in your view controller to use a protocol, inputting, next to the definition of the interface, a statement similar to:
1 2 3 | UIViewController <uiwebviewdelegate> { @ Interface myViewController: UIViewController {<uiwebviewdelegate> ... } |
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 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.
Latest Comments
Mark : @ Mark: Thanks for the answer. For example, if you have a code to save / use the folder ...
Mark : Thanks for the answer. For example, if you have a code to save / use the folder ...
Giovambattista Fazioli : @ Mark: in principle, absolutely, a lot depends on how and what to download. La. ..
Mark : Hello, good guide! I wanted to ask, you can use the same code to run a sql database? Instead of ...
Joseph : Hi I wanted to ask you how you can do in the post I uploaded a gallery of 50 images, for example, ...