【问题标题】:Change UILabel text from appdelegate从 appdelegate 更改 UILabel 文本
【发布时间】:2019-05-22 13:44:37
【问题描述】:

我有一个 UIViewcontroller,比如说“DialerViewController”,它有一个 UILabel @IBOutlet 弱 var statusText:UILabel! , 默认值为“pending”,如何使用应用程序委托更改 statusText 的值,假设应用程序委托从服务器下载文本并需要在完成后更新 statusText。

我是快速开发的新手,解决这个问题的最佳方法是什么?

【问题讨论】:

  • 在 Appdelegate 中从服务器下载文本有什么原因吗?
  • DialerViewController 是您应用中唯一的视图控制器吗?如果不是...请提供有关您的视图控制器层次结构的更多信息。

标签: ios swift uilabel appdelegate


【解决方案1】:

我认为您不能这样做,因为 AppDelegate 方法是在您的应用程序的特定状态下调用的,而这种方法可能很好:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool

但是当它被调用时,你的 viewController 还没有加载。

【讨论】:

    【解决方案2】:

    从网络加载它不是 AppDelegate 的责任,添加新的网络服务类并将其注入视图控制器。只需获得有关层架构的知识,扎实。它对新开发者来说非常强大,祝你好运。

    【讨论】:

      【解决方案3】:

      如果DialerViewController 是您应用中唯一的视图控制器,您可以这样处理它...

      (window?.rootViewController as? DialerViewController)?.statusText?.text = "YOURTEXT"
      

      另一种选择是让DialerViewController 实例观察一些特定的通知,并在从服务器下载文本时将此通知发布到应用程序委托中。

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

      【讨论】:

      • 这个答案的最大问题是 App Delegate 是所有这些都应该做的最后一个地方。 App Delegate 绝对不应该直接更新视图控制器的标签。如果有的话,App Delegate 应该将新数据传递给视图控制器,并且视图控制器应该用数据更新它自己的标签。但即使这样也不是很好,因为 App Delegate 不应该负责加载数据。
      • @rmaddy 这些绝对是值得讨论和值得一提的论点。尽管如此,问题是“如何使用应用程序委托更改 statusText 的值”,这些是一些可能性。因此,您的评论应该是对问题恕我直言的答案或评论。
      • 使用通知中心的想法非常棒,而且看起来更好,感谢分享。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      • 1970-01-01
      • 1970-01-01
      • 2012-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多