Transfer values between IOS view

Source: Internet
Author: User

1, using Nsuserdefaults to transmit the value, this method is limited to the case of transmitting a small amount of data:

For example, if you want to pass a float value, use it when you need to pass it.
[[Nsuserdefaults Standarduserdefaults] setfloat:float forkey::@ "float"]
When you receive the value, use the
[[Nsuserdefaults Standarduserdefaults] floatforkey:@ "float"]

2, Nsnotificationcenter to pass the value

-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event
{
Uitouch * Touch = [touches anyobject];
Cgpoint point = [Touch locationinview:self];
CGRect roundrect = [self rectneedrefreshfromthispoint:point];
Mlastpoint = Cgpointmake (-1,-1);

NSLog (@ "%s:point (%f,%f)", __function__, Point.x, POINT.Y);

[Self addtocurrentlinewithpoint:point.x y:point.y];
[Self endLine];
[Self setneedsdisplayinrect:roundrect];

NSNumber *pointx = [NSNumber numberwithfloat:point.x];
NSNumber *pointy = [NSNumber numberwithfloat:point.y];
Nsdictionary *pointdict = [nsdictionary dictionarywithobjectsandkeys:pointx,@ "Pointx", pointY,@ "PointY", nil];
[[Nsnotificationcenter defaultcenter]postnotificationname:@ "Passtouchedendpointmsg"object:self userinfo:pointdict];

}

Functions in the Message Center:

[[Nsnotificationcenter Defaultcenter] Addobserver:self
Selector: @selector (gettouchedpoint:)
name:@ "Passtouchedendpointmsg"
Object:nil];

-(void) Gettouchedpoint:(nsnotification *) Noti
{
Nsdictionary *pointdict = [Noti userInfo];
Touchedendpointx = [[Pointdict objectforkey:@ "Pointx"] floatvalue];
Touchedendpointy = [[Pointdict objectforkey:@ "pointy"] floatvalue];
NSLog (@ "%f:%f", Touchedendpointx,touchedendpointy);
}

Using the message to pass parameters has the following points: Object refers to the sender, in the poseter side of the userinfo can be stored in the parameters to be passed, must be nsdictionary type. Get this dictionary type on the center side by: [Notification UserInfo];

Transfer values between IOS view

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.