Notification Pass Value

Source: Internet
Author: User
Tags notification center

Contact Notification Center for a long time, has not used it to pass value, today the task completed earlier, so did a small demo, after the interface Notification Center forward interface value

Sender View Controller:

-(void) Viewdidload {

[Super Viewdidload];

}

-(Ibaction) Call: (ID) Sender {

When the text in the input box is not empty

if (_atextfield.text) {

With the value of the input box as value, a as a key, stored as a dictionary

Nsdictionary *dics = @{@ "A": _atextfield.text};

Create a notification object with name two unique identifier for the controller notification

Nsnotification *notifications = [[Nsnotification alloc] initwithname:@ "abc" Object:nil Userinfo:dics];

[[Nsnotificationcenter defaultcenter]postnotification:notifications];

[Self dismissviewcontrolleranimated:yes completion:nil];

}

}

Recipient View Controller:

-(void) Viewdidload {

[Super Viewdidload];

Recipient Registration Notification Center, the name field is the same as the sender

[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (ABC:) name:@ "abc" Object:nil];

}

-(void) ABC: (Nsnotification *) Notif {

_abt.text = notif.userinfo[@ "A"];

NSLog (@ "%@", _abt.text);

}

Note: Dalloc is written in the controller that creates the viewer

-(void) dealloc

{

After the message is sent, remove

[[Nsnotificationcenter defaultcenter]removeobserver:self name:@ "abc" Object:nil];

}

Summary: To sum up, the notification Center is very suitable for use when the two view controllers are associated with little, lightweight values, comparable to attribute values

Notification Pass Value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.