ViewController 和 AppDelegate 之间使用 swift 2 for iOS 进行双向通信 [英] Bi-Directional communication between ViewController and AppDelegate with swift 2 for iOS

查看:33
本文介绍了ViewController 和 AppDelegate 之间使用 swift 2 for iOS 进行双向通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 iPhone 应用程序,它使用 PubNub 网络将数据发布到 Raspberry Pi 并从它订阅消息.Xcode 7,Swift 2.这些消息是使用我称为客户端的对象发送的,该对象在 AppDelegate.swift init() 方法中声明.我已经能够成功地从我唯一的一个 ViewController 引用我的 AppDelegate,但是从 AppDelegate 将数据返回到我的 ViewController 对我来说变得棘手.

I am developing an iPhone app that uses the PubNub network to publish data to a Raspberry Pi and subscribe to messages from it. Xcode 7, Swift 2. These messages are sent using an object that I call client, which is declared in the AppDelegate.swift init() method. I have been able to successfully reference my AppDelegate from my one and only ViewController, however getting data back to my ViewController from the AppDelegate is where things have gotten tricky for me.

我知道这种双向控制可能并不理想.我不喜欢两个对象相互了解的概念.但是,由于这是一个小型的DIY家庭项目,暂时我想知道如何让我的AppDelegate直接调用ViewController中的函数.我在堆栈溢出中发现的所有建议都已过时或不起作用.我该怎么做才能轻松地将字符串从 AppDelegate 传递给我的 ViewController?

I'm aware that this bi-directional control is probably not ideal. I don't like the concept of both objects knowing about each other. However, since this is for a small DIY home project, for the time being I would like to know how I can get my AppDelegate to directly call a function within the ViewController. All of the suggestions I have found on stack overflow are out of date or do not work. What can I do to easily pass a String to my ViewController from the AppDelegate?

更具体地说:我想从 AppDelegate 底部的 client() 方法中调用我的 ViewController 的 setMessage() 方法,为 ViewController 提供我在客户端函数中收到的字符串.

To be more specific: I want to call the setMessage() method of my ViewController from within my client() method at the bottom of AppDelegate, giving the ViewController the string that I receive in the client function.

视图控制器

import UIKit

class ViewController: UIViewController {

    var currentMessage = NSString()
    @IBOutlet weak var receivedMessage: UILabel!
    @IBOutlet weak var messageInput: UITextField!
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

    @IBAction func sendButtonPressed(sender: AnyObject) {
        let messageToSend = messageInput.text
        appDelegate.client.publish(messageToSend, toChannel: "my_channel", compressed: false, withCompletion: nil)
    }

    func setMessage(message : String) {
        receivedMessage.text = message
    }
}

应用委托

import UIKit
import PubNub

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate, PNObjectEventListener {

    var client : PubNub
    var config : PNConfiguration

    override init() {
        config = PNConfiguration(publishKey: "pub-c-ecaea738-6b0f-4c8d-80a9-a684e405dbe9",
                                 subscribeKey: "sub-c-b71a16f8-5056-11e5-81b5-02ee2ddab7fe")

        client = PubNub.clientWithConfiguration(config)
        client.subscribeToChannels(["my_channel"], withPresence: false)

        super.init()
        client.addListener(self)
    }

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        return true
    }

    func applicationWillTerminate(application: UIApplication) {
        client.unsubscribeFromAll()
    }

    func client(client: PubNub!, didReceiveMessage message: PNMessageResult!) {
        let messageString = String(message.data.message);
        print(messageString)
    }
}

推荐答案

从你的 AppDelegate 你可以使用

From your AppDelegate you can use

let vc=self.window!.rootViewController as! ViewController

vc.setMessage(someText)

但是,我会使用 NSNotification 并让视图控制器订阅它.

but, I would use an NSNotification and have the view controller subscribe to that.

这篇关于ViewController 和 AppDelegate 之间使用 swift 2 for iOS 进行双向通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆