How to get NSString variable value from NSObject in ViewController

I am trying to set up an object to manage all my data so that it can set preferences in the background so that it looks like my tables are browsing faster than now, etc.

This is what I am trying to achieve.

enter image description here

I set the variable to NSObject from secondVC when the tableviewcell is selected as follows:

- (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]; } //etc 

At the end, you will see the method that is installed 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 correctly outputs the result.

however, I have no idea how to call productionSearchObjectStringFVC and pass the value that it stores in uitableviewcell, which I would like to pass in my first controller.

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); } 

This nslog prints zero ..

I have three questions

1, how do I get the correct value in the first value controller.

2, should I use viewDidAppear like this? .. I think not .. how can I do it better

3, What do you think is a good way to do this type of thing, since in the future I would like to use NSObjectClass to analyze information, cache, etc. it's all about the feelings, leaving the views simply displayed when the data is ready, hopefully helps in productivity.

Any help would be greatly appreciated, as I really want to study this material, as I know its important knowledge for me.

+4
source share
2 answers

Your question is so beautifully and clearly formatted and sketchy that you are ashamed to ask you to do a search. But here it is:

Finding data sharing between view managers

You will find a lot of good discussion about data sharing between view controllers.

In short, however, I can tell you why your code is not working. In your method tableView:didSelectRowAtIndexPath: each time you create (alloc/init) a new instance of your VehicleControllerNSObject class. Then go back to your first view controller in viewDidAppear : again you create (alloc/init) a whole new instance each time.

So, you have several objects going and going, and they have nothing to do with each other. This is a bit like providing important information to one person at a bus station, and then randomly selects another person and tries to extract the same information from it.

So, one quick idea would be to create only one instance of your VehicleControllerNSObject (just aside, this is a little weird name for the class, since in general all objective-c objects are descendants of NSObject anyway. I'll just call it VehicleController now)

So, let's say you need a "sharedInstance" VehicleController . You can add a class method to VehicleController to give you a way to easily get this sharedInstance file:

 +(VehicleController*)sharedInstance { static VehicleController *sharedInstance_ = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedInstance_ = [[VehicleController alloc] init]; }); return sharedInstance_; } 

So, to get this instance in the methods of 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 out the search, many people have a good discussion about this. This is just one approach. I hope this at least makes sense why your code doesn't work.

Hope this helps.

+4
source

The answer to question 3. No.

I think the best way to do something like this is to use Core Data and NSManagedObject.

The combination of the UITableViewController and NSFetchedResultsController that comes from the Core Data sqlite backup storage if the preset loads and updates your UITableView.

It would be a long time to describe everything here. So I will stay there.

If you do not want to do this, it is always possible to use shared pointers for a mutable object or to use a singleton object to transfer information between a UIViewController.

+1
source

Source: https://habr.com/ru/post/1380490/


All Articles