即使在使用view.setNeedsDisplay()刷新视图后,.isHidden属性也不会反映视图的更改

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了即使在使用view.setNeedsDisplay()刷新视图后,.isHidden属性也不会反映视图的更改相关的知识,希望对你有一定的参考价值。

从app delegate接收后我传递一个静态字符串并使用set needs Layout刷新movieDetailsViewController,函数和代码正常工作,但更改不会反映在视图中。

AppDelegate中: -

let storyboard = UIStoryboard(name: "Main", bundle: nil)
                    let movieDetailsVC = storyboard.instantiateViewController(withIdentifier: "MovieDetailsViewController") as! MovieDetailsViewController




                        if (rootViewController?.isKind(of: MovieDetailsViewController.self))!
                        {
                            if notificationContentType == 1
                            {
                                movieDetailsVC.FROMPAGE = "FROMAPPDELEGATE"
                                movieDetailsVC.view.setNeedsDisplay()
                            }
                        }

视图控制器: -

override func viewDidLoad()
    {
        super.viewDidLoad()

        let appDelegate: AppDelegate? = UIApplication.shared.delegate as? AppDelegate
        appDelegate?.read()

        if FROMPAGE == "FROMAPPDELEGATE"
        {
//                self.updateUI()
            print("Function is Called")
            //print(self.view.frame)
            //self.view.setNeedsDisplay()

            let appDelegate: AppDelegate? = UIApplication.shared.delegate as? AppDelegate
            appDelegate?.read()


            var currentreleaseId : Int!
            var currentrentalType : Int!

            currentreleaseId = resultDictionaryFromFCM.value(forKey: "releaseId") as? Int
            let convertedId = String(currentreleaseId)
            currentrentalType = resultDictionaryFromFCM.value(forKey: "rentalType") as? Int
            let elapsedTime = resultDictionaryFromFCM.value(forKey: "elapsedTime") as? Float

            if convertedId == ApiClassStruct.RELEASEID
            {
                let movieStartDate = resultDictionaryFromFCM.value(forKey: "clientStartTime") as? Double
                defaults.set(movieStartDate, forKey: "MOVIE_START_DATE")
                if currentrentalType == 1
                {
                    defaults.set("true", forKey: "isStillPlaying")
                    self.tableViewOutlet.isHidden = true
                    self.seekBarOutlet.isHidden = false


                    let maximumDuration = defaults.integer(forKey: "MAXIMUMDURATION")
                    self.seekBarMaximumDurationLabelOutlet.text = self.convertTime(miliseconds: maximumDuration)
                    self.seekBarOutlet.maximumValue = Float(maximumDuration)

                    let currentDate = NSDate()
                    let dateFormatter = DateFormatter()
                    dateFormatter.dateFormat = "yyyy-MM-dd, HH:mm:ss"
                    dateFormatter.timeZone = NSTimeZone(name: "IST") as TimeZone!
                    let date = dateFormatter.date(from: dateFormatter.string(from: currentDate as Date))
                    let currentDATE = date!.timeIntervalSince1970 * 1000

                    let movieStartDate = defaults.value(forKey: "MOVIE_START_DATE") as? Double

                    let difference = currentDATE - movieStartDate!

                    self.seekBarOutlet.setValue(Float(difference), animated: true)
                    let selectedValue = "(Int(self.seekBarOutlet.value))"
                    let convertedValue = self.convertTime(miliseconds: Int(selectedValue)!)
                    self.seekBarMinimumDurationLabelOutlet.text = convertedValue

                    self.myTimer =  Timer.scheduledTimer(timeInterval:1, target: self, selector: #selector(self.updateSlider), userInfo: nil, repeats: true)

                    print("Play condition ended")
                }
}

但这些变化并没有反映出来,长期坚持这个问题。

答案
let movieDetailsVC = storyboard.instantiateViewController(withIdentifier: "MovieDetailsViewController") as! MovieDetailsViewController

创建MovieDetailsViewController的新实例,并修改它。如果您不提供该实例,则不会显示任何更改。如果您已经提供了MovieDetailsViewController实例,则所呈现的实例不会受其影响。

我建议使用通知来通知MovieDetailsViewController更新其UI。

例如,在app delegate中使用它来创建和发布新通知:

NotificationCenter.default.post(name: NSNotification.Name("movieDetailsUpdateNotif"),
                                object: self, userInfo: nil)

然后在MovieDetailsViewController.viewDidLoad注册以收听这些通知:

override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self,
                                           selector: #selector(receiveNotification(notification:)),
                                           name: NSNotification.Name("movieDetailsUpdateNotif"),
                                           object: nil)
}

要清理它,请在deinit中取消注册(只是表现得很好):

deinit {
    NotificationCenter.default.removeObserver(self)
}

最后,实现MovieDetailsViewController.receiveNotification(notification:)来处理通知:

@objc fileprivate func receiveNotification(notification: NSNotification) {
    // update UI here, e.g. hide things, show other, etc.
    // this will be called when AppDelegate creates a and posts that notification
}

以上是关于即使在使用view.setNeedsDisplay()刷新视图后,.isHidden属性也不会反映视图的更改的主要内容,如果未能解决你的问题,请参考以下文章

更新 Label.text setNeedsDisplay() 不起作用 - Swift

即使已弃用,在 Display 上使用 .getWidth 是不是安全

即使在使用 renderTo 之后,Extjs 按钮也不会在 div 内呈现

颤振:即使使用了`provider`,小部件仍在重建

即使在使用 await [重复] 之后,Javascript 也难以使用承诺值

CGImageRef 即使在释放后也会使用大量内存