(Habi House of Xiao Meow)
Little meow is sorting out the information recently. See the notice this east, can't help to share your own learning income.
What is notification
Notification (Notification) is an implementation of the observer pattern in the development framework, which is supported by the cocoa framework and is typically used to try to interact with the controller and the data model. Notifications enable you to send messages to one or more objects.
Inherit from NSObject, implement the Nscopying protocol protocol.
Notifications are broadcast to other objects through notification hubs (Nsnotificationcenter). The notification contains a name (name), an object property, and an optional dictionary (dictionary attribute) of three parameters. These parameters assist in the completion of the notification operation.
the principle of notification
Notification an object is very simple, is poster to provide observer information package. The Notification object has two important member variables: Name and object. The general name is used to uniquely indicate a notification object, and object is a pointer to the sender (poster) (to allow observer to call back to poster when notification is received), so notification has two methods:-( NSString *) name,-(ID) object is available for invocation. At the same time, the notification object also contains a parameter, which is a dictionary (optional parameter), which stores some information in the process of passing the value for the recipient to use. The system requires this parameter to be an immutable dictionary.
Once the above information is built, we can create a notification.
Nsnotification *notification = nil;
notification = [nsnotification notificationwithname:aname object:aobj userinfo:adictionary];
Where Aname is the name of the notification object, Aobj is the sender, and adictionary is the optional dictionary parameter for the notification.
When the notification is created, the notification can be sent when necessary, and when the notification is sent, a control center is required to send the notification, and the control center is the Notification Center (nsnotificationcenter). We can also create and send notifications through the control center, which is usually what we do.
Practical use of notifications
1. Page is not very relevant, but need to pass the information. These two pages as a sender, one as a listener;
2. Information needs to be sent to multiple locations. A page as the sender, multiple pages as listeners.
How to use notifications
1. Registration Notice
2. Send Notifications
3. Remove Notifications
Code implementation
Create a Viewcontroller and change it to MRC. Operate inside the. m file.
Read the registration Notice first
1- (void) Viewdidload {2 [Super Viewdidload];3UIView *mainview =[[[ UIView Alloc]initwithframe:[uiscreen mainscreen].bounds]autorelease];4Self.view =MainView;5 6UIButton *redbutton =[UIButton Buttonwithtype:uibuttontyperoundedrect];7Redbutton.frame = CGRectMake ( -, -, -, -);8[Redbutton Settitle:@"Red"Forstate:uicontrolstatenormal];9 [Redbutton addtarget:self Action: @selector (Setredcolor) forcontrolevents:uicontroleventtouchupinside];Ten [Self.view Addsubview:redbutton]; One AUIButton *bluebutton =[UIButton Buttonwithtype:uibuttontyperoundedrect]; -[BlueButton Settitle:@"Blue"Forstate:uicontrolstatenormal]; -Bluebutton.frame = CGRectMake ( -, Max, -, -); the [BlueButton addtarget:self Action: @selector (Setbluecolor) forcontrolevents:uicontroleventtouchupinside]; - [Self.view Addsubview:bluebutton]; - /** - * Registration Notice + */ -[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (dochange:) Name:@"Colorchange" Object: nil]; + A}
Send Notifications
1- (void) setbluecolor{2 /**3 * Send Notifications4 */5Nsdictionary *dic = [Nsdictionary dictionarywithobject:@"Bluecolor"Forkey:@"Color"];6[[Nsnotificationcenter Defaultcenter] Postnotificationname:@"Colorchange" Object: Self userinfo:dic];7 }8 9- (void) setredcolor{Ten /** One * Send Notifications A */ -Nsdictionary *dic = [Nsdictionary dictionarywithobject:@"Redcolor"Forkey:@"Color"]; -[[Nsnotificationcenter Defaultcenter]postnotificationname:@"Colorchange" Object: Self userinfo:dic]; the } - -- (void) Dochange: (Nsnotification *) notification{ -Nsdictionary *dic =[Notification UserInfo]; +NSString *colorstr = [dic valueforkey:@"Color"]; -Uicolor *color =Nil; + A if([Colorstr isequaltostring:@"Bluecolor"]) { atcolor =[Uicolor Bluecolor]; - } - if([Colorstr isequaltostring:@"Redcolor"]) { -color =[Uicolor Redcolor]; - } -Self.view.backgroundColor =color; in}
Remove notifications
1 -(void) dealloc{2/* *3 * Remove notification 4 * /5 [[Nsnotificationcenter Defaultcenter]removeobserver: Self]; 6 [Super Dealloc]; 7 }
The implementation results are as follows. Click on the Red screen to turn red, click Blue, and the screen turns blue.
Little meow iOS Dev Growth Note: notification (Notification) QuickStart