[Basic iOS control and basic ios Control
A. DefinitionThe iOS program has an nsicationicationcenter singleton object, which is used to publish notifications between different objects. Any object can publish notifications in nsicationicationcenter to release its own events. listener (Observer) you can choose to receive a specific notification. 1 // 2 // Radio. h 3 // Notification 4 // 5 // Created by hellovoidworld on 14/12/7. 6 // Copyright (c) 2014 hellovoidworld. all rights reserved. 7 // 8 9 # import <Foundation/Foundation. h> 10 11 @ interface Radio: NSObject12 13 // Radio station name 14 @ property (nonatomic, copy) NSString * name; 15 16 @ end
1 // 2 // Radio. m 3 // Notification 4 // 5 // Created by hellovoidworld on 14/12/7. 6 // Copyright (c) 2014 hellovoidworld. all rights reserved. 7 // 8 9 # import "Radio. h "10 11 @ implementation Radio12 13 @ end
1 // 2 // main. m 3 // Notification 4 // 5 // Created by hellovoidworld on 14/12/7. 6 // Copyright (c) 2014 hellovoidworld. all rights reserved. 7 // 8 9 # import <Foundation/Foundation. h> 10 # import "Radio. h "11 # import" Person. h "12 13 int main (int argc, const char * argv []) {14 @ autoreleasepool {15 Radio * r1 = [[Radio alloc] init]; 16 r1.name = @ ""; 17 18 Radio * r2 = [[Radio alloc] init]; 19 r2.nam E = @ "enemy station"; 20 21 // only receives messages from our station 22 Person * p1 = [Person alloc] init]; 23 p1.role = @ "our loyal soldiers"; 24 25 // can only receive messages from enemy stations 26 Person * p2 = [Person alloc] init]; 27 p2.role = @ "enemy's hateful dog"; 28 29 // The skill can receive messages from our station and 30 persons * p3 = [[Person alloc] init]; 31 p3.role = @ ""; 32 33 // Well, I am the medium for transmitting radio waves 34 nsicationicationcenter * center = [nsicationicationcenter defacenter center]; 35 36 // 1. add listener 37 [center addObserv Er: p1 selector: @ selector (receiveInfoFromRadio :) name: @ "attack" object: r1]; 38 [center addObserver: p2 selector: @ selector (receiveInfoFromRadio :) name: @ "defend" object: r2]; 39 [center addObserver: p3 selector: @ selector (receiveInfoFromRadio :) name: @ "attack" object: r1]; 40 [center addObserver: p3 selector: @ selector (receiveInfoFromRadio :) name: @ "defend" object: r2]; 41 42 // 2. our radio station r1 released Attack Information 43 [center postNotifi CationName: @ "attack" object: r1 userInfo: @ {@ "title": @ "attack !, All the money belong to us! ", @" Title2 ": @" And the girls !!! "}]; 44 45 // 3. enemy station r2 released Anti-DDoS information 46 [center postNotificationName: @ "defend" object: r2 userInfo: @ {@ "title": @ "TP back quickly! "}]; 47 48} 49 return 0; 50}
Out:
21:18:03. 142 Notification [7923: 714964]
I am our loyal warrior
,
Receiving messages from our radio station
,
Content is
{
Title = "attack !, All the money belong to us! ";
Title2 = "And the girls !!! ";
}
21:18:03. 143 Notification [7923: 714964]
I am a zombie.
,
Receiving messages from our radio station
,
Content is
{
Title = "attack !, All the money belong to us! ";
Title2 = "And the girls !!! ";
}
21:18:03. 143 Notification [7923: 714964]
I am an enemy's dog.
,
Receiving messages from enemy Stations
,
Content is
{
Title = "TP back quickly! ";
}
21:18:03. 143 Notification [7923: 714964]
I am a zombie.
,
Receiving messages from enemy Stations
,
Content is
{
Title = "TP back quickly! ";
}Program ended with exit code: 0