C.Johns C.Johns - 1 year ago 119
iOS Question

How to get NSString variable value from NSObject to ViewController

I am trying to set up an object to control all of my data so it can set things up in the background to it appears my tableviews load faster than they do now etc.

This is what I am trying to achieve.

enter image description here

I am setting a variable in the NSObject from the secondVC when the tableviewcell is selected like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//Access selected cells content (cell.textLabel.text)
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

//Parent view logic (sends info back to the correct cell in parent view)
if (parentViewSelectedIndexPath.section == 0)
if (parentViewSelectedIndexPath.row == 0)
//Predicates restrict the values that will be returned from the query
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@",@"MANUFACTURER",cell.textLabel.text];
NSArray *filterArray = [myDataArray filteredArrayUsingPredicate:predicate];
//[[self delegate] setManufactureSearchFields:filterArray withIndexPath:indexPath]; //This is where I pass the value back to the mainview
//Using Object
VehicleControllerNSObject *vehicleControllerNSObject = [[VehicleControllerNSObject alloc] init];
[vehicleControllerNSObject setFirstCell:filterArray];


At the end there you can see the method that is getting set up in the VechicleControllerNSObject which looks like this.

-(void)setFirstCell:(NSArray *)array{

manufactureSearchObjectStringFVC = [[array valueForKey:@"MANUFACTURER"] objectAtIndex:0];
NSLog(@"%@", manufactureSearchObjectStringFVC); // this prints the correct value to the console

As you can see this prints the correct output fine.

however I have no idea how to call manufactureSearchObjectStringFVC and pass the value it holds into the uitableviewcell that I would like to pass it in on my firstviewcontroller.

This is what I have for testing atm.

- (void)viewDidAppear:(BOOL)animated
[super viewDidAppear:animated];

VehicleControllerNSObject *vehicleControllerNSObject = [[VehicleControllerNSObject alloc] init];
manufactureSearchObjectString = vehicleControllerNSObject.manufactureSearchObjectStringFVC;
NSLog(@"%@", vehicleControllerNSObject.manufactureSearchObjectStringFVC);


That nslog prints null..

I have three questions

1, how do I get the correct value into the first valuecontroller.

2, should I be using viewDidAppear like this?.. I think not.. how can I do this better

3, Do you think this is a good way of doing this type of thing, as in the future i would like to use the NSObjectClass to parse info, cache etc all behind the senses leaving the views to just display when the data is ready hopefully helping performance..

Any help would be hugely appreciated as I really want to learn this stuff as i know its important for me to know.

Answer Source

Your question is so beautifully and clearly formatted and diagrammed that it seems a shame to ask you to do a search. But here it is:

Search for Sharing Data between View Controllers

You'll find many good discussions about sharing data between view controllers.

Briefly, though, I can tell you why your code isn't working. In your tableView:didSelectRowAtIndexPath: method, you are creating (alloc/init) a new instance of your VehicleControllerNSObject class each time. Then back in your first view controller on viewDidAppear:, again you are creating (alloc/init) a whole new instance each time.

So you have multiple objects coming and going and they have nothing to do with each other. It's a bit like giving some important information to one person at a bus station and then later randomly picking some other person out and trying to retrieve that same information from her.

So one quick idea would be to create just once instance of your VehicleControllerNSObject (just an aside, that's a bit of a strange name for a class since generally all objective-c objects are descendants of NSObject anyway. I'm just going to call that VehicleController for now)

So let's say you wanted a 'sharedInstance' of VehicleController. You could add a class method to VehicleController to give you a way to easily get that one sharedInstance:

+(VehicleController*)sharedInstance {

    static VehicleController *sharedInstance_ = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        sharedInstance_ = [[VehicleController alloc] init];

    return sharedInstance_;

So to get that instance in methods in other classes you can just do something like :

VehicleController *sharedController = [VehicleController sharedInstance];
sharedController.someProperty = someValue;

// and then back in your first view controller, similarly:
VehicleController *sharedController = [VehicleController sharedInstance];
id someValue = sharedController.someProperty;

Again, check the search, many people have had good discussions on this. This is just one approach. I hope it at least makes sense why your code wasn't working.

Hope that helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download