我的故事板上有2个选择器视图,所有内容都是单独完成的。但是出于某种原因,我的第二个观众正在复制第一个观众。
#import <UIKit/UIKit.h>
@interface MaisOuiViewController : UIViewController <UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate>
@property (strong, nonatomic) IBOutlet UIPickerView *from;
@property (strong, nonatomic) IBOutlet UIPickerView *to;
@property (strong, nonatomic) NSArray * fromlang;
@property (strong,nonatomic) NSArray * tolang;
@end
@synthesize from;
@synthesize to;
@synthesize fromlang = _fromlang;
@synthesize tolang= _tolang;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.inWord.delegate = self;
//Load NSArray fromlang
_fromlang= [[NSArray alloc] initWithObjects:@"English",@"Spanish",@"German", nil];
//Load NSArray tolang
_tolang= [[NSArray alloc] initWithObjects:@"Hindi",@"Chinese",@"Check", nil];
}
#pragma mark - UIPickerView Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)numberOfComponentsInPickerView1:(UIPickerView *)pickerView1
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _fromlang.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [_fromlang objectAtIndex:row];
}
- (NSInteger)pickerView1:(UIPickerView *)pickerView1 numberOfRowsInComponent1:(NSInteger)component1
{
return _tolang.count;
}
- (NSString *)pickerView1:(UIPickerView *)pickerView1 titleForRow:(NSInteger)row forComponent1:(NSInteger)component1
{
return [_tolang objectAtIndex:row];
}
@end
有人可以指出我哪里出错吗?
由于
答案 0 :(得分:1)
UIPickerViews
from
和to
看起来相同,因为委托和数据源方法相同。 iOS
将根据苹果iOS文档引用
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _fromlang.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [_fromlang objectAtIndex:row];
}
您可以为to
和from
设置标记,并使用下面提到的代码
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.inWord.delegate = self;
//Set tags to differentiate
from.tag=1;
to.tag=2;
//Load NSArray fromlang
_fromlang= [[NSArray alloc] initWithObjects:@"English",@"Spanish",@"German", nil];
//Load NSArray tolang
_tolang= [[NSArray alloc] initWithObjects:@"Hindi",@"Chinese",@"Check", nil];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
if(pickerView.tag==1)
{
return 1;
}
else
{
return 1;
}
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(pickerView.tag==1)
{
return _fromlang.count;
}
else
{
return _tolang.count;
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(pickerView.tag==1)
{
return [_fromlang objectAtIndex:row];
}
else
{
return [_tolang objectAtIndex:row];
}
}
希望这会有所帮助。
答案 1 :(得分:0)
如果您有两个按钮来显示然后取一个整数变量并设置一些值或取BOOL值来产生差异或设置选择器的标签然后根据您的条件在选择器视图中调用您的数组。
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *titleString;
if (currentSelectedDropDown == 1) {
AIFillAccountTypeParserModel *modal = [[AIFillAccountTypeParserModel alloc]init];
if (isSearching) {
modal = [filteredArray objectAtIndex:row];
}else{
modal = [accountTypesPickerArray objectAtIndex:row];
}
titleString = modal.accountType;
accountTypeTxtField.text = titleString;
}
else if(currentSelectedDropDown == 2) {
AIFillRatingParserModel *modal = [[AIFillRatingParserModel alloc]init];
if (isSearching) {
modal = [filteredArray objectAtIndex:row];
}else{
modal = [accountTypesPickerArray objectAtIndex:row];
}
titleString = modal.rating_Description;
ratingTxtField.text = titleString;
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (currentSelectedDropDown == 1) {
AIFillAccountTypeParserModel *modal = nil;
if (isSearching) {
modal = [filteredArray objectAtIndex:row];
}
else{
modal = [accountTypesPickerArray objectAtIndex:row];
}
NSString *titleString = modal.accountType;
accountTypeTxtField.text = titleString;
accountTypeId = modal.accountId;
NSUserDefaults *accountType = [NSUserDefaults standardUserDefaults];
[accountType setObject:modal.accountId forKey:@"SavedaccountType"];
}
else if (currentSelectedDropDown == 2) {
AIFillOwnershipParserModel *modal = nil;
if (isSearching) {
modal = [filteredArray objectAtIndex:row];
}
else{
modal =[accountTypesPickerArray objectAtIndex:row];
}
NSString *titleString = modal.ownership;
ownershipTypeTxtField.text = titleString;
ownerShipId = modal.ownerShipID;
}
}
这只是让你理解
的一个例子