0

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;

         }];
Abubakr Dar
  • 4,078
  • 4
  • 22
  • 28
Curnelious
  • 1
  • 16
  • 76
  • 150
  • You can do it by declaring your BL class as strong in appdelegate.h, (like-property(strong, nonatomic) BLEModule *bluetooth & alloc init at didfinishlaunching. this way you class will be alive throughout whole app. You can use it, where ever you want (like - appdelegate.bluetooth) – Pawan Rai Feb 16 '15 at 10:56

1 Answers1

1

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

Community
  • 1
  • 1
YYfim
  • 1,402
  • 1
  • 9
  • 24
  • Thanks, but its actually not my question. I know what delegates are, and i know ways to pass data between views. My goal is that when viewA is dead, i will pass from viewA to the next view, an instance of the BL class, that was created in viewA . so right before viewA is dead, he will send the BL instance to the next view . – Curnelious Feb 16 '15 at 09:41
  • have you tried using viewWillDisappear/viewDidDisappear? – YYfim Feb 16 '15 at 09:43