0

私は iOS 開発が初めてで、BLE デバイスに接続するアプリの作成に苦労しています。多くのView Controllerがあるため、すべてのView Controllerで周辺機器を常に接続しておく必要があります。

これを実現するために、すべての BLE 接続メソッドをSingleton. これはうまく機能します。私は connect メソッドを呼び出して、周辺機器View Controllerに接続します。Singleton

さて、問題は、UILabelからの接続状態(スキャン中、接続中、接続中、切断中)で更新したいView ControllerにあることSingletonです。

そこで、からインスタンスを取得してView Controller、ラベルを次のように直接変更しようとしました。

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

また、次のようなビュー コントローラー クラスをインスタンス化しました。

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

次に、メインでメソッドを作成しようとしました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 Controller中でviewDidLoad私は電話します:

[[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") でラベルを更新します。

4

2 に答える 2

3

を使用する必要があります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
}

シグルトン

- (void) foo {
    /// some actions

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

    ///
}

推奨事項: 通知名を定数に移動し、定数名を使用します。命名規則については、Apple のガイドラインを参照してください

于 2014-07-21T09:13:19.350 に答える
1

これを行う適切な方法は、経由NSNotificationです。このコミュニケーションデバイスは、まさにこのような状況のためのものです。潜在的な受信者が利用可能かどうかを気にせずにメッセージをブロードキャストします。

ビューコントローラーでは、/ が表示/非表示になったときに/を呼び出しNSNotificationCenterます。経由で通知を投稿します。addObserverremoveObserverpostNotification:

于 2014-07-21T09:06:23.467 に答える