Curnelious
Curnelious

Reputation: 1

How to Keep class alive between views?

We have some Bluetooth class, that creates connection to a bluetooth module . As long as this class is alive, the connection is alive . We are creating that class instance, in viewA . Than when we go to viewB ,we would like to keep the BL class alive, by passing it to the next view , viewB .

How would we do that (without singleton ,just passing it,or using app delegate)

in viewA :

//creating the instance of the BL class

   self.bluetooth=[[BLEModule alloc] init];
    self.bluetooth.delegate=self;
  ...
  ....
//go the next view-viewB (now pass that instance self.bluetooth to viewB)

        UIViewController *next=[[CallTestView alloc]init];
        AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [UIView transitionFromView:delegate.window.rootViewController.view
                            toView:next.view
                          duration:1.5f
                           options:UIViewAnimationOptionTransitionFlipFromLeft
                        completion:^(BOOL finished)
         {
             delegate.window.rootViewController=next;

         }];

Upvotes: 0

Views: 144

Answers (1)

YYfim
YYfim

Reputation: 1412

First, using the AppDelegate is kinda like a singleton (you keep a reference to one object that is initialized once, and you pass the reference to it when someone ask).

But to answer your question, you have a few ways you can accomplish this

1) delegation - viewA is a delegate of viewB and you can pass information between them. Check this out Link

2) KVO - viewB registers to a "radio station" and listen to massages and viewA broadcast to that "radio station" , by that wat you can pass information between viewA and viewB (even between multiple views). Check this out Link

3) segue - you can pass information between viewA and viewB with the segue (if they are connected with one). Check this Link

There are advantages and Disadvantages to every method above. You need to figure out what is best for your case

Upvotes: 1

Related Questions