Tuesday, December 13, 2011

Passing the data between the viewControllers

Source: http://www.devx.com/wireless/Article/42476/1954
For the detail, please visit above website.


Sometimes you need to pass data from one view to another. The easiest way is to create a property on the target view and set (or retrieve) that property from the calling view.
Here's an example. Double-click the VCExampleViewController.xib file and add a DatePicker view to the View window (see Figure 18).
Figure 18. Enhancing the View: Add a DatePicker view to the first View.


In the VCExampleViewController.h file, create an outlet for this DatePicker view and then expose it as a property:

#import <UIKit/UIKit.h>
@interface VCExampleViewController : UIViewController {
    //---outlet for the DatePicker view---
    IBOutlet UIDatePicker *datePicker;
}
//---expose this outlet as a property---
@property (nonatomic, retain) UIDatePicker *datePicker;

-(IBAction) displayView:(id) sender;

@end

In the VCExampleViewController.xib window, control-click and drag the File's Owner Item to the DatePicker view and then select datePicker.
In the SecondViewController.h, create an object of type UIDatePicker and then expose it as a property:

#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController {
    //---object of type UIDatePicker---
    UIDatePicker *selectedDatePicker;
}
//---expose the object as a property---
@property (nonatomic, retain) UIDatePicker *selectedDatePicker;

-(IBAction) btnReturn:(id) sender;

@end

In the SecondViewController.m file, add the following lines (in bold) to the viewDidLoad method:

#import "SecondViewController.h"
@implementation SecondViewController
@synthesize selectedDatePicker;
- (void)viewDidLoad {
    //---display the date and time selected in the previous view---
    NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
    [formatter setDateFormat:@"MMM dd, yyyy HH:mm"];
    
    UIAlertView *alert = [[UIAlertView alloc] 
                             initWithTitle:@"Date and time selected" 
                             message:[formatter stringFromDate:selectedDatePicker.date] 
                             delegate:self 
                             cancelButtonTitle:@"OK" 
                             otherButtonTitles:nil];
    [alert show];
    [alert release];    
    [super viewDidLoad];
}
- (void)dealloc {
    //---release the memory used by the property---
    [selectedDatePicker release];
    [super dealloc];
}

Finally, in the VCExampleViewController.m file, add the line shown in bold:

#import "VCExampleViewController.h"
#import "SecondViewController.h"
 
@implementation VCExampleViewController
SecondViewController *secondViewController;
@synthesize datePicker;
-(IBAction) displayView:(id) sender{
secondViewController = [[SecondViewController alloc] 
                                initWithNibName:@"SecondView" 
                                bundle:nil];
    //---set the property of the second view with the DatePicker view in the current view---
    secondViewController.selectedDatePicker = datePicker;

Figure 19. Passing Values Between Views: The second view now displays the date and time selected from the DatePicker in the first view.

     [UIView beginAnimations:@"flipping view" context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:
          UIViewAnimationTransitionCurlDown 
          forView:self.view cache:YES];
    
    [self.view addSubview:secondViewController.view];
    [UIView commitAnimations];    
}

Press Command-r to test the application. Select a date and time in the DatePicker view and press the Display SecondView button. The second view will now display the date and time selected in the first view (see Figure 19).

You've seen how to pass values between two views. As these operations form the basics of iPhone programming, you should be well on your way to creating your own iPhone applications.

No comments:

Post a Comment