从 appdelegate 更新 viewcontroller - 最佳实践?
update viewcontroller from appdelegate - best practice?
我正在尝试在 AppDelegate.swift 中实现 CoreLocation 方法的 iBeacons(方法在 AppDelegate 中实现以确保 App 后台功能)
在 "ViewController.swift" 附带的 SingleView 应用程序中,将数据从 AppDelegate 传递到 ViewController 以更新视图(例如 UIImages、UILabels 或 UITableViews)的最佳做法是什么?
我已经成功实施了两种不同的方法:
1) 委托,通过触发 Viewcontroller 内部的委托方法 AppDelegate.swift:
protocol BeaconLocationDelegate { func minorBeaconChanged(minorValue:NSNumber) }
var locationDelegate: BeaconLocationDelegate
locationDelegate?.minorBeaconChanged(nearestBeacon.minor)
ViewController.swift:
在 viewDidLoad 方法中:
(UIApplication.sharedApplication().delegate as AppDelegate).locationDelegate = self
(我觉得这看起来很难看 - 有什么更好的方法可以将此 属性 声明为委托?)
协议实现:
func minorBeaconChanged(minorValue: NSNumber) {
// do fancy stuff here
}
2) 通过在 AppDelegate 中创建对 ViewController 的引用:
let viewController:ViewController = window!.rootViewController as ViewController
viewController.doSomethingFancy()
这两种方法对我来说都很好,但我认为第一种通过委托的方法更优雅,并且当你有多个 ViewControllers 时是更好的选择。
有什么建议吗?
我认为第二种解决方案要简单得多。它还确保视图控制器不会内存不足("released")并且应用程序委托不会尝试联系不存在的对象。
此外,我不理解在多个控制器的情况下的委托参数。在那种情况下,您将不得不有多个代表,这确实不是一个理想的情况。或者您将不得不从外部将委托更改为不同的视图控制器 - 也不是很优雅而且风险很大。
对于多视图控制器场景,我建议通过 NSNotificationCenter
进行通知。这是一个失去耦合,确保只有那些需要做出反应的对象才能接收到消息。视图控制器应该在创建时开始监听通知,并在它们消失时从通知中心注销。
这个怎么样?
在 AppDelegate 中
var minorBeaconChanged: NSNumber -> () = { minor in }
并在更新位置时调用此方法。
在ViewController的视图中
( UIApplication.sharedApplication().delegate as? AppDelegate )!.minorBeaconChanged = { minor in
// Do with minor
}
编辑:2015 年 8 月 27 日
如果你想要多个观察者。
在 AppDelegate 中
var minorBeaconObservers: [ NSNumber -> () ] = []
并用 'minor'
调用 minorBeaconObservers 中的所有项目
在ViewController的视图中
( UIApplication.sharedApplication().delegate as? AppDelegate )!.minorBeaconObservers.append(
{ minor in
// Do with minor
}
)
我正在尝试在 AppDelegate.swift 中实现 CoreLocation 方法的 iBeacons(方法在 AppDelegate 中实现以确保 App 后台功能)
在 "ViewController.swift" 附带的 SingleView 应用程序中,将数据从 AppDelegate 传递到 ViewController 以更新视图(例如 UIImages、UILabels 或 UITableViews)的最佳做法是什么?
我已经成功实施了两种不同的方法:
1) 委托,通过触发 Viewcontroller 内部的委托方法 AppDelegate.swift:
protocol BeaconLocationDelegate { func minorBeaconChanged(minorValue:NSNumber) }
var locationDelegate: BeaconLocationDelegate
locationDelegate?.minorBeaconChanged(nearestBeacon.minor)
ViewController.swift:
在 viewDidLoad 方法中:
(UIApplication.sharedApplication().delegate as AppDelegate).locationDelegate = self
(我觉得这看起来很难看 - 有什么更好的方法可以将此 属性 声明为委托?)
协议实现:
func minorBeaconChanged(minorValue: NSNumber) {
// do fancy stuff here
}
2) 通过在 AppDelegate 中创建对 ViewController 的引用:
let viewController:ViewController = window!.rootViewController as ViewController
viewController.doSomethingFancy()
这两种方法对我来说都很好,但我认为第一种通过委托的方法更优雅,并且当你有多个 ViewControllers 时是更好的选择。
有什么建议吗?
我认为第二种解决方案要简单得多。它还确保视图控制器不会内存不足("released")并且应用程序委托不会尝试联系不存在的对象。
此外,我不理解在多个控制器的情况下的委托参数。在那种情况下,您将不得不有多个代表,这确实不是一个理想的情况。或者您将不得不从外部将委托更改为不同的视图控制器 - 也不是很优雅而且风险很大。
对于多视图控制器场景,我建议通过 NSNotificationCenter
进行通知。这是一个失去耦合,确保只有那些需要做出反应的对象才能接收到消息。视图控制器应该在创建时开始监听通知,并在它们消失时从通知中心注销。
这个怎么样?
在 AppDelegate 中
var minorBeaconChanged: NSNumber -> () = { minor in }
并在更新位置时调用此方法。
在ViewController的视图中
( UIApplication.sharedApplication().delegate as? AppDelegate )!.minorBeaconChanged = { minor in
// Do with minor
}
编辑:2015 年 8 月 27 日
如果你想要多个观察者。
在 AppDelegate 中
var minorBeaconObservers: [ NSNumber -> () ] = []
并用 'minor'
调用 minorBeaconObservers 中的所有项目在ViewController的视图中
( UIApplication.sharedApplication().delegate as? AppDelegate )!.minorBeaconObservers.append(
{ minor in
// Do with minor
}
)