0

I am super confused.

I have 2 controllers, lets call them controller1 and controller2.

In controller1.m I have this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    NSString *object = self.objects[indexPath.row];
    cell.textLabel.text = [object description];
    return cell;
}

and in controller2.m I am trying to reload the tableview in controller1.m:

- (void)GetRequest
{

    NSArray *tableData = [dataSource.areaData GetPurchaseOrderItems:[NSString stringWithFormat:@"%@%@",areaPickerSelectionString,unitPickerSelectionString]];

    if(!purchaseOrder.objects){
        purchaseOrder.objects = [[NSMutableArray alloc]init];
    }

    for(int i = 0; i < [tableData count]; i++){
        [purchaseOrder.objects addObjectsFromArray:[tableData objectAtIndex:i]];
        NSLog(@"%@",[tableData objectAtIndex:i]);
    }
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [purchaseOrder.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    NSLog(@"%@", purchaseOrder.objects);

    //[self.tableView reloadData];

}

I have tried the following:

controller1.h:

@property(nonatomic, retain) UITableView *tableView;

controller1.m:

@synthesize tableView;

controller2.h:

#import "controller1.h"

@interface controller2 ()
{


    controller1 *purchaseOrder;

}

- (void)viewDidLoad {
     purchaseOrder = [[controller1 alloc]init];
}

and then [purchaseOrder.tableView reloadData];

and my tableView doesnt get reloaded. WTF ? I have no idea what I am doing wrong here. I also get this warning on:

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

and here is the warning:

Local declaration of 'tableView' hides instance variable
2
  • There seems to be another variable name 'tableView'. You need to rename it or make sure you're targeting the right one. Please show more code to check that. Commented Apr 30, 2015 at 19:24
  • There are a bunch of things odd about this but let's start with purchaseOrder = [[controller1 alloc]init];. This creates a new controller1. If you want purchaseOrder to update, you need to push or present it first. I suspect you probably already have a controller1 somewhere else but this new one has no relationship to any that might have already been on the screen. Commented Apr 30, 2015 at 19:24

1 Answer 1

1

You need a reference to controller1 within controller2. In Controller2.h, declare a controller1 property.

#import "Controller1.h"
@interface Controller2 : UIViewController
@property (nonatomic, strong) Controller1 *controller1;
@end

I am going to make the assumption that controller1 is segueing to controller2. So you can pass the reference of controller1 to controller2 in prepareForSegue. Be sure to #import Controller2.h in Controller1.m. In Controller1.m:

- (void)prepareForSegue:(UIStoryBoardSegue *)segue sender:(id)sender
{
    if ([segue.destinationViewController isKindOfClass:[Controller2 class]]) {
        Controller2 *controller2 = (Controller2 *)segue.destinationViewController;
        controller2.controller1 = self;  // now you can reference the tableView in controller2
    }  
}

Now in Controller2.m, you can reload the table view where you like.

- (void)GetRequest
{
    // ...
    [self.controller1.tableView reloadData];
}
Sign up to request clarification or add additional context in comments.

7 Comments

I dont understand what I am suppose to put in the prepareForSegue (after the ...)
It is a method that controller1 inherits from UIViewController. I couldn't remember the actual syntax so I copped out and put ... Start typing "- prepareFor" in controller1 and autocomplete should fill the rest out for you.
Edited my answer to fill out the method name.
I get 3 errors with your code: 1. Expected a type in the prepareForSegue method, 2. destinationController not found on object type '__strong id' and 3 Property controller1 not found on object type controller2
1. Cast the destinationViewController to Controller2. 2. It is destinationViewController instead of destinationController. Also, it looks like you did sender.DestinationViewController when it should be segue.destinationViewController. 3. Make sure to #import the header to Controller2 in order to see the controller1 property that we added.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.