Artikel mit Tag 'UIApplicationDelegate'

iPhone: Erstellen Sie eine Methode, um auf Ereignisse zu reagieren Erster Klasse

Normalerweise wird ein Ereignis, das nicht mehr als eine Nachricht, wird sie aufgelöst (eingestellt und umgesetzt) ​​in der gleichen Klasse oder Kontext, der Funktion oder Prozedur "Berufung". o in un UIViewController . Zum Beispiel, wenn wir eine Schaltfläche hinzufügen UIButton über Code (programmatisch), können wir unsere Klasse innerhalb eines finden UIView oder UIViewController . In beiden Fällen wird der Betrieb von der Verteilung und der Initialisierung der Taste wird durch die Einstellung der gefolgt werden target : die eine Nachricht erhalten soll, wenn es "Klick" auf den Button, Typ

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: @ "Drück mich" fürState: UIControlStateNormal];
/ / Entscheiden Sie, wer die Nachricht erhalten UIControlEventTouchUpInside
self action : @selector ( onButtonClicked ) forControlEvents : UIControlEventTouchUpInside ] ; [AddTarget Taste: self Aktion: @ selector (OnButtonClicked) forControlEvents: UIControlEventTouchUpInside];
/ / ...
void ) onButtonClicked { - (Void) {OnButtonClicked
/ / ...
}

Zeile 5 der oben gezeigten Code entscheidet, wer (das Objekt) und was (das Verfahren) "Anruf", wenn unsere Taste gedrückt wird. Im obigen Beispiel ist bekannt, daß die Druckeinstellung der Nachricht an die Methode gesendet dargestellt onButtonClick umgesetzt unten, dann Teil des gleichen Kontext (oder Klasse). potremmo inviare il nostro messaggio ad un qualsiasi altro oggetto, posto quindi al difuori del contesto in uso. Der erste offensichtliche Überlegung, dass wir tun können, ist es daher, dass durch Verändern der Parameter self und action konnten wir unsere Botschaft an jedes andere Objekt senden, dann auch außerhalb des Kontextes, in Gebrauch. : Hier ist ein Beispiel: class UIApplicationDelegate schafft eine UIViewController :

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

associata al UIViewController stesso: Die SplashScreenController macht eine Methode, die Sie animieren können UIView mit der zugehörigen UIViewController selbst:

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 (@ "Zeichentrick abgeschlossen");
}

. Der obige Code zeigt eine Methode innerhalb der Klasse definiert SplashScreenController Typ UIViewController . ) onAnimationFinished definito più sotto, facente parte sempre della classe SplashScreenController . Es tut nichts animieren UIView animieren it down, und wenn die Animation beendet ist, rufen Sie die Methode (Schicke eine Nachricht an sich selbst - und damit self ) onAnimationFinished unten definiert, das ist ein Teil der Klasse immer SplashScreenController . , non saremo informati della fine dell'animazione: Daraus folgt, dass in unserem myAppDelegate , wenn wir die Methode aufrufen animateBackgroundDown , werden wir nicht das Ende der Animation informiert werden:

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

Was wir brauchen, ist vielmehr eine neue Version erstellen animateBackgroundDown als mir zu sagen, wo die Nachricht am Ende der Animation senden und welche Methode zu nennen. In der Praxis wollen wir sicherstellen, dass in der Lage sein in unserer Klasse schreiben 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 (@ "Zeichentrick abgeschlossen");
}

, bensì in myAppDelegate . Dieses Mal die Methode onAnimationFinished nicht in der Nähe UIViewController , aber in myAppDelegate . nel modo seguente: Um dies zu tun, ändern Sie einfach die Methode animateBackgroundDown in UIViewController wie folgt:

1
2
3
4
5
6
7
8
9
10
11
12
/ /
/ / SplashScreenController.m
/ /
void ) animateBackgroundDown : ( id ) target selector : ( SEL ) selector { - (Void) animateBackgroundDown: (id) Zielauswahlvorrichtung: (SEL) Selektor {
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: Selektor];
CGRect ) { 0 , 480 , 320 , 480 } ; self.view.frame = (CGRect) {0, 480, 320, 480};
; [UIView commitAnimations];
}

). Jetzt haben wir eine Methode, die den "Kontext" (dauert target ) und die Methode aufgerufen werden ( selector ). a qualsiasi altro “oggetto” / classe in grado di riceverlo. Nun, wenn die Animation endet, wird die Meldung AnimationDidStop werden Ihnen zugesandt werden myAppDelegate andere "Objekt" / Klasse in der Lage, es zu empfangen.

Fortsetzung ...