Obj-C - How to pass data between viewcontrollers using a singleton?

后端 未结 2 1389
陌清茗
陌清茗 2020-12-22 14:26

Alright, so this is an extension to a question I asked last night. I have a little firmer grasp on how data can be passed between view controllers using various techniques.

相关标签:
2条回答
  • 2020-12-22 14:57

    I wouldn't recommend using a Singleton as a good way to pass data around your application. Most apps are simple enough that this kind of central access is not necessary, and it usually creates a maintenance nightmare... but I don't think the fact that you're using a Singleton is actually important to getting your code working.

    Assuming you have access to the data in ViewController1, in your case through the a Singleton instance of Model (which needs a more descriptive name), then all you have to do is pass through the data to ViewController2 when it is created and presented, which eliminates the need for a Singleton at all.

    Once you create the controller, set the data you need, and then present the view controller - which is basically what you're doing anyway.

    As to why it's not working: Is the view controller being presented, just not with the correct data? Or is there actually an issue presenting the controller at all? I would set a breakpoint in the go: action of ViewController1, make sure the data you expect is in the textfield, correctly populates the Model and that the value is correctly pulled out of the Model in ViewController2.

    Unless you've removed some of the code, it looks like you correctly populate the Model property in ViewController1, but in ViewController2 you refer to a local ivar passedTextrather than pulling it from the model.

    On a separate note, the way to go back from a presented modal view controller is usually to dismiss that controller, not to re-create the initial controller and present that over the top.

    0 讨论(0)
  • 2020-12-22 15:06

    Your addressing, and memory management is just plain... off. Firstly, there's absolutely no reason to create a singleton for this, but that's beside the point here.

    Secondly, when declaring properties, (atomic, assign) is defaulted to if not otherwise specified, which means your string:

    @property (nonatomic)NSString *passedValue;
    

    is weak sauce, ripe for deallocation and destruction at a moments notice. Declare it copy, strong, or retain.

    Thirdly, there's absolutely no reference to your singleton in the pushed view controller, yet you seem to have the belief that objects that are named the same in different classes retain their value (especially when #import'ed). Not so. You need to reference your singleton and pull the value of [Model sharedModel].passedText into that text field.

    In fact, I fixed your sample in two lines:

    //ViewController2.m
    #import "ViewController2.h"
    
    //actually import the singleton for access later
    #import "Model.h"
    
    @interface ViewController2 () {
        NSString *passedtext;
    }
    @end
    @implementation ViewController2
    @synthesize lbl = _lbl;
    @synthesize passedValue = _passedValue;
    - (void)viewDidLoad
    {
    
     // do code stuff here
        NSLog(@"passedText = %@",passedText);
        //actually reference the singleton this time
        _lbl.text = [Model sharedModel].passedText;
    
        [super viewDidLoad];
    }
    - (void)viewDidUnload
    {
        [self setLbl:nil];
        [super viewDidUnload];
        // Release any retained subviews of the main view.
    }
    - (IBAction)back:(id)sender {
    
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        ViewController *vc = (ViewController *) [storyboard instantiateViewControllerWithIdentifier:@"welcome"];
        [self presentModalViewController:vc animated:YES];
    }
    @end
    

    Which yields this:

    Fixed the code

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