热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

修改另一个视图控制器的对象-Modifyinganobjectofanotherviewcontroller

Iwouldliketoknowisitpossibleto,letsay,changeormodifyanobjectlikeabuttonofanother

I would like to know is it possible to, let say, change or modify an object like a button of another view controller from AppDelegate.swift. Here is what I tried to begin with but somehow stucked.

我想知道是否可以改变或修改对象,如AppDelegate.swift中另一个视图控制器的按钮。这是我试图开始但不知何故被卡住了。

func application(_ application: UIApplication,didReceiveRemoteNotification userInfo: [AnyHashable : Any],fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

   if application.applicatiOnState== .active {
        if self.window?.rootViewController is homeViewController {
            //modify a button exist in homeViewController


        }
   }

}

Any help is appreciated. Thanks in advance.

任何帮助表示赞赏。提前致谢。

4 个解决方案

#1


2  

You can use the NotificationCenter to send and receive internal notifications (note they are different from local and remote notifications).

您可以使用NotificationCenter发送和接收内部通知(请注意它们与本地和远程通知不同)。

First create your notification doing something like this:

首先创建这样的通知:

extension Notification.Name {
    static let remoteNotificatiOnReceived= Notification.Name("uk.co.company.app.remoteNotificationReceived")
}

Then in your view controller that is to respond do something like this:

然后在你的视图控制器中响应做这样的事情:

class TestViewController: UIViewController {
    var remoteNotificationReceivedObserver: NSObjectProtocol?

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        remoteNotificatiOnReceivedObserver= NotificationCenter.default.addObserver(forName: Notification.Name.remoteNotificationReceived, object: nil, queue: nil, using: { (notification) in
            DispatchQueue.main.async { // because the notification won't be received on the main queue and you want to update the UI which must be done on the main queue.
                // Put the code to change the button here
            }
        })
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)

        if let remoteNotificatiOnReceivedObserver= remoteNotificationReceivedObserver {
            NotificationCenter.default.removeObserver(remoteNotificationReceivedObserver)
        }
    }
}

Then elsewhere in you app you post the notification like this:

然后在你的应用程序的其他地方发布通知,如下所示:

    NotificationCenter.default.post(name: Notification.Name.remoteNotificationReceived, object: nil)

#2


2  

You can use NotificationCenter! add observer in your view controller and remove it while you go back from that view controller(custom notification). and when you receive notification post it!

您可以使用NotificationCenter!在视图控制器中添加观察者并在从该视图控制器返回时将其删除(自定义通知)。当你收到通知后发布它!

If you don't have idea that how to deal with NotificationCenter then refer This SO post !

如果您不知道如何处理NotificationCenter,请参阅此SO帖子!

#3


1  

The only place you should really be interacting with another vew controller like that is during segues (if you are using Storyboards). Even then, you should let the view functions for that controller be responsible for changing the state of its buttons and just pass some variable to the controller or perhaps better set up the controller to listen for notifications. Then your app delegate, or other controller can just post notifications that your home controller listens for.

你应该真正与另一个vew控制器进行交互的唯一地方就是segues(如果你使用的是Storyboard)。即便如此,您应该让该控制器的视图函数负责更改其按钮的状态,并将一些变量传递给控制器​​,或者更好地设置控制器以监听通知。然后您的应用程序委托或其他控制器只能发布您的家庭控制器侦听的通知。

#4


1  

It is possible, but addressing directly members of another ViewController breaks responsibility. It is a good practice to define Interface protocols for internal interactions. In this particular case, it is a good idea to create protcol RemoteNotificationReciverInterface (or kind of RemoteNotificationReciveable according to some modern coding styles advice, although I found it difficult to find appropriate adjective in this case) :

这是可能的,但直接解决另一个ViewController的成员会破坏责任。为内部交互定义接口协议是一种很好的做法。在这种特殊情况下,根据一些现代编码样式建议创建protcol RemoteNotificationReciverInterface(或者RemoteNotificationReciveable的类型是一个好主意,尽管我发现在这种情况下很难找到合适的形容词):

protocol RemoteNotificationReciverInterface: class {
    func didReciveNotification(info : [AnyHashable : Any])
}

Then extent your ViewController( and any view controllers that had to react on Notifications when they are topmost)

然后扩展您的ViewController(以及任何视图控制器,当它们位于最顶层时必须对通知作出反应)

extension HomeViewController: RemoteNotificationReciverInterface {
    func didReciveNotification(info : [AnyHashable : Any]) {
        // Chnage you button, ignore para,eters
    }
}

You can adopt UINavigationContoroller, UITabBarConroller etc. to forward notifications to their topmost controllers, like:

您可以采用UINavigationContoroller,UITabBarConroller等将通知转发给最顶层的控制器,例如:

extension UINavigationController: RemoteNotificationReciverInterface {
    func didReciveNotification(info : [AnyHashable : Any]) {
        (topViewController as? RemoteNotificationReciverInterface)?.didReciveNotification(info: info)
    }
}

And the easily forward it from app delegate.

并且可以从app delegate轻松转发它。

func application(_ application: UIApplication,didReceiveRemoteNotification userInfo: [AnyHashable : Any],fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    if application.applicatiOnState== .active {
        (window?.rootViewController as? RemoteNotificationReciverInterface).didReciveNotification(info: userInfo)
    }
}

推荐阅读
  • 本文介绍了利用ARMA模型对平稳非白噪声序列进行建模的步骤及代码实现。首先对观察值序列进行样本自相关系数和样本偏自相关系数的计算,然后根据这些系数的性质选择适当的ARMA模型进行拟合,并估计模型中的位置参数。接着进行模型的有效性检验,如果不通过则重新选择模型再拟合,如果通过则进行模型优化。最后利用拟合模型预测序列的未来走势。文章还介绍了绘制时序图、平稳性检验、白噪声检验、确定ARMA阶数和预测未来走势的代码实现。 ... [详细]
  • 使用nodejs爬取b站番剧数据,计算最佳追番推荐
    本文介绍了如何使用nodejs爬取b站番剧数据,并通过计算得出最佳追番推荐。通过调用相关接口获取番剧数据和评分数据,以及使用相应的算法进行计算。该方法可以帮助用户找到适合自己的番剧进行观看。 ... [详细]
  • Vue基础一、什么是Vue1.1概念Vue(读音vjuː,类似于view)是一套用于构建用户界面的渐进式JavaScript框架,与其它大型框架不 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • Voicewo在线语音识别转换jQuery插件的特点和示例
    本文介绍了一款名为Voicewo的在线语音识别转换jQuery插件,该插件具有快速、架构、风格、扩展和兼容等特点,适合在互联网应用中使用。同时还提供了一个快速示例供开发人员参考。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 本文详细介绍了git常用命令及其操作方法,包括查看、添加、提交、删除、找回等操作,以及如何重置修改文件、抛弃工作区修改、将工作文件提交到本地暂存区、从版本库中删除文件等。同时还介绍了如何从暂存区恢复到工作文件、恢复最近一次提交过的状态,以及如何合并多个操作等。 ... [详细]
  • Whatsthedifferencebetweento_aandto_ary?to_a和to_ary有什么区别? ... [详细]
  • React基础篇一 - JSX语法扩展与使用
    本文介绍了React基础篇一中的JSX语法扩展与使用。JSX是一种JavaScript的语法扩展,用于描述React中的用户界面。文章详细介绍了在JSX中使用表达式的方法,并给出了一个示例代码。最后,提到了JSX在编译后会被转化为普通的JavaScript对象。 ... [详细]
  • 从零基础到精通的前台学习路线
    随着互联网的发展,前台开发工程师成为市场上非常抢手的人才。本文介绍了从零基础到精通前台开发的学习路线,包括学习HTML、CSS、JavaScript等基础知识和常用工具的使用。通过循序渐进的学习,可以掌握前台开发的基本技能,并有能力找到一份月薪8000以上的工作。 ... [详细]
  • 恶意软件分析的最佳编程语言及其应用
    本文介绍了学习恶意软件分析和逆向工程领域时最适合的编程语言,并重点讨论了Python的优点。Python是一种解释型、多用途的语言,具有可读性高、可快速开发、易于学习的特点。作者分享了在本地恶意软件分析中使用Python的经验,包括快速复制恶意软件组件以更好地理解其工作。此外,作者还提到了Python的跨平台优势,使得在不同操作系统上运行代码变得更加方便。 ... [详细]
  • 本文介绍了Python语言程序设计中文件和数据格式化的操作,包括使用np.savetext保存文本文件,对文本文件和二进制文件进行统一的操作步骤,以及使用Numpy模块进行数据可视化编程的指南。同时还提供了一些关于Python的测试题。 ... [详细]
  • Introduction(简介)Forbeingapowerfulobject-orientedprogramminglanguage,Cisuseda ... [详细]
  • 本博文基于《Amalgamationofproteinsequence,structureandtextualinformationforimprovingprote ... [详细]
  • Thisworkcameoutofthediscussioninhttps://github.com/typesafehub/config/issues/272 ... [详细]
author-avatar
蓝羽月妞妞
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有