Pass the Value Block in the iOS attribute to pass the value between two viewcontrollers

Source: Internet
Author: User

Pass the Value Block in the iOS attribute to pass the value between two viewcontrollers

The attribute value is to pass the data on page A to page B. The following is to pass the TextField content of FirstViewController to the navigation bar title and console output on the SecondViewController page.

# Import


@ Interface FirstViewController: UIViewController

{

UITextField * tf;

}

@ End

# Import "FirstViewController. h"

# Import "SecondViewController. h" // reference the header file on the page to which the data is uploaded (ViewController ).

-(Void) viewDidLoad

{

[SuperviewDidLoad];

// Define a button

UIButton * button = [UIButtonbuttonWithType: UIButtonTypeRoundedRect];

Button. frame = CGRectMake (100,100,100,100 );

Button. backgroundColor = [UIColorredColor];

[Button addTarget: selfaction: @ selector (doButton) forControlEvents: UIControlEventTouchUpInside];

[Self. viewaddSubview: button];

Tf = [[UITextFieldalloc] initWithFrame: CGRectMake (10,300,100, 40)];

Tf. tintColor = [UIColororangeColor];

Tf. backgroundColor = [UIColorgrayColor];

Tf. Label = 1000;

[Self. viewaddSubview: tf];

}

-(Void) doButton {

Tf = (UITextField *) [self. viewviewWithTag: 1000];

// Push the incoming stack reference count + 1, and control belongs to the System

SecondViewController * seV = [[SecondViewControlleralloc] init]; // instantiate the parameter; otherwise, the corresponding attribute cannot be found.

// Directly pass the attribute value

SeV. naviTitle = tf. text;

SeV. str = @ "successful value transfer"; // attribute (Value assignment) the value to be passed must be written before the next window.

[Self. navigationControllerpushViewController: seVanimated: YES];

}


@ End



# Import


@ Interface SecondViewController: UIViewController

@ Property (nonatomic, strong) NSString * str;

@ Property (nonatomic, retain) NSString * naviTitle;

@ End



# Import "SecondViewController. h"


@ Interface SecondViewController ()


@ End


@ Implementation SecondViewController

@ Synthesize naviTitle = _ naviTitle;


-(Void) viewDidLoad

{

[SuperviewDidLoad];

// Do any additional setup after loading the view.

Self. view = [[UIViewalloc] initWithFrame: CGRectMake (320,480,)];

Self. title = self. naviTitle; // display the content uploaded on the first page to the navigation title.

}


-(Void) viewWillAppear :( BOOL) animated {

NSLog (@ "% @", self. str );

NSLog (@ "11 --- % @", self. naviTitle );

}

@ End


Block Value passing

# Import


@ Interface FirstViewController: UIViewController

{

UITextField * tf;

}

@ Property (nonatomic, retain) UILabel * label;

@ End

# Import "FirstViewController. h"

# Import "SecondViewController. h"

@ Implementation FirstViewController

-(Void) viewDidLoad

{

[SuperviewDidLoad];

// Define a button

UIButton * button = [UIButtonbuttonWithType: UIButtonTypeRoundedRect];

Button. frame = CGRectMake (100,100,100,100 );

Button. backgroundColor = [UIColorredColor];

[Button addTarget: selfaction: @ selector (doButton) forControlEvents: UIControlEventTouchUpInside];

[Self. viewaddSubview: button];

// Define a display control

Self. label = [[UILabelalloc] initWithFrame: CGRectMake (0,400,100, 40)];

Self. label. backgroundColor = [UIColorgreenColor];

Self. label. text = nil; // to display the string passed by the second view Controller

[Self. viewaddSubview: self. label];


}

-(Void) doButton {

Tf = (UITextField *) [self. viewviewWithTag: 1000];

// Push the incoming stack reference count + 1, and control belongs to the System

SecondViewController * seV = [[SecondViewControlleralloc] init]; // instantiate it accordingly; otherwise, the corresponding attribute cannot be found.

// The callback method transmits the data in the input box.

[SeVreturnText: ^ (NSString * showText ){

Self. label. text = showText;

}];

[Self. navigationControllerpushViewController: seVanimated: YES];

}


@ End

# Import

Typedefvoid (^ ReturnTextBlock) (NSString * showText); // redefine a name

@ Interface SecondViewController: UIViewController

@ Property (nonatomic, retain) UITextField * tf;

@ Property (nonatomic, copy) ReturnTextBlock returnTextBlock; // defines a Block attribute.

-(Void) returnText :( ReturnTextBlock) block;

@ End

# Import "SecondViewController. h"


-(Void) viewDidLoad

{

[SuperviewDidLoad];

// Define an input box to pass the text to the first interface and display it on UILabel

Self. tf = [[UITextFieldalloc] initWithFrame: CGRectMake (10,300,100, 40)];

Self. tf. tintColor = [UIColororangeColor];

Self. tf. backgroundColor = [UIColorgrayColor];

[Self. viewaddSubview: self. tf];


}


// A Block statement Block function is included in the first interface.

// Save the passed Block statement Block to the instance variable returnTextBlock (the attribute defined in. h) of this class, and find a time to call

-(Void) returnText :( ReturnTextBlock) block {

Self. returnTextBlock = block;

}

// When the view is about to disappear, you need to rewrite it:

-(Void) viewWillDisappear :( BOOL) animated {

If (self. returnTextBlock! = Nil ){

Self. returnTextBlock (self. tf. text );

NSLog (@ "self. tf. text % @", self. tf. text );

}

}

@ End





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.