Summary of IOS value-transfer methods

Source: Internet
Author: User
Tags notification center uikit

1. Attribute Passing value
The value is passed back to the front.
2. Protocol Transmission value
3, block transfer value
Instead of the protocol proxy value, the main point in time.
4, simple interest transmission value
Data sharing.
5. Notification value

Notification Center
The Nsnotificationcenter provides a more decoupled approach. The most typical application is any object pair that can send notifications to the center, while any object can listen to the notification of the center.
The code to send the notification is as follows:

[[Nsnotificationcenterdefaultcenter] postnotificationname:@ "Mynotificationname" object:broadcasterobject];
The code to register to receive notifications is as follows:

[[nsnotificationcenterdefaultcenter]     addobserver: Listenerobject     selector: @selector (receivingmethodonlistener:)       name:@ "Mynotificationname"     object:nil];
You can specify a specific broadcaster object when registering for notifications, but this is not required. You may have noticed the defaultcenter. This is actually the only center you'll use in your app. Notifications are open to the entire application, so there is only one center. The
also has a nsdistributednotificationcenter. This is used for inter-application communication. There is only one center of that type on the entire computer.
Pros: Both the sender and the recipient of the notification do not need to know each other. You can specify the specific method to receive notifications. The notification name can be any string.
Disadvantage: More code is required to observe the key value. You must remove listeners before deleting them. can't pass a lot of numbers, only let who do what.

1, use sharedapplication, define a variable to pass.

2, using files, or Nsuserdefault to pass

3, through a single case class to pass

4, through the delegate to pass.

iOS Development using Delegate delegate to pass data between different Windows is the content of this article, mainly to explain how to use the delegate delegate to pass data between different windows, the specific content to see the details. There are many ways to pass parameters between two uiview windows in iOS development , such as

The first 3 methods, for the time being, learn how to use delegate methods to pass data in different UIView.

For example: In Window 1, open Window 2, and then fill in Window 2 A number, the number is passed back to window 1.

Windows 1

Windows 2

Window 2 results are passed to window 1

1, first define a delegate uiviewpassvaluedelegate used to pass the value

@protocol uiviewpassvaluedelegate  -(void) Passvalue: (NSString *) value;  

This protocol is used to pass values.

2. In the header file of window 1, declare delegate

#import <UIKit/UIKit.h> #import "UIViewPassValueDelegate.h"  @interface Delegatesampleviewcontroller: Uiviewcontroller <UIViewPassValueDelegate> {      Uitextfield *_value;  }  @property (nonatomic, retain) iboutlet Uitextfield *value;  -(Ibaction) ButtonClick: (ID) sender;  

and implement this delegate

-(void) Passvalue: (NSString *) value  {    self.value.text = value;      NSLog (@ "The Get value is%@", value);  

Button's click Method, open Window 2, and point to Window 2 of the delegate implementation method to window 1.

-(Ibaction) ButtonClick: (ID) sender  {      Valueinputview *valueview = [[Valueinputview alloc] initwithnibname:@ " Valueinputview "Bundle:[nsbundle Mainbundle]";      Valueview.delegate = self;      [Self setmodaltransitionstyle:uimodaltransitionstylecoververtical];      [Self Presentmodalviewcontroller:valueview animated:yes];  

Implementation of the second window

. h header File

#import <UIKit/UIKit.h> #import "UIViewPassValueDelegate.h"   @interface Valueinputview:uiviewcontroller {       nsobject<uiviewpassvaluedelegate> * delegate;      Uitextfield *_value;  }  @property (nonatomic, retain) iboutlet Uitextfield *value;  @property (nonatomic, retain) nsobject<uiviewpassvaluedelegate> * delegate;  -(Ibaction) ButtonClick: (ID) sender;  

. m implementation file

#import "ValueInputView.h"  @implementation valueinputview  @synthesize delegate;  @synthesize value = _value;  -(void) dealloc {      [self.value release];      [Super Dealloc];  }   -(Ibaction) ButtonClick: (ID) sender  {      [delegate passValue:self.value.text];      NSLog (@ "Self.value.text is%@", self.value.text);      [Self dismissmodalviewcontrolleranimated:yes];           }  -(void) didreceivememorywarning {      //Releases the view if it doesn ' t has a superview.      [Super didreceivememorywarning];            Release any cached data, images, etc. that aren ' t on use.  }   -(void) viewdidunload {      [super viewdidunload];      Release any retained subviews of the main view.      e.g. Self.myoutlet = nil;  }    

Summary of IOS value-passing methods

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.