IOS: Reverse-pass data method one when switching views: notification

Source: Internet
Author: User

Notification Method:

1 A (single) notification Center, responsible for managing all notifications in iOS

2. Need to get some kind of notification, must register as observer (SUBSCRIBE)

3. If you no longer need to take some kind of notice, you need to cancel registration.

4. You can send a notification to the notification center that the notification center is forwarded to the appropriate observer (subscriber).

After the first controller and the second controller are joined in a modal manner, each controller is associated with its own class, and the Idetifier identity of the segue is set to a name, and when the data is being transmitted, it needs to be uniquely identified according to the Segue identifier. The method of notification is used when transmitting data in reverse.

1. All Documents:

2. The first controller Firstviewcontroller associated classes are:

3, the second controller Secondviewcontroller the associated class is:

4, to Segue identifier set a name, as the identity

The specific code is as follows:

The Viewcontroller (. h/.m) class associated with the Firstviewcontroller controller:

1 #import "ViewController.h"2 #import "SecondViewController.h"3 4 @interfaceViewcontroller ()5@property (Weak, nonatomic) Iboutlet Uitextfield *Firsttextfield;6 7 @end8 9 @implementationViewcontrollerTen  One- (void) Viewdidload { A [Super Viewdidload]; - } -  the //Override this method, when the view switches, automatically call --(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender - { -     if([Segue.identifier isequaltostring:@"modal"]) +     { -         //Get Destination Controller +Secondviewcontroller *SECONDVC =Segue.destinationviewcontroller; A          at         //forward-passing data -Secondvc.information =Self.firstTextField.text; -          -         //register notice to become an observer -[[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (receiveinfo:) name:notificationtepyObject: nil]; -     } in } -  to //Receiveinfo Events +-(void) Receiveinfo: (nsnotification*) Notification - { the     //reverse receiving data from a notification *Self.firstTextField.text =[Notification.userinfo Objectforkey:notificationinfokey]; $     Panax Notoginseng     //Cancel Registration -[[Nsnotificationcenter defaultcenter]removeobserver:self Name:notificationtepyObject: nil]; the } + @end

The Secondviewcontroller (. h/.m) class associated with the Secondviewcontroller controller:

1 #import "SecondViewController.h"2 3 @interfaceSecondviewcontroller ()4@property (Weak, nonatomic) Iboutlet Uitextfield *Secondtextfield;5 6 @end7 8 @implementationSecondviewcontroller9 //trigger event on returnTen-(Ibaction) returnclicked: (Uibarbuttonitem *) Sender One { A     //passing data in reverse -      -     //1. Message content theNsdictionary *dicinfo =@{notificationinfokey:self.secondtextfield.text}; -      -     //2, through the Notification center to pass out -[[Nsnotificationcenter Defaultcenter] Postnotificationname:notificationtepyObject: Self userinfo:dicinfo]; +      -     //Close modal window + [Self.presentingviewcontroller dismissviewcontrolleranimated:yes completion:nil]; A     //[self dismissviewcontrolleranimated:yes completion:nil]; at } -  -- (void) Viewdidload { - [Super Viewdidload]; -      -     //Display the text box contents (accept the data passed over) inSelf.secondTextField.text =self.information; - } to  + @end

IOS: Reverse-pass data method one when switching views: notification

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.