使用数组数据时,必须在下一个视图中显示选定的选取器视图值

时间:2013-06-21 10:48:28

标签: ios uipickerview

我有两种观点 在第一个视图中,我有3个选择器视图,我将选择选择器视图值 我必须在下一个视图中显示这些值(第二个视图) 我必须在第二个视图中显示以前选择的数据。

注意 - 我正在为pickerview数据使用数组。

第一个视图(inputViewController.h)

#import <UIKit/UIKit.h>
#import "dataViewController.h" @class dataViewController;


@interface inputViewController : UIViewController<UIScrollViewDelegate,UITextFieldDelegate>{
    NSString *name;
    IBOutlet UIPickerView *agePicker;
    IBOutlet UIPickerView *genderPicker;
    IBOutlet UIPickerView *locationPicker;
    IBOutlet UIScrollView *scroll;
    NSMutableArray *ageArray;
    NSMutableArray *genderArray;
    NSMutableArray *locationArray; }
-(IBAction)saveData:(id)sender; @property(strong,nonatomic) dataViewController *selectData; @property(strong,nonatomic) NSString
*name;

@property(strong, nonatomic) IBOutlet UITextView *nameField; //@property(strong, nonatomic) IBOutlet UIPickerView *agePicker; //@property(strong, nonatomic) IBOutlet UIPickerView *sexpicker;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; @end

第一个视图(inputViewController.m)

#import "inputViewController.h"
#import "dataViewController.h"

@interface inputViewController ()

@end

@implementation inputViewController @synthesize nameField; @synthesize selectData; @synthesize name; //@synthesize agePicker; //@synthesize sexpicker;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self; }

- (void)viewDidLoad {
    self.title=@"inputView";
    scroll.contentSize=CGSizeMake(300, 900);
    [scroll addSubview:self.view];    agePicker.transform= CGAffineTransformMakeScale(.5, 0.5);    genderPicker.transform= CGAffineTransformMakeScale(.5, 0.5);
    locationPicker.transform= CGAffineTransformMakeScale(.5, 0.5); 
    [self.view addSubview:locationPicker];    ageArray=[[NSMutableArray alloc]initWithObjects:@"0-6m",@"7-12m",@"1-5y",@"6-10y",@"10-20y",@"21-30y",@"31-40y",@"41-50y",@"51-60y",@"60y above", nil];

    genderArray=[[NSMutableArray alloc]initWithObjects:@"male",@"female", nil];
    locationArray=[[NSMutableArray alloc]initWithObjects:@"america",@"australia", nil];


    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib. }
 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
     if ([pickerView isEqual:agePicker]) {
         return 1;
     } else if ([pickerView isEqual:genderPicker]) {
    return 1; } else {
    return 1; }
     }

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    //return [ageArray count];
    if ([pickerView isEqual:agePicker]) {
        return [ageArray count];
    }
    else if ([pickerView isEqual:genderPicker]) {
        return [genderArray count];
    }
    else {
        return [locationArray count];
    }
     }
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    return 100; }
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 50; }

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
        // return [ageArray objectAtIndex:row];
    if ([pickerView isEqual:agePicker]) {
        return [ageArray objectAtIndex:row];
    }
    else if ([pickerView isEqual:genderPicker]) {
        return [genderArray objectAtIndex:row];
    }

    else {
        return [locationArray objectAtIndex:row];
    }

} 
-(IBAction)saveData:(id)sender{
    selectData=[[dataViewController alloc]initWithNibName:@"dataViewController" bundle:[NSBundle mainBundle]];   //  [selectData setName:self.name];
    //selectData.name = nameField.text;
    [self.navigationController pushViewController:selectData animated:YES]; }
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    self.name=nameField.text;
    return  YES; }

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    // [nameField resignFirstResponder];
    [self.view endEditing:YES];

}



- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil; }

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait); }

@end

第二个视图(dataViewController.h)

#import <UIKit/UIKit.h>

@interface dataViewController : UIViewController{
    NSString *name;
     } @property(strong,nonatomic) IBOutlet UILabel *nameLabel; @property(strong,nonatomic) IBOutlet UILabel *ageLabel; @property(strong,nonatomic) IBOutlet UILabel *genderLabel; @property(strong,nonatomic) IBOutlet UILabel *locationLabel; @property(nonatomic, retain) NSString *name;


     @end

第二个视图(dataViewController.m)

#import "dataViewController.h"

@interface dataViewController ()

@end

@implementation dataViewController @synthesize nameLabel,ageLabel,genderLabel,locationLabel,name;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self; }

- (void)viewDidLoad {
    [nameLabel setText:self.name];
    //[self.nameLabel setText:self.name];



    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib. }
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated]; }


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil; }

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait); }

@end

1 个答案:

答案 0 :(得分:0)

试试这个,

-(IBAction)saveData:(id)sender
{
 selectData=[[dataViewController alloc]initWithNibName:@"dataViewController" bundle:[NSBundle mainBundle]];   //  [selectData setName:self.name];
selectData.strName = nameField.text;
selectData.strAge=[ageArray objectAtIndex:[agePicker selectedRowInComponent:0]];
selectData.strGender=[genderArray objectAtIndex:[genderPicker selectedRowInComponent:0]];
selectData.strLocation=[locationArray objectAtIndex:[locationPicker selectedRowInComponent:0]];
[self.navigationController pushViewController:selectData animated:YES];

}

第二个视图(dataViewController.h)

 #import <UIKit/UIKit.h>

@interface dataViewController : UIViewController
{
NSString *strName;
NSString *strAge;
NSString *strGender;
NSString *strLocation;
IBOutlet UILabel *nameLabel;
IBOutlet UILabel *ageLabel;
IBOutlet UILabel *genderLabel;
IBOutlet UILabel *locationLabel;
} 
@property(nonatomic, retain) NSString *strName;
@property(nonatomic, retain) NSString *strAge;
@property(nonatomic, retain) NSString *strGender;
@property(nonatomic, retain) NSString *strLocation;
@end

第二个视图(dataViewController.m)

#import "dataViewController.h"

@interface dataViewController ()

@end

@implementation dataViewController
@synthesize strName,strAge,strLocation,strGender;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self; }

- (void)viewDidLoad {
  [nameLabel setText:strName];
  [ageLabel setText:strAge];
  [genderLabel setText:strGender];
  [locationLabel setText:strLocation];

[super viewDidLoad];
// Do any additional setup after loading the view from its nib. }
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated]; }


- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil; }

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait); }

@end