从 appdelegate 更改 UILabel 文本
Change UILabel text from appdelegate
我有一个 UIViewcontroller 让我们说 "DialerViewController" 它有一个 UILabel
@IBOutlet 弱变量状态文本:UILabel!
,
它的默认值为 "pending",我如何使用应用程序委托更改 statusText 的值,我们假设应用程序委托从服务器下载文本并需要在完成后更新 statusText。
我是 swift 开发的新手,最好的解决方法是什么?
我认为你不能那样做,因为 AppDelegate 方法是在你的应用程序的特定状态下调用的,而这个方法可能是好的:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
但是调用时,您的 viewController 尚未加载。
从网络加载不是 AppDelegate 的责任,添加新的网络服务 class 并将其注入视图控制器。只需了解有关层架构的知识即可。它对新开发者来说非常强大,祝你好运。
如果 DialerViewController
是您应用中唯一的视图控制器,您可以这样处理它...
(window?.rootViewController as? DialerViewController)?.statusText?.text = "YOURTEXT"
另一种选择是让 DialerViewController
实例在从服务器下载文本时在应用程序委托中观察一些特定的通知和 post 这个通知。
// create an extension for your own notification
extension Notification.Name {
static let textWasDownloadedNotification = Notification.Name("textWasDownloadedNotification")
}
class DialerViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// make your dialer view controller listen to your new notification
NotificationCenter.default.addObserver(self, selector: #selector(updateLabel), name: .textWasDownloadedNotification, object: nil)
}
// function that gets called when a notification is received
@objc func updateLabel(_ notification: Notification) {
// get the new text from the notification's `userInfo` dictionary
let text = notification.userInfo?["text"] as? String
// update the label
statusText.text = text
}
}
// somewhere in your app delegate...
// prepare the `userInfo` dictionary with the information that is needed
let userInfo = ["text": "This is the new text."]
// post the notification
NotificationCenter.default.post(name: .textWasDownloadedNotification,
object: nil,
userInfo: userInfo)
见https://developer.apple.com/documentation/foundation/notificationcenter。
我有一个 UIViewcontroller 让我们说 "DialerViewController" 它有一个 UILabel @IBOutlet 弱变量状态文本:UILabel! , 它的默认值为 "pending",我如何使用应用程序委托更改 statusText 的值,我们假设应用程序委托从服务器下载文本并需要在完成后更新 statusText。
我是 swift 开发的新手,最好的解决方法是什么?
我认为你不能那样做,因为 AppDelegate 方法是在你的应用程序的特定状态下调用的,而这个方法可能是好的:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
但是调用时,您的 viewController 尚未加载。
从网络加载不是 AppDelegate 的责任,添加新的网络服务 class 并将其注入视图控制器。只需了解有关层架构的知识即可。它对新开发者来说非常强大,祝你好运。
如果 DialerViewController
是您应用中唯一的视图控制器,您可以这样处理它...
(window?.rootViewController as? DialerViewController)?.statusText?.text = "YOURTEXT"
另一种选择是让 DialerViewController
实例在从服务器下载文本时在应用程序委托中观察一些特定的通知和 post 这个通知。
// create an extension for your own notification
extension Notification.Name {
static let textWasDownloadedNotification = Notification.Name("textWasDownloadedNotification")
}
class DialerViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// make your dialer view controller listen to your new notification
NotificationCenter.default.addObserver(self, selector: #selector(updateLabel), name: .textWasDownloadedNotification, object: nil)
}
// function that gets called when a notification is received
@objc func updateLabel(_ notification: Notification) {
// get the new text from the notification's `userInfo` dictionary
let text = notification.userInfo?["text"] as? String
// update the label
statusText.text = text
}
}
// somewhere in your app delegate...
// prepare the `userInfo` dictionary with the information that is needed
let userInfo = ["text": "This is the new text."]
// post the notification
NotificationCenter.default.post(name: .textWasDownloadedNotification,
object: nil,
userInfo: userInfo)
见https://developer.apple.com/documentation/foundation/notificationcenter。