简体   繁体   中英

Method in UIViewController being called from AppDelegate is not using existing objects

I've been driving myself nuts trying to figures this out, and have tried just about everything to my knowledge. I'm hoping someone can figure out this issue, let me explain my current setup.

I'm using the Bump-api within my iOS app. I have the configureBump method in the AppDelegate which is called in the initial launch of the app. So the app is connected to the Bump servers from the start.

Next, I have a NavigationController with the following UIViewControllers stacked in this order:

ViewControllerA -> ViewControllerB -> ViewControllerC

VCA (ViewControllerA) is the first view when starting the app. In the ViewWillLoad I have the following code:

myAppDelegate = [[UIApplication sharedApplication] delegate];
[myAppDelegate vibrateBump:NO];

vibrateBump is a method in my AppDelegate which I use to turn off & on bump. In which case, I want it off on VCA since there is nothing I want to be transfered.

However, in VCB I have the same strip of code with vibrateBump:YES , this is where I want Bump to work. This all works fine, dont worry I'm getting there.

Now, in the configureBump method within the AppDelegate , I have it setup to call a method from VCB when bumped. This is when things get weird. Here is an example of the method being called in VCB :

@implementation VCB {
   FMDatabase * db;
}

- (void)viewDidLoad{
   //db is set up here...
}

-(void) otherMethodWithinVC{
   //Some action here...
}

-(void) myMethod:(NSArray*)myArray{
   [db open] //This does now work

   Do some stuff here.... //All this works

   [db close] //This does now work
   [self otherMethodWithinVC] //This does now work
}

Why is [db open] not working? its as if there is another instance of myMethod being called. When I changed it up while testing, and decided to create db within myMethod , db started to work.

Here is how I have it setup within the configureBump method:

- (void) configureBump {
    VCB * vcb = [[VCB alloc] init];

    [[BumpClient sharedClient] setChannelConfirmedBlock:^(BumpChannelID channel) {
       //Some action here...
    }];

    [[BumpClient sharedClient] setDataReceivedBlock:^(BumpChannelID channel, NSData *data) {
        dataArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];

        if([dataArray count]>0){
            if([vcb myMethod:dataArray];
        }
    }];

    //Some action here...

}

Any ideas?

When you are calling mymethod from the appdelegate you are creating a different instance of VCB controller but in the case of

myAppDelegate = [[UIApplication sharedApplication] delegate]; 

you are creating a single instance of the appdelegate like a singleton

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM