Value Transfer Mode between IOS pages

Source: Internet
Author: User

1. Transmit data through Delegate

This section focuses onTo explain how to useDelegateTransfer data between different windows. For details, see the following details.

For example, in window 1, open window 2 and enter a number in window 2. The number is returned to window 1.

Window 1

Window 2

The result of window 2 is passed to window 1.

1. First define a delegate UIViewPassValueDelegate to pass the value

@protocol UIViewPassValueDelegate  - (void)passValue:(NSString *)value;  @end 

This protocol is used to pass the value

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;  @end 

And implement this delegate

- (void)passValue:(NSString *)value  {    self.value.text = value;      NSLog(@"the get value is %@", value);  } 

Click method of the button, open window 2, and point the delegate implementation method of window 2 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;  @end 

. 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 have a superview.      [super didReceiveMemoryWarning];            // Release any cached data, images, etc. that aren't in use.  }   - (void)viewDidUnload {      [super viewDidUnload];      // Release any retained subviews of the main view.      // e.g. self.myOutlet = nil;  }    @end 

 

2. data transmission between different interfaces using Singleton

First, write a singleton class that inherits NSObject

Check. h file

 @property(strong ,nonatomic) UITable * Table; @property(strong ,nonitomic) UITextFiled * Text; +(check*)shareDataModle;

Check. m

// Define a static checke Class Object and assign a null value

  static check * dataModle = nil;  +(check*)shareDataModle  {      if (dataModle == nil)      {          dataModle = [[check alloc]init];      }  }        

// Assign a value to the object of the singleton in the data source

-(void)checkDataSource{  [check shareDatamodle].Lable = @"15";  [check shareDatamodle].Text = @"22";}

// Introduce the header file of the singleton and assign values to the corresponding object in the corresponding method

// Pass the attribute value in the singleton to the receiving object on the current interface. At this point, data transmission and receiving are completed.

  -(void)viewWillAppear:(BOOL)animated  {      [super viewWillAppear:animated];      self.numberLable.text=[check shareDataModle].Lable;      self.danHao.text = [check shareDataModle].Text;  }

 

3. Use [[UIApplication sharedApplication] openURL:] to load other applications in iOS development

 

In iOS development, you often need to call other apps, such as making phone calls and sending emails. UIApplication: openURL: The method is the simplest way to achieve this goal. This method generally calls different apps through the url parameter mode provided.

The openURL method can call the following applications:

Call the Browser (Safari Browser)

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http:google.com"]]; 
Call Google Maps)
NSString *addressText = @"7 Hanover Square, New York, NY 10004";  addressText = [addressText stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];  NSString* urlText = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", addressText];  [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];  

Call the email client (Apple Mail)

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://devprograms@apple.com"]];  

Dialing (Phone Number)

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://6463777303"]];  

Call SMS)

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://466453"]];  

Call App Store)

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=291586600&amp;amp;mt=8"]];  
4. Use NSUerDefaults or file persistence data to transfer data between pagesIn IOS, you can use NSUserDefaults, sqlite, and CoreData to store data. NSUserDefaults is used to store data similar to user configurations, the latter two users store massive and complex data. NSUserDefault is easy to use:
NSUserDefaults *mySettingData = [NSUserDefaults standardUserDefaults];

You can add data to the NSUserDefaults object. The supported data types include NSString, NSNumber, NSDate, NSArray, NSDictionary, BOOL, NSInteger, and NSFloat, if you want to store custom objects (such as custom class objects), you must convert them to NSData storage:

NSArray *arr = [[NSArray alloc] initWithObjects:@"arr1", @"arr2", nil]  [mySettingData setObject:arr forKey:@"arrItem"];  [mySettingData setObject:@"admin" forKey:@"user_name"];  [mySettingData setBOOL:@YES forKey:@"auto_login"];  [mySettingData setInteger:1 forKey:@"count"];  
After adding data to NSUserDefaults, they become global variables, and the data in NSUserDefault can be read and written in the App:
NSUserDefaults *mySettingDataR = [NSUserDefaults standardUserDefaults];   NSLog(@"arrItem=%@", [mySettingDataR objectForKey:@"arrItem"]);  NSLog(@"user_name=%@", [mySettingDataR objectForKey:@"user_name"]);  NSLog(@"count=%d", [mySettingDataR integerForKey:@"count"]);  

To delete a data item, you can use removeObjectForKey to delete the data:

 [mySettingData removeObjectForKey:@"arrItem"];   
It should be noted that NSUserDefaults regularly writes data in the cache to the disk, rather than writing data instantly. To prevent data loss caused by program exit after NSUserDefaults is written, you can use synchronize to force data to be written to the disk immediately after data is written:
[mySettingData synchronize];  
After running the preceding statement, the data in NSUserDefaults is written. in the plist file, if the program is run on the simulator, you can find a plist file named YOUR-USERNAME under the/Users/YOUR-APP-DIR/Library/Application Support/iPhone Simulator/4.1/Applications/YOUR-Bundle_Identifier.plist/Library/Prefereces directory for Mac, open the file with Xcode and you can see the data you just written.

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.