Reload data for UIPickerView from another ViewController

Huy Hóm Hỉnh

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

pbasdf

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];

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Reload data and components in UIPickerView

From Dev

How to reload data in a TableView from a different ViewController in Swift

From Dev

Initialization Data after it receiving from another ViewController

From Dev

How to fill TableView with data from another ViewController

From Dev

Passing Core Data attributes from viewController to another viewController

From Dev

Swift: Passing data from a tableView in a ViewController to another ViewController

From Dev

Reload collection view data from another view class

From Dev

Dismiss ViewController AND reload previous / pass data back?

From Dev

UITableView Reload data after dismissing a ViewController

From Dev

Dismiss ViewController AND reload previous / pass data back?

From Dev

How extract from UITableView data (UIImage and String) to another ViewController

From Dev

UIPickerView data from Parse objects

From Dev

Swift - How to reload ViewController from a ModalPopupController

From Dev

How can I change the strings of a UIPickerView from another UIPickerview?

From Dev

Passing data to another ViewController without moving to the ViewController

From Dev

UICollectionViewCell sending data to another viewController

From Dev

Multiple UIPickerView in one ViewController

From Dev

Updating UILabel from another ViewController

From Dev

Present TabBarController from another ViewController

From Dev

Passing Data from ViewController to instantiated custom viewcontroller

From Dev

Missing data from Parse to UIPickerView in Swift

From Dev

Missing data from Parse to UIPickerView in Swift

From Dev

reload data in TabItem from another window when changing tab on tabcontrol WPF

From Dev

Reload data tableView from JSON

From Dev

Reload data from server angular

From Dev

Display UIPickerView based on another UIPickerView

From Dev

Update the title of a UIButton from a UIPickerView from another controller

From Dev

Pushing Data from UITableViewCell to ViewController

From Dev

Passing data from MyScene to ViewController?

Related Related

  1. 1

    Reload data and components in UIPickerView

  2. 2

    How to reload data in a TableView from a different ViewController in Swift

  3. 3

    Initialization Data after it receiving from another ViewController

  4. 4

    How to fill TableView with data from another ViewController

  5. 5

    Passing Core Data attributes from viewController to another viewController

  6. 6

    Swift: Passing data from a tableView in a ViewController to another ViewController

  7. 7

    Reload collection view data from another view class

  8. 8

    Dismiss ViewController AND reload previous / pass data back?

  9. 9

    UITableView Reload data after dismissing a ViewController

  10. 10

    Dismiss ViewController AND reload previous / pass data back?

  11. 11

    How extract from UITableView data (UIImage and String) to another ViewController

  12. 12

    UIPickerView data from Parse objects

  13. 13

    Swift - How to reload ViewController from a ModalPopupController

  14. 14

    How can I change the strings of a UIPickerView from another UIPickerview?

  15. 15

    Passing data to another ViewController without moving to the ViewController

  16. 16

    UICollectionViewCell sending data to another viewController

  17. 17

    Multiple UIPickerView in one ViewController

  18. 18

    Updating UILabel from another ViewController

  19. 19

    Present TabBarController from another ViewController

  20. 20

    Passing Data from ViewController to instantiated custom viewcontroller

  21. 21

    Missing data from Parse to UIPickerView in Swift

  22. 22

    Missing data from Parse to UIPickerView in Swift

  23. 23

    reload data in TabItem from another window when changing tab on tabcontrol WPF

  24. 24

    Reload data tableView from JSON

  25. 25

    Reload data from server angular

  26. 26

    Display UIPickerView based on another UIPickerView

  27. 27

    Update the title of a UIButton from a UIPickerView from another controller

  28. 28

    Pushing Data from UITableViewCell to ViewController

  29. 29

    Passing data from MyScene to ViewController?

HotTag

Archive