简体   繁体   English

从另一个ViewController重新加载UIPickerView的数据

[英]Reload data for UIPickerView from another ViewController

My problem is about set new data to my UIPickerView from another ViewController. 我的问题是关于从另一个ViewController将新数据设置到UIPickerView。 I have main.storyboard(ViewController.h and ViewController.m), I have a pickerview(I call it aPicker) in it. 我有main.storyboard(ViewController.h和ViewController.m),我有一个pickerview(我称它为aPicker)。 I have a CustomTableViewController to load some images. 我有一个CustomTableViewController来加载一些图像。 When I tap on these images, data of aPicker will change. 当我点击这些图像时,aPicker的数据将改变。

aPicker in ViewController.m 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;
}    

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 在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. 当我点击图像时,选择器只能隐藏和显示(我认为这意味着我的程序可以理解指向aPicker的指针),但是无法将数据从testArr添加到aPicker,我还尝试重置委托和接收到的数据源,直到不起作用。 I test with the same code(create testArr, set aPickerAdapter, reloadAllComponent... )in ViewController.m, it works. 我在ViewController.m中使用相同的代码测试(创建testArr,设置aPickerAdapter,reloadAllComponent ...),它可以工作。 Why I can't reload it? 为什么我不能重新加载? Help me please! 请帮帮我!

ps: I'm just a newbie and not good at English. ps:我只是新手,英语不好。 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. 您正在尝试让TableViewController直接更新另一个视图控制器的视图-不应该。 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: 有多种技术(请参阅此链接 ),但是在您的情况下,我会在您的视图控制器中实现一个方法,并从您的自定义TableViewController调用它:

In ViewController: 在ViewController中:

-(void)updatePicker:(NSArray *)newArray {
    aPickerAdapter = testArr;
    [aPicker reloadAllComponents]
    // and do anything else you need
}

In your CustomerTableViewController didSelectRow method: 在您的CustomerTableViewController didSelectRow方法中:

[self.delegate updatePicker:testArr];

Try implementing the UIPickerViewDelegate-Method 尝试实现UIPickerViewDelegate-Method

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

instead of the viewForRow-method. 而不是viewForRow方法。

For example like this: 例如这样:

- (NSString *)pickerview:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{    
    return [aPickerAdapter objectAtIndex:row];
}

I've solved it by the way of @pbasdf anh his link (answer of @Matt Price really useful). 我已经通过@pbasdf及其链接 (@Matt Price的答案非常有用)解决了这一问题。 Tks all :D Tks全部:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM