Four ways of passing parameters between iOS pages
1, use sharedapplication, define a variable to pass.
2, using files, or Nsuserdefault to pass
3, through a single case class to pass
4, through the delegate to pass.
iOS Development using Delegate delegate to pass data between different Windows is the content of this article, mainly to explain how to use the delegate delegate to pass data between different windows, the specific content to see the details. There are many ways to pass parameters between two uiview windows in iOS development , such as
The first 3 methods, for the time being, learn how to use delegate methods to pass data in different UIView.
For example: In Window 1, open Window 2, and then fill in Window 2 A number, the number is passed back to window 1.
Windows 1
Windows 2
Window 2 results are passed to window 1
1, first define a delegate uiviewpassvaluedelegate used to pass the value
@protocol uiviewpassvaluedelegate -(void) Passvalue: (NSString *) value;
This protocol is used to pass values.
2. In the header file of window 1, declare delegate
#import <UIKit/UIKit.h> #import "UIViewPassValueDelegate.h" @interface Delegatesampleviewcontroller: Uiviewcontroller <UIViewPassValueDelegate> { Uitextfield *_value; } @property (nonatomic, retain) iboutlet Uitextfield *value; -(Ibaction) ButtonClick: (ID) sender;
and implement this delegate
-(void) Passvalue: (NSString *) value { self.value.text = value; NSLog (@ "The Get value is%@", value);
Button's click Method, open Window 2, and point to Window 2 of the delegate implementation method to window 1.
-(Ibaction) ButtonClick: (ID) sender { Valueinputview *valueview = [[Valueinputview alloc] initwithnibname:@ " Valueinputview "Bundle:[nsbundle Mainbundle]"; Valueview.delegate = self; [Self setmodaltransitionstyle:uimodaltransitionstylecoververtical]; [Self Presentmodalviewcontroller:valueview animated:yes];
Implementation of the second window
. h header File
#import <UIKit/UIKit.h> #import "UIViewPassValueDelegate.h" @interface Valueinputview:uiviewcontroller { nsobject<uiviewpassvaluedelegate> * delegate; Uitextfield *_value; } @property (nonatomic, retain) iboutlet Uitextfield *value; @property (nonatomic, retain) nsobject<uiviewpassvaluedelegate> * delegate; -(Ibaction) ButtonClick: (ID) sender;
. m implementation file
#import "ValueInputView.h" @implementation valueinputview @synthesize delegate; @synthesize value = _value; -(void) dealloc { [self.value release]; [Super Dealloc]; } -(Ibaction) ButtonClick: (ID) sender { [delegate passValue:self.value.text]; NSLog (@ "Self.value.text is%@", self.value.text); [Self dismissmodalviewcontrolleranimated:yes]; } -(void) didreceivememorywarning { //Releases the view if it doesn ' t has a superview. [Super didreceivememorywarning]; Release any cached data, images, etc. that aren ' t on use. } -(void) viewdidunload { [super viewdidunload]; Release any retained subviews of the main view. e.g. Self.myoutlet = nil; }
Four ways of passing parameters between iOS pages