My problem is about set new data to my UIPickerView from another ViewController. I have main.storyboard(ViewController.h and ViewController.m), I have a pickerview(I call it aPicker) in it. I have a CustomTableViewController to load some images. When I tap on these images, data of aPicker will change.
aPicker in ViewController.m
- (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;
}
I call custom table here:
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];
Receive in 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;
}
When tap image in table
- (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....
}
When I tap to image, picker only hide and show(I think it mean my program can understand the pointer point to aPicker),but it can't add data from my testArr to aPicker, I also try to reset delegate and datasource received, it till not works. I test with the same code(create testArr, set aPickerAdapter, reloadAllComponent... )in ViewController.m, it works. Why I can't reload it? Help me please!
ps: I'm just a newbie and not good at English. If I have any mistake, please forgive me, thanks
I would do it differently. You are trying to have your TableViewController update another view controller's view directly - you shouldn't. You need instead to pass the data back to your first view controller, so that it can update its own views. There are various techniques for this (see this link), but in your circumstances I would implement a method in your view controller and call it from your custom TableViewController:
In ViewController:
-(void)updatePicker:(NSArray *)newArray {
aPickerAdapter = testArr;
[aPicker reloadAllComponents]
// and do anything else you need
}
In your CustomerTableViewController didSelectRow method:
[self.delegate updatePicker:testArr];