使用选择器视图的文本字段中的默认值

Default value in textfield using picker view

我是 iOS 的新手,我需要在 文本字段中显示 objectAtIndex(0) 使用 uipickerview 。而 运行 在选择器视图中选择后应用程序文本字段为空只有值可以显示在文本字段中我需要最初文本字段不为空.

viewdidload代码:

    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;

选择器视图委托:

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

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

}

像您的 ViewDidLoad 一样设置,但确保一旦您的 arrMsg.count!=0

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

选择-2

或者当你在 arrMsg 中附加数据时,像

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

之后继续你的工作