How do I pass variables between view controllers?

前端 未结 6 1374
礼貌的吻别
礼貌的吻别 2020-12-06 20:21

i\'m having a hard time with Xcode; for some reason, it just won\'t let me pass a variable from one view controller class to another. It should work, i was basically just co

相关标签:
6条回答
  • 2020-12-06 20:59

    Follow these simple steps please, it should work.

    In your SecondViewController:

    1. Create an initializer in the second View Controller. Ex:

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

    2. Create a variable for holding the array. Say myResultsArray.

    3. Inside the initWithResultsArray method, save the value of resultsArray to myResultsArray.

    4. Initialize the SecondViewController using initWithResultsArray instead of just init.

    5. Present your controller as usual, you will be able to work with myResultsArray.

    Hope this helps.

    Monica ツ

    0 讨论(0)
  • 2020-12-06 21:01

    First off, don't use -> — that's direct instance-variable access. It may work, but you're changing another object's instance variables without its knowledge, which is just asking for trouble.

    And no, Adam Rosenfield didn't mean dvc->array_resultados; he meant resultadosControllerCell->array_resultados, which is what he said and which he based on what you said.

    The correct solution is a blend of your original line and your revision of Adam's line:

    dvc.array_resultados = [self array_resultados];
    

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

    Speaking of which, you should declare that property as copy, not retain. Otherwise, you'll find yourself holding somebody else's mutable array, which they will then modify—more bad mojo.

    0 讨论(0)
  • 2020-12-06 21:09

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

    resultadosControllerCell->array_resultados  = [self array_resultados];
    
    0 讨论(0)
  • 2020-12-06 21:12

    This doesn't exaclty answer your question, but your memory management is a bit wonky. This line:

    [dvc dealloc];
    

    should read like this:

    [dvc release];
    dvc = nil;
    

    In Cocoa, you should never call dealloc directly -- follow the retain/release/autorelease pattern and things will work better and as intended.

    0 讨论(0)
  • 2020-12-06 21:14

    Ok i've had it; using a cheap trick to show the 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];
    

    Really hate using this, but i'm already a day late. I should have delivered it by yesterday.

    Thanks for your help guys, if you happen to find the solution please let me know. You never know if it'll happen again :/

    Cheers!

    0 讨论(0)
  • 2020-12-06 21:19

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

    You would 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's very simple and very powerful.

    0 讨论(0)
提交回复
热议问题