1

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

TMob
  • 1,278
  • 1
  • 13
  • 33
Huy Hóm Hỉnh
  • 597
  • 7
  • 18
  • You say: "When I tap to image, picker only hide and show". Both view controllers are shown at the same time? – kelin Oct 14 '14 at 07:38
  • Could you please explain your code further, or add the code-snippet of your property-declaration? For example what is aPickerAdapter... – TMob Oct 14 '14 at 07:47
  • hi, aPickerAdapter is a NSMutableArray. It contains data for aPicker(you can see it on numberOfRowsInComponent and viewForRow). I think aPickerAdapter is the matter, maybe the pointer to aPickerAdapter in ViewController.m and aPickerAdapter in CustomTableViewController.m are not equal. But I don't know how to handle that :( – Huy Hóm Hỉnh Oct 14 '14 at 08:00
  • @kelin: I only have a storyboard. Every thing will load automatically, also my customtable. When it showed, I tap image in tableview, aPicker will show(and hide->show->hide....every tap). :D – Huy Hóm Hỉnh Oct 14 '14 at 08:07
  • @HuyHómHỉnh, you want to say that you not even run your application on the simulator? :D – kelin Oct 14 '14 at 08:10
  • @kelin: sorry if I don't understand what you mean. I run on the simulator. 'Automatically' means when I click "Run" button in Xcode, all it show in front of my eyes(I don't do anything to load anything). My first action is to tap to image to see aPicker. Are you understands? – Huy Hóm Hỉnh Oct 14 '14 at 08:21
  • @HuyHómHỉnh, yes I do. I asked this to insure that ViewController is loaded into memory. How do you access `aPickerAdapter` variable of ViewController on `-tableView:didSelectRowAtIndexPath:` method of CustomTableViewController? – kelin Oct 14 '14 at 08:42
  • I add it to tranferData(at second code blocks), send to customtable and receive at third code block. But I don't know how to send data back :( – Huy Hóm Hỉnh Oct 14 '14 at 08:53

3 Answers3

2

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];
Community
  • 1
  • 1
pbasdf
  • 21,386
  • 4
  • 43
  • 75
  • I understand your way, but I don't know how to call method updatePicker(in Viewcontroller.m) from CustomTableViewController.m. I can't understand what seft.delegate mean? seft in didSelectRow will mean CustomTableViewController? Thanks! – Huy Hóm Hỉnh Oct 14 '14 at 08:40
  • Yes, self in `didSelectRow` will refer to the `customTableViewController`. But you set the `delegate` property immediately after you instantiated it (`customTableViewController.delegate=self`). That line is in your main view controller, so `self` there refers to the main view controller. Hence in your `customTableViewController`, `self.delegate` refers back to your main view controller. – pbasdf Oct 14 '14 at 09:59
0

Try implementing the UIPickerViewDelegate-Method

  • (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

instead of the viewForRow-method.

For example like this:

- (NSString *)pickerview:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{    
    return [aPickerAdapter objectAtIndex:row];
}
TMob
  • 1,278
  • 1
  • 13
  • 33
  • If having viewForRow, the titleForRow may be not works. I used viewForRow because I need to custom this picker. About specifing delegate and datasource in the storyboard, is it drag and drop into View Controller(yellow button above screen design on storyboard)? Thanks :D – Huy Hóm Hỉnh Oct 15 '14 at 03:14
  • I'm sorry, the comment about the Datasource/Delegate was not correct. I misunderstood something. – TMob Oct 15 '14 at 05:35
0

I've solved it by the way of @pbasdf anh his link (answer of @Matt Price really useful). Tks all :D

Community
  • 1
  • 1
Huy Hóm Hỉnh
  • 597
  • 7
  • 18