Search This Blog

Oct 7, 2010

How to send data from one controller to second controller in iPhone application.

This example will shows that how to send data from one controller to second controller in iPhone application.

For sending data from one controller to another these steps will be used.

1. Put this code in first controller .h file.

@interface UIFirstClass : UIViewController {

    IBOutlet UIButton *btnNext;
    NSArray *arrOnFirstClass;
}

-(IBAction)btnNext_TouchUpInside:(id)sender;
@end

2. Include second controller header file in first controller .m file and put this code in first controller .m file.

#import "UIFirstClass.h"
#import "UISecondClass.h"

@implementation UIFirstClass

- (void)viewDidLoad {
    [super viewDidLoad];
    arrOnFirstClass = [[NSArray alloc]initWithObjects:@"One", @"Two", @"Three", @"Four", nil];
   
}

-(IBAction)btnNext_TouchUpInside:(id)sender{
    UISecondClass *secondClass = [[UISecondClass alloc]initUserInfo:arrOnFirstClass];
    [self.navigationController pushViewController:secondClass animated:YES];
}

@end

3. Put this code in second controller .h file.

#import

@interface UISecondClass : UIViewController {

    IBOutlet UIButton *btnPrevious;
}
-(IBAction)btnPrevious_TouchUpInside:(id)sender;
- (id)initUserInfo:(NSArray *)arrTemp ;
@end


4. Put this code in second controller .m file.

@implementation UISecondClass
NSArray *arrUserInfo;

- (id)initUserInfo:(NSArray *)arrTemp {
    if (self = [super initWithNibName:@"SecondView" bundle:nil]) {
        arrUserInfo = arrTemp;
    }
    return self;
}

-(IBAction)btnPrevious_TouchUpInside:(id)sender{

    [self.navigationController popViewControllerAnimated:YES];
}

@end

No comments:

Post a Comment