我在UIPickerView中遇到了重装组件的问题。这是我的代码:
在我的头文件(.h)上:
@interface GoToAyatViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>{
IBOutlet UIPickerView *pickerViewObj;
}
@property (nonatomic, retain) IBOutlet UIPickerView *pickerViewObj;
@end
关于我的实现(.m):
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
ratusan = [[NSMutableArray alloc] initWithObjects:@" 0",@" 1",nil];
puluhan = [[NSMutableArray alloc] initWithObjects:@" 0",@" 1",@" 2",
@" 3",@" 4", @" 5",
@" 6", @" 7", @" 8",
@" 9",nil];
satuan = [[NSMutableArray alloc] initWithObjects:@" 0",@" 1",@" 2",
@" 3",@" 4", @" 5",
@" 6", @" 7", @" 8",
@" 9",nil];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
pickerViewObj = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 250, 400, 80)];
pickerViewObj.showsSelectionIndicator = YES;
pickerViewObj.dataSource = self;
pickerViewObj.delegate = self;
[self.pickerViewObj selectRow:2 inComponent:0 animated:NO];
[pasalTypeButton setBackgroundColor:[UIColor blueColor]];
self.isPasal = YES;
self.title = self.kitab;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return kPICKERCOLUMN;
NSLog(@"numberOfComponentsInPickerView jalan");
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
NSLog(@"numberOfRowsInComponent jalan");
if (component==0){
return [ratusan count];
}else if (component == 1) {
return [puluhan count];
}else{
return [satuan count];
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSLog(@"set title run");
switch (component)
{
case 0:
return [ratusan objectAtIndex:row];
//break;
case 1:
return [puluhan objectAtIndex:row];
//break;
case 2:
return [satuan objectAtIndex:row];
//break;
}
return nil;
}
在实现中我把ibaction设置为在uipickerview上设置第一个组件的新值并激活reloadallcomponents:
-(IBAction) showAyat
{
self.ratusan = nil;
self.ratusan = [[NSMutableArray alloc] initWithObjects:@" 0",@" 1",@" 2",nil];
[self.pickerViewObj reloadAllComponents];
NSLog(@"total ratusan=%d", [self.pickerViewObj numberOfRowsInComponent:0]);
}
这里是我的笔尖设置的图片,以防我在设置委托或数据源时出错。
上面的代码当我触发(点击按钮)showAyat方法时,它不会在运行时更改我的uipickerview的值,但我可以看到numberOfRowsInComponent:0已使用NSLog更改为3(在2之前)。我不知道我的pickerview没有更新显示有什么问题。 请有人帮帮我。
答案 0 :(得分:3)
<强>问题:强>
您正在创建两个UIPickerView
。
两者都从delegate
获取数据值。
由于在Xib中创建的选择器在代码中初始化另一秒pickerViewObj
时失去与UIPickerView
变量的连接。因此,当时提出的UIPickerView
不会改变其价值。
<强>解决方案:强>
从代码中删除用于创建UIPickerView
的代码。