开发者

How can I store user input in a UIPickerView?

I currently have a UIPickerView in my app. There is a text box that the user will enter a string in, and I want that string to be saved in the UIPickerView so they can go back and tap on it instead of typing it again.

I have an array now that has some text in it, but I wan开发者_开发百科t to know how to add it once the program is running.


Suppose pickerArray is your array name whose data is shown in pickerView.

what you have to do is:-

 (void)textFieldDidEndEditing:(UITextField *)textField
{
[pickerArray addObject:textField.text];
[*yourPickerViewNmae* reloadAllCompononents];
}

I hope it will help you.Happy Coding:)


I have done it and checked. it works fine.

Following is the code.

#import "pickerViewController.h"

@implementation pickerViewController
@synthesize textfield,picker,pickerarray,btn;
- (void)dealloc
{
    [super dealloc];
    [picker release];
    [textfield release];
    [pickerarray release];
    [btn release];
}

- (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.
}

#pragma mark - View lifecycle


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    textfield.delegate=self;
    picker.delegate=self;
    picker.dataSource=self;
    picker.showsSelectionIndicator=YES;
    pickerarray=[[NSMutableArray alloc]init];

}


-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [pickerarray count];
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    textfield.delegate=self;
    [pickerarray addObject:textField.text];

    [picker reloadAllComponents];

    NSLog(@"%@",pickerarray);
}

-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{


    return [pickerarray objectAtIndex:row];

}
-(IBAction)killkeyBoard:(id)sender
{
    [textfield resignFirstResponder];
}


1.Use delegate function of UITextField, within the delegate function add the text in the desired arry(use NSMutable array).


UIPickerDatasource can take an array of items to be displayed, in your case your data is dynamic as you need to enter in text and then you want it to be entered in your picker control.

You need to have a NSMutableArray in which you can add and remove objects dynamically. Then after adding you can call reloadAllComponents method of your picker to reload data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜