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