Search code examples
iosuitextfielduipickerview

Default value in textfield using picker view


I am new to iOS i need to display objectAtIndex(0) in textfield using uipickerview .while run the app textfield empty after selection in picker view only value can displayed in textfield i need initially textfield not be empty.

viewdidload code:

    pktStatePicker  .delegate = self;

    pktStatePicker  .dataSource = self;

    [ pktStatePicker  setShowsSelectionIndicator:YES];

    txtText.inputView =  pktStatePicker  ;

    // Create done button in UIPickerView


    UIToolbar*  mypickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)];

    mypickerToolbar.barStyle = UIBarStyleBlackOpaque;

    [mypickerToolbar sizeToFit];


    NSMutableArray *barItems = [[NSMutableArray alloc] init];


    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];

    [barItems addObject:flexSpace];


    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClicked)];

    [barItems addObject:doneBtn];


    [mypickerToolbar setItems:barItems animated:YES];


    txtText.inputAccessoryView = mypickerToolbar;

picker view delegates:

- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{
     NSLog([arrmsg1 objectAtIndex:row]);
    txtText.text = (NSString *)[arrMsg objectAtIndex:row];

}

Solution

  • set like on your ViewDidLoad, but ensure once your arrMsg.count!=0

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    txtText.text = (NSString *)[arrMsg objectAtIndex:0];
    }
    

    choice-2

    or when you append the data in arrMsg after that call like

    if (arrMsg.count >0)
    {
    txtText.text = (NSString *)[arrMsg objectAtIndex:0];
    }
    

    there after continue your work asuseual