How to pass variables between dispatchers?

I find it hard with Xcode; for some reason, it just won't let me pass a variable from one class to another class controller. It should work, I basically just copied / pasted from my other classes (it works on all of them ... except this one). I was on it all night, tried everything I could think of, and still remains.

Here is the view controller class where I am making the call:

ResultadosViewController.h:

#import <UIKit/UIKit.h> #import "Filme.h" #import "Festival.h" #import "Top10Discos.h" #import "Peca.h" @class DetalhesViewController; @interface ResultadosViewController : UIViewController { // Navegation DetalhesViewController *dvc; BOOL isViewPushed; // What i'd really like to pass lol NSArray *array_resultados; } @property (nonatomic, retain) NSArray *array_resultados; @property (nonatomic, readwrite) BOOL isViewPushed; @end* 

ResultadosViewController.m:

 #import "ResultadosViewController.h" #import "DetalhesViewController.h" #import "Filme.h" #import "Peca.h" #import "Top10Discos.h" #import "Festival.h" @implementation ResultadosViewController @synthesize isViewPushed, array_resultados; (...) - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Navigation logic -- create and push a new view controller if (indexPath.row == 2) { if(dvc != nil) [dvc dealloc]; NSString *ffs = [[array_resultados objectAtIndex:indexPath.row] TituloFilme]; dvc = [[DetalhesViewController alloc] initWithNibName:@"DetailedView" bundle:[NSBundle mainBundle]]; **resultadosControllerCell.array_resultados = [self array_resultados];** *"Request for member 'array_resultados' in something not a structure or union"* //Push the view controller to the top of the stack. [self.navigationController pushViewController:dvc animated:YES]; } } 

And here is another class that I want to send to the array:

DetalhesViewController.h:

 #import <UIKit/UIKit.h> #import "Filme.h" #import "Festival.h" #import "Top10Discos.h" #import "Peca.h" @interface DetalhesViewController : UIViewController { // Navegacao NSArray *array_resultados; } @property (nonatomic, retain) NSArray *array_resultados; @end 

I'm not sure if any of you will see a .m file for this class; in this case, just ask.

Thanks in advance Hal

PS: I tried with other variables (other types too), cleared / rebuilt, recreated the xib file, you name it ... I got away from the tricks: (

+4
source share
6 answers

First of all, do not use -> - this is direct access to the instance variable. This may work, but you are modifying the instance variables of an object without its knowledge, which just requires trouble.

And no, Adam Rosenfield did not mean dvc->array_resultados ; he meant resultadosControllerCell->array_resultados what he said and which he based on what you said.

The correct solution is a mixture of your source string and your revision of Adam's string:

 dvc.array_resultados = [self array_resultados]; 

This goes through the property that you declared in the DetalhesViewController class.

Speaking of which, you should declare this property as copy , not retain . Otherwise, you will find that you have someone else's mutable array that they will then modify - a worse mojo.

+4
source

This does not mean that you are answering your question, but your memory management is a bit inconvenient. This line:

 [dvc dealloc]; 

should look like this:

 [dvc release]; dvc = nil; 

In Cocoa, you should never call dealloc directly - follow the retain/release/autorelease and everything will work better for the intended purpose.

+3
source

Just guess, but did you try using the arrow operator -> instead of the dot operator . ?

 resultadosControllerCell->array_resultados = [self array_resultados]; 
0
source

Well, I had it; using a cheap trick to show info:

 UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:ffs message:@"this gets covered" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; UILabel *myTextField = [[UILabel alloc] init]; myTextField.text = @"FFS!"; CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 130.0); [myAlertView setTransform:myTransform]; [myTextField setBackgroundColor:[UIColor whiteColor]]; [myAlertView addSubview:myTextField]; [myAlertView show]; [myAlertView release]; 

Actually I hate using it, but I'm already late for the day. I should have delivered it yesterday.

Thanks for the help guys, if you were lucky enough to find a solution, please let me know. You never know if this will happen again: /

Hooray!

0
source

A more competent way to do this is to separate the data from both view controllers to the model. You will have a separate class (NSObject) called ResultadoModel.h / m. This will be singleton, so both classes can access the same instance.

You will access the array by doing something like this (in both vcs):

 [[[ResultadoModel sharedInstance] array_resultados] propertyOrMethod]; 

You can search how to create a singleton, it is very simple and very powerful.

0
source

Follow these simple steps, please, this should work.

In your SecondViewController:

  • Create an initializer in the second view controller. Example:

    - (id)initWithResultsArray: (NSArray *) resultsArray ;

  • Create a variable to store the array. Say myResultsArray .

  • Inside the initWithResultsArray store the resultsArray value in myResultsArray .

  • Initialize the SecondViewController using initWithResultsArray instead of init .

  • Imagine your controller, as usual, you can work with myResultsArray .

Hope this helps.

Monica ツ

0
source

All Articles