ios - 从单例更改 View Controller 中的 UILabel

标签 ios objective-c singleton bluetooth-lowenergy cbcentralmanager

我是 iOS 开发的新手,我正在努力制作一个连接到 BLE 设备的应用程序。因为我有很多 View Controller ,所以我需要在所有 View Controller 中始终保持外围设备连接。

为此,我在 Singleton 中实现了所有 BLE 连接方法。 .这很好用,我从 View Controller 调用连接方法和 Singleton连接到外围设备。

现在,问题是我有一个 UILabel在我的 View Controller 中,我想使用来自 Singleton 的连接状态(扫描、连接、连接、断开连接)进行更新.

所以我尝试从 View Controller 中获取实例并像这样直接更改标签:

MainViewController *controller = [[MainViewController alloc] init];
controller.myLabel.text =  @"TEST";

我还实例化了 View Controller 类:

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MyStoryboard" bundle: nil];
MainViewController *controller = (MainViewController*)[mainStoryboard instantiateViewControllerWithIdentifier:@"MainVC"];

然后我尝试在main中创建一个方法View Controller :

- (void) updateLabel:(NSString *) labelText{
     NSLog(@"CALLED IN MAIN");
     self.myLabel.text = labelText;
 }

然后从Singleton调用它喜欢:

MainViewController *controller = [[MainViewController alloc] init];
[controller updateLabel:@"TEST"]

调用正确(显示了 NSLog)但标签未更新。

我真的不知道如何更新我的 View Controller来自 Singleton 的标签.也不知道我尝试这样做的方式是否正确。

如有任何建议或帮助,我们将不胜感激。谢谢。

----- 更新: -----

感谢 Mundi 和 Nikita,我得到了一个更好的方法来通过 NSNotification 实现我需要的东西。对于所有需要它的人,我是这样做的:

在我的 View ControllerviewDidLoad我打电话:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateConnectionLabel:) name:@"connectionLabelNotification" object:nil];

然后在同一个类中,我实现了通知观察者方法,如下所示:

- (void)updateConnectionLabel:(NSNotification *) notification {
     if ([[notification name] isEqualToString:@"connectionLabelNotification"]) {
        self.connectionLabel.text = notification.object; //The object is a NSString
     }
}

然后在我的Singleton ,当我需要时我会打电话:

[[NSNotificationCenter defaultCenter] postNotificationName:@"connectionLabelNotification" object:[NSString stringWithFormat:@"CONNECTED"]];

View Controller收到来自 Singleton 的通知它使用我在通知对象上添加的文本更新标签(在本例中为@"CONNECTED")。

最佳答案

你需要使用NSNotification

示例代码如下:

viewDidLoad中:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(mySelector:)
                                             name:DeviceStateChanged
                                           object:nil];

dealloc中:

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:DeviceStateChanged
                                              object:nil];

还在ViewController中添加一个方法:

- (void) mySelector:(NSNotification *) notification {
    // action performed
}

Sigleton

- (void) foo {
    /// some actions

    // device connected
    [[NSNotificationCenter defaultCenter] postNotificationName:DeviceStateChanged object:self];

    ///
}

建议:将通知名称移至常量并使用常量名称。有关命名约定,请参阅 Apple 指南

关于ios - 从单例更改 View Controller 中的 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24861276/

相关文章:

actionscript-3 - 避免全局状态

ios - 当键盘出现 Swift 时出现移动 View 问题

ios - 从 csv 文件动态分配值给 swift 字典

ios - 基于 UIPinch 缩放图像

ios - UITabBarItem 的背景颜色?(Swift/xCode)

iphone - UINavigationController - 在弹出 View Controller 之前运行代码

ios - NSRegularExpression 如何给我一个 NSRange 越界?

ios - 如何将NSDictionary数据添加到NSMutableArray?

c++单例在父类中创建子类

c# - 如何在 Razor 页面中使用相同的单例实例?