How to access a property of a viewcontroller from another one? iPhone

前端 未结 3 1532
刺人心
刺人心 2020-12-11 12:15

I define a property in the viewcontroller A

  @property (nonatomic) BOOL updateOnServer;

and also synthesize it, give it values and then I

相关标签:
3条回答
  • 2020-12-11 13:00

    A simple way to save and retrieve information of variable ,although not prefered some time is NSUserDefaults.
    Use NSUserDefaults Check Out the Link for how to use NSUserDefaults. Or you can declare Global Variable.

    0 讨论(0)
  • 2020-12-11 13:02

    You can always use a singleton class as follows:

    Create a new class as follows:

    ServerCheck.h
    
    #import <Foundation/Foundation.h>
    
    @interface ServerCheck : NSObject
    {
        BOOL updateOnServer;
    }
    @property (nonatomic) BOOL updateOnServer;
    
    + (id)sharedSingletonController;
    @end
    
    ServerCheck.m
    
    #import "ServerCheck.h"
    
    @implementation ServerCheck
    @synthesize updateOnServer
    +(ServerCheck*)sharedSingletonController{
    
        static ServerCheck *sharedSingletonController;
    
        @synchronized(self) {
            if(!sharedSingletonController){
                sharedSingletonController = [[ServerCheck alloc]init];
            }
        }
    
        return sharedSingletonController;
    }
    
    -(id)init{
        self = [super init];
        if (self != nil) {
    
            }
        return self;
    }
    
    @end
    

    You can access the BOOL value as follows:

    ServerCheck *serverData = [ServerCheck sharedSingletonController];
    serverData.updateOnServer = YES;
    
    0 讨论(0)
  • 2020-12-11 13:13

    You set updateOnServer within viewControllerA, and in viewControllerB you create a new instance of updateOnServer which you haven't set to anything. So it takes the default value of BOOL which is NO.

    Kindly look here for possible solution.

    You have many ways to do : make the variable global, static, singleton, pass as an argument, post as notification.

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