我的问题是从另一个ViewController向我的UIPickerView设置新数据。我有main.storyboard(ViewController.h和ViewController.m),我有一个pickerview(我称之为aPicker)。我有一个CustomTableViewController来加载一些图像。当我点击这些图片时,aPicker的数据将会改变。
ViewController.m中的aPicker
- (NSInteger)numberOfComponentsInPickerView: (UIPickerView *)pickerView;{
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if(pickerView== aPicker)
{
//I will code it later
}
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;{
if(pickerView==aPicker)
{
return [aPickerAdapter count];
}
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UIView* tmpView= [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 32)];
UILabel *lbl;
if(pickerView==aPicker)
{
lbl = [[UILabel alloc] initWithFrame:CGRectMake(64, 0, 256, 32)];
[lbl setText:[aPickerAdapter objectAtIndex:row]];
}
[tmpView insertSubview:lbl atIndex:0];
return tmpView;
}
我在这里调用自定义表:
NSMutableArray* tranferData= [[NSMutableArray alloc] init];
[tranferData addObject:aPickerAdapter];
[tranferData addObject:aPicker];
[tranferData addObject:self];
aPicker.delegate=self;
[tranferData addObject:aPicker.delegate];
aPicker.dataSource=self;
[tranferData addObject:aPicker.dataSource];
customTableViewController=[[CustomTableViewController alloc] initWithNibName:@"CustomTableViewController" bundle:[NSBundle mainBundle] set:tranferData];
customTableViewController.delegate=self;
[self.view addSubview:customTableViewController.view];
在CustomTableViewController.m中接收
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil set:(NSMutableArray *)myarray{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
myArray=myarray;
// Custom initialization on passed parameter observation object
aPickerAdapter=[myarray objectAtIndex:0];
aPicker=[myarray objectAtIndex:1];
tranferSeft=[myarray objectAtIndex:2];
pickerDelegate=[myarray objectAtIndex:3];
pickerDataSource=[myarray objectAtIndex:4];
}
return self;
}
点击表格中的图片
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *string= [@"tap to row " stringByAppendingString:[NSString stringWithFormat:@"%d",indexPath.row]];
NSLog(string);
//NSString *str= [onlineIdList objectAtIndex:indexPath.row];
//UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Demo" message:str delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
//[alert show];
NSMutableArray *testArr= [[NSMutableArray alloc]init];
[testArr addObject:@"2"];
[testArr addObject:@"4"];
[testArr addObject:@"6"];
//aPicker.delegate= pickerDelegate;
//aPicker.dataSource= pickerDataSource;
**aPickerAdapter=testArr;
[aPicker reloadAllComponents];**
[self hideAndShowSubChannel];//this funtion is to hide and show aPicker, defaut aPicker is hide, on first tap it will show([aPicker setHidden:NO]), and it hide on second tap....
}
当我点击图片时,选择器只隐藏和显示(我认为这意味着我的程序可以理解指向aPicker的指针),但它无法将我的testArr中的数据添加到aPicker,我也尝试重置委托和收到数据源,直到不起作用。我在ViewController.m中使用相同的代码(创建testArr,设置aPickerAdapter,reloadAllComponent ...)进行测试,它可以正常工作。为什么我无法重装?请帮帮我!
ps:我只是一个新手而且不擅长英语。如果我有任何错误,请原谅我,谢谢答案 0 :(得分:2)
我会以不同的方式做到这一点。您正试图让TableViewController直接更新另一个视图控制器的视图 - 您不应该这样做。您需要将数据传递回第一个视图控制器,以便它可以更新自己的视图。有各种各样的技术(参见this link),但在你的情况下,我会在你的视图控制器中实现一个方法,并从你的自定义TableViewController调用它:
在ViewController中:
-(void)updatePicker:(NSArray *)newArray {
aPickerAdapter = testArr;
[aPicker reloadAllComponents]
// and do anything else you need
}
在您的CustomerTableViewController中,didSelectRow方法:
[self.delegate updatePicker:testArr];
答案 1 :(得分:0)
尝试实施UIPickerViewDelegate-Method
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
而不是viewForRow-method。
例如:
- (NSString *)pickerview:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [aPickerAdapter objectAtIndex:row];
}
答案 2 :(得分:0)
我通过@pbasdf和他link的方式解决了这个问题。 (@Matt Price的回答真的很有用)。 Tks all:D