Pass value for iOS Block

Source: Internet
Author: User

The block is similar to the function pointer in C. It is very convenient to transmit data in the Controller. Let's continue with the example in the previous chapter and transfer the data from Second to First. Here we use blocks to complete the process. It seems like the protocol, however, it is simpler than the Protocol. The Code is as follows: // FirstViewController-(void) viewDidLoad {[super viewDidLoad]; self. nameLable = [[[UILabel alloc] initWithFrame: CGRectMake (10, 10,300, 60)] autorelease]; self. nameLable. textAlignment = UITextAlignmentCenter; self. nameLable. font = [UIFont systemFontOfSize: 50]; self. nameLable. textColor = [UIColor blueColor]; [self. view addSubview: self. nameLable]; UIButton * button = [UIButton buttonWithType: UIButtonTypeRoundedRect]; button. frame = CGRectMake (130,170, 60, 40); [button setTitle: @ "Next" forState: UIControlStateNormal]; [button addTarget: self action: @ selector (pushNext :) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: button];}-(void) pushNext :( id) sender {// initialize second SecondViewController * second = [[SecondViewController alloc] init]; // call the second block. send = ^ (NSString * str) {self. nameLable. text = str ;}; // pushed to [self. navigationController pushViewController: second animated: YES]; [second release];} objective-C 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 19 20 21 22 23 24 26 27 28 29 30 31 ////// //// // SecondViewController. h # import <UIKit/UIKit. h> typedef void (^ SendMessage) (NSString * str); // declaration block @ interface SecondViewController: UIViewController <UITextFieldDelegate> @ property (nonatomic, copy) SendMessage send; // declare a block type attribute @ end // SecondViewController. m-(void) viewDidLoad {[super viewDidLoad]; UITextField * textFd = [[UITextField alloc] initWithFrame: CGRectMake (10, 10,300,150)]; textFd. borderStyle = UITextBorderStyleRoundedRect; textFd. delegate = self; textFd. tag = 100; [self. view addSubview: textFd]; [textFd release];}-(BOOL) textFieldShouldReturn :( UITextField *) textField {[textField resignFirstResponder]; // first, pass the real parameter if (self. send) {self. send (textField. text);} return YES ;}

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.