Transfer Data between ios page jumps ---- use delegate

Source: Internet
Author: User

Transfer Data between ios page jumps ---- use delegate

 

Mainly used, two classes, one delegate

Class a: Class B is called. After Class B is executed, a data must be passed to Class a, and Class a displays the data.

1. delegate, this header file is enough. Implement this proxy method in the class

# Import

@ ProtocolUIViewPassValueDelegate

-(Void) passValue :( NSString *) value;

Once a class implements this callback function, this class will obtain the current value data. Therefore, the class that receives data must implement a callback function. The current project is passValue.

@ End

2. The first page

. H file

# Import

# Import UIViewPassValueDelegate. h

# Import ValueInputView. h

@ InterfaceDelegateSampleViewController: UIViewController

{

UITextField * _ value;

}

@ Property (strong, nonatomic) IBOutletUITextField * value;

-(IBAction) buttonClick :( id) sender;

@ End

. M file

# Import DelegateSampleViewController. h

@ ImplementationDelegateSampleViewController

@ Synthesizevalue = _ value;

-(Id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) nibBundleOrNil

{

Self = [superinitWithNibName: nibNameOrNil bundle: nibBundleOrNil];

If (self ){

}

Returnself;

}

-(Void) viewDidLoad

{

[SuperviewDidLoad];

}

-(Void) viewDidUnload

{

[SelfsetValue: nil];

[SuperviewDidUnload];

}

-(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) interfaceOrientation

{

Return (interfaceOrientation = UIInterfaceOrientationPortrait );

}

 

-(IBAction) buttonClick :( id) sender

{

ValueInputView * valueView = [[ValueInputViewalloc] init];

ValueView. delegate = self;

[SelfsetModalTransitionStyle: UIModalTransitionStyleCoverVertical];

[SelfpresentModalViewController: valueView animated: YES];

}

-(Void) passValue :( NSString *) value

{

Self. value. text = value;

NSLog (@ the get value is % @, value );

}

@ End

3. Second Class:

. H file

# Import

# Import UIViewPassValueDelegate. h

@ InterfaceValueInputView: UIViewController

{

NSObject * Delegate;

UITextField * _ value;

}

@ Property (retain, nonatomic) IBOutletUITextField * value;

@ Property (nonatomic, retain) NSObject * Delegate;

-(IBAction) buttonClick :( id) sender;

@ End

. M file

# Import ValueInputView. h

 

@ ImplementationValueInputView

@ Synthesizevalue = _ value;

@ Synthesizedelegate;

 

-(Id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) nibBundleOrNil

{

Self = [superinitWithNibName: nibNameOrNil bundle: nibBundleOrNil];

If (self ){

// Custom initialization

}

Returnself;

}

-(Void) viewDidLoad

{

[SuperviewDidLoad];

}

-(Void) viewDidUnload

{

[SelfsetValue: nil];

[SuperviewDidUnload];

}

-(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) interfaceOrientation

{

Return (interfaceOrientation = UIInterfaceOrientationPortrait );

}

-(IBAction) buttonClick :( id) sender {

[DelegatepassValue: self. value. text];

NSLog (@ self. value. text is % @, self. value. text );

[SelfdismissModalViewControllerAnimated: YES];

}

@ 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.