ViewController 和 AppDelegate 之间的双向通信 swift 2 for iOS
Bi-Directional communication between ViewController and AppDelegate with swift 2 for iOS
我正在开发一个 iPhone 应用程序,它使用 PubNub 网络将数据发布到 Raspberry Pi 并订阅来自它的消息。 Xcode 7, Swift 2. 这些消息是使用我称为客户端的对象发送的,该对象在 AppDelegate.swift init() 方法中声明。我已经能够从我的唯一 ViewController 成功引用我的 AppDelegate,但是从 AppDelegate 将数据返回到我的 ViewController 对我来说事情变得棘手了。
我知道这种双向控制可能并不理想。我不喜欢两个对象相互了解的概念。然而,由于这是一个小型的 DIY 家庭项目,目前我想知道如何让我的 AppDelegate 直接调用 ViewController 中的函数。我在堆栈溢出上找到的所有建议都已过时或不起作用。我该怎么做才能轻松地将字符串从 AppDelegate 传递到我的 ViewController?
更具体地说:我想从 AppDelegate 底部的 client() 方法中调用 ViewController 的 setMessage() 方法,为 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
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 使用
let vc=self.window!.rootViewController as! ViewController
vc.setMessage(someText)
但是,我会使用 NSNotification
并让视图控制器订阅它。
我正在开发一个 iPhone 应用程序,它使用 PubNub 网络将数据发布到 Raspberry Pi 并订阅来自它的消息。 Xcode 7, Swift 2. 这些消息是使用我称为客户端的对象发送的,该对象在 AppDelegate.swift init() 方法中声明。我已经能够从我的唯一 ViewController 成功引用我的 AppDelegate,但是从 AppDelegate 将数据返回到我的 ViewController 对我来说事情变得棘手了。
我知道这种双向控制可能并不理想。我不喜欢两个对象相互了解的概念。然而,由于这是一个小型的 DIY 家庭项目,目前我想知道如何让我的 AppDelegate 直接调用 ViewController 中的函数。我在堆栈溢出上找到的所有建议都已过时或不起作用。我该怎么做才能轻松地将字符串从 AppDelegate 传递到我的 ViewController?
更具体地说:我想从 AppDelegate 底部的 client() 方法中调用 ViewController 的 setMessage() 方法,为 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
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 使用
let vc=self.window!.rootViewController as! ViewController
vc.setMessage(someText)
但是,我会使用 NSNotification
并让视图控制器订阅它。