适用于iOS的Swift 2的ViewController和AppDelegate之间的双向通信 [英] Bi-Directional communication between ViewController and AppDelegate with swift 2 for iOS

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

问题描述

我正在开发一个iPhone应用程序,该应用程序使用PubNub网络将数据发布到Raspberry Pi并订阅来自它的消息.Xcode 7,Swift2.这些消息是使用我称为client的对象发送的,该对象在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.

ViewController

ViewController

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
    }
}

AppDelegate

AppDelegate

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.

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

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