Simple Passing of variables between classes in Xcode

前端 未结 3 1523
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-18 11:27

I am trying to do an ios app but I am stuck at passing data between classes . This is my second app . The first one was done whit a global class , but now I need multiple cl

相关标签:
3条回答
  • 2020-12-18 12:03

    if you use segue between 2 controllers you must overload prepareToSegue method

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
    // check if it's the good segue with the identifier
    if([[segue identifier] isEqualToString:@"blablabla"])
    {
        // permit you to get the destination segue
        [segue destinationViewController];
        // then you can set what you want in your destination controller
    }
    }
    
    0 讨论(0)
  • 2020-12-18 12:11

    There are several things to do. Depending on the app, you could either add a variable to the AppDelegate class, making it availible to all classes through a shared instance. The most common thing (I think) is to make a singleton. For that to work, you can make a class, say StoreVars, and a static method that returns the object, which makes the class "global". Within the method, you initialize all your variables, like you always would. Then you can always reach them from wherever.

    @interface StoreVars : NSObject
    
    @property (nonatomic) NSArray * mySharedArray;
    + (StoreVars*) sharedInstance;
    
    @implementation StoreVars
    @synthesize mySharedArray;
    
    + (StoreVars*) sharedInstance {
        static StoreVars *myInstance = nil;
        if (myInstance == nil) {
            myInstance = [[[self class] alloc] init];
            myInstance.mySharedArray = [NSArray arrayWithObject:@"Test"];
        }
        return myInstance;
    }
    

    This will make a singleton. If you remember to import "StoreVars.h" in your two viewControllers, you can access the now shared array like this;

    [StoreVars sharedInstance].mySharedArray;
                   ^
    

    This is a method returning a StoreVars object. Within the StoreVars class, you can implement any object and initialize it in the static method. Just always remember to initialize it, or else, all your object will be 0/nil.

    If you are not a fan of the UINavigationController and would rather use segues, it's a lot easier, but can make your app rather "messy" imo. There is a method implemented in UIViewController you should overload:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        // Make sure your segue name in storyboard is the same as this line
        if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
        {
            // Get reference to the destination view controller
            YourViewController *vc = [segue destinationViewController];
    
            // Pass any objects to the view controller here, like...
            [vc setMyObjectHere:object];
        }
    }
    

    source: How to pass prepareForSegue: an object

    Do some research before asking questions like this. Read some tutorials, and try out yourself, and then ask questions related to what you are really looking for. It's not everyday people want to do all the work for you, but sometimes you're lucky. Like today.

    Cheers.

    0 讨论(0)
  • 2020-12-18 12:19

    The problem you face is quite perplexing for beginners. "Solving" it wrong way can result in learning a ton of bad habits.
    Please have a look at Ole Begemann's excellent tutorial on Passing Data Between View Controllers - it's really worth reading.

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