Block page value summary, block page value summary

Source: Internet
Author: User

Block page value summary, block page value summary

Take a module in my project as an example. First, there are two pages. The first page is the display city page, and the second page is the value (city name) to be uploaded using block ).

Display controls on the first page:

// Customize the left positioning View

Self. locView = [[LocationView alloc] initWithFrame: CGRectMake (0, 0, SCREENWIDTH/2-35, 25)];

Self. locView. imgView. image = [UIImage imageNamed: @ "around"];

UITapGestureRecognizer * tapGes = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (cityListView)];

[Self. locView addGestureRecognizer: tapGes];

UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView: _ locView];

Self. navigationItem. leftBarButtonItem = leftButton;

 

Select page on the second page:

First, declare the block code block in the. h file.

Typedef void (^ ChangeCityBlock) (NSString * cityStr );

@ Interface CityListViewController: UIViewController {

ChangeCityBlock _ sendBlock;

}

@ Property (nonatomic, copy) NSString * currentCity;

-(Void) setSendBlock :( ChangeCityBlock) block;

-(ChangeCityBlock) sendBlock;

@ End

You are ready to find a job here. The following is the key to transferring values.

When we jump from the first page to the second page, my code is as follows for your reference only:

// City list

-(Void) cityListView {

CityListViewController * clVC = [[CityListViewController alloc] init];

[ClVC setSendBlock: ^ (NSString * cityStr ){

_ LocView. cityLb. text = cityStr;

}];

ClVC. currentCity = _ locView. cityLb. text;

[Self. navigationController pushViewController: clVC animated: YES];

}

Next, we need to implement the defined block method on the second page.

-(Void) setSendBlock :( ChangeCityBlock) block {

If (_ sendBlock! = Block ){

_ SendBlock = [block copy];

}

}

-(ChangeCityBlock) sendBlock {

Return _ sendBlock;

}

Finally, when the returned value is determined on the second page and the first page is returned, a method is triggered to pass the returned value to the block code.

-(Void) blockReturnCity :( NSString *) city {

If (self. sendBlock ){

Self. sendBlock (city );

} Else {

NSLog (@ "no block passed in ");

}

[Self goback];

}

In this way, the value determined in the second page is uploaded to the first page.

 

Note: This conclusion only aims to make a memorandum for your iOS learning. If there is any confusion in the train of thought or step sequence, please forgive me! If you have any good methods, please do not hesitate to inform us.

Related Article

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.