Iphone 单个选择器中的选定项为空

Iphone 单个选择器中的选定项为空,iphone,ios,objective-c,Iphone,Ios,Objective C,我有一个选择器,当用户选择类型时,它将更新标签,但当我选择项时,它将等于null到标签 //update label type NSInteger row = [singlePicker selectedRowInComponent:0]; NSString *selected = [pickerData objectAtIndex:row]; NSString *strTypeSelected = [[NSString alloc] initWithFormat

我有一个选择器,当用户选择类型时,它将更新标签,但当我选择项时,它将等于null到标签

    //update label type
    NSInteger row  = [singlePicker selectedRowInComponent:0];
   NSString *selected = [pickerData objectAtIndex:row];
   NSString *strTypeSelected  = [[NSString alloc] initWithFormat:@"%@",selected];


    typelabel.text =strTypeSelected;

首先确保将outlet和datasource指定给
UIPickerView
的singlePicker对象,然后尝试此代码

typelabel.text = [pickerData  objectAtIndex:[singlePicker selectedRowInComponent:0]];
试试这个

txtField1.text = [NSString stringWithFormat:@"%@",[arrData objectAtIndex:[picker selectedRowInComponent:0]]];
检查
picker.delegate=self
in.m文件->
viewDidLoad
方法

签入.h文件

...Controller<UIPickerViewDataSource, UIPickerViewDelegate> {...
…控制器{。。。

它可能会对您有所帮助。
谢谢。

使用NSLog检查一次。您是否检查了pickerData?它可以包含指定行的空数据。如果选中,则为有效字符串(您应该使用日志检查该字符串)那么下一行,*strTypeSelected=…完全没有必要。您已经有了一个字符串,为什么还要创建另一个呢?@darkSlayer您检查是否将委托和数据源提供给UIPickerView对象,然后尝试上面的代码??