Four ways of passing parameters between iOS pages

Source: Internet
Author: User
Tags uikit

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

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.