ios - 即使在使用 view.setNeedsDisplay() 刷新 View 后,.isHidden 属性也不会反射(reflect)对 View 的更改

标签 ios swift swift3 view

收到应用程序委托(delegate)后,我传递了一个静态字符串并使用 set needs Layout 刷新了 movieDetailsViewController,函数和代码正常工作,但更改没有反射(reflect)在 View 中。

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()
                            }
                        }

View Controller :-

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")
                }
}

但是变化没有反射(reflect)出来,长期卡在这个issue上。

最佳答案

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

创建 MovieDetailsViewController 的新实例,并对其进行修改。如果您不提供该实例,则不会看到任何更改。如果您已经呈现了 MovieDetailsViewController 实例,则呈现的实例将不受其影响。

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

例如,在应用委托(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
}

关于ios - 即使在使用 view.setNeedsDisplay() 刷新 View 后,.isHidden 属性也不会反射(reflect)对 View 的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49689127/

相关文章:

ios - 当我从另一个 View 中退出时,Touch ID 自动触发成功

ios - URL 查询字符串中的空格使其为零

ios - 注册 Nib 时自定义单元格( Collection View )

android - 是否有任何跨平台工具可以将响应式网站转变为 native 移动应用程序?

ios - 尝试将 Parse 中的一行数据提取到 Swift 中的数组中

ios - 转换二维数组

ios - Swift:UIButton textLabel.text 值在 switch 语句中不可用

objective-c - 如何使用 objective-c 分割或切碎音频文件

xcode - 如何在 Swift Playground 中获取 Canvas

ios - NSDate 代码未迁移到 Swift 3.0