【发布时间】:2015-02-16 19:06:05
【问题描述】:
我们有一些蓝牙类,可以创建与蓝牙模块的连接。
只要这个类是活着的,连接就是活着的。
我们正在 viewA 中创建该类实例。
当我们转到 viewB 时,我们希望通过将 BL 类传递给下一个视图 viewB 来保持其存活。
我们将如何做到这一点(没有单例,只是传递它,或者使用应用委托)
在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;
}];
【问题讨论】:
-
你可以通过在 appdelegate.h 中将你的 BL 类声明为 strong 来做到这一点,(like-property(strong, nonatomic) BLEModule *bluetooth & alloc init at didfinishlaunching。这样你的类将在整个过程中保持活跃整个应用程序。您可以在任何地方使用它(例如 - appdelegate.bluetooth)
标签: ios objective-c