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.