Segue在macOS中不起作用 [英] Segue doesn't work in macOS

查看:115
本文介绍了Segue在macOS中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ViewController中有一个NSSlider,它必须通过类型为Show的segue将整数值传递给SecondViewController,并在每次移动视图时更新它.因此,移动滑块可以在SecondViewController的窗口中以交互方式随机播放图像.

I have NSSlider in ViewController that must pass integer value to SecondViewController via segue of type Show and update a View every time I move it. So, moving slider I interactively shuffle images in SecondViewController's window.

我花了一个星期的时间来尝试实现NSSlider的这种行为,但失败了.也许我在Interface Builder中建立了错误的连接.我不知道.如果您知道使NSSlider用于混排图像的任何其他方法,请告诉我.

I spent a week trying to implement such a behaviour of NSSlider but I failed. Maybe I made wrong connections in Interface Builder. I don't know. If you know any other method to make NSSlider work for shuffling images, tell me, please.

有关详细信息,请参阅更新的答案.

See updated answer for detailed information.

真的很感谢您的帮助!

ViewController

import Cocoa

class ViewController: NSViewController {

    @IBOutlet weak var slider: NSSlider!

    @IBAction func passData(_ sender: NSSlider) {        
        func prepare(for segue: NSStoryboardSegue, sender: NSSlider?) {
            if segue.identifier!.rawValue == "SegueIdentifierForSecondVC" {
                if let secondViewController = segue.destinationController as? SecondViewController {
                    secondViewController.imagesQuantity = slider.intValue 
                }
            }
        }
    }
}

SecondViewController

import Cocoa

class SecondViewController: NSViewController {

    var imagesQuantity: Int = 1

    override func viewWillAppear() {
        super.viewWillAppear()

        print(imagesQuantity)
    }
}

但是它不起作用.我的代码有什么问题?

But it doesn't work. What's wrong in my code?

任何帮助表示赞赏.

更新后的答案

视图控制器

import Cocoa

extension NSStoryboardSegue.Identifier {
    static let secondVC = NSStoryboardSegue.Identifier("SegueIdentifierForSecondVC")
}

class ViewController: NSViewController {

    @IBOutlet weak var slider: NSSlider!

    @IBAction func segueData(_ sender: NSSlider) {
        self.performSegue(withIdentifier: .secondVC, sender: slider)
    }
    override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
        if segue.identifier! == .secondVC {
            if let secondViewController =
                segue.destinationController as? SecondViewController {
                secondViewController?.imagesQty = slider.integerValue 
            }
        }
    }
}

第二视图控制器

import Cocoa

class SecondViewController: NSViewController {

    var imagesQty = 30  

    override func viewWillAppear() {
        super.viewWillAppear()
        //let arrayOfViews: [NSImageView] = [view01...view12]

        let url = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Desktop/ArrayOfElements")

        do {
            let fileURLs = try FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: [.skipsHiddenFiles]).reversed()
            let photos = fileURLs.filter { $0.pathExtension == "jpg" }

            for view in arrayOfViews {
                //"imagesQty" is here
                let i = Int(arc4random_uniform(UInt32(imagesQty-1)))
                let image = NSImage(data: try Data(contentsOf: photos[i]))
                view.image = image
            }
        } catch {
            print(error)
        }
    }
}

推荐答案

该错误不在代码中.

您必须将segue连接到 ViewController ,而不是WindowController.

You have to connect the segue to the ViewController, not to the WindowController.

将滑块动作连接到IBAction(而不是segue),并将segue从ViewControllerSecondViewController(不是从滑块).

Connect the slider action to the IBAction (not to the segue) and the segue from ViewController to SecondViewController (not from the slider).

如果第二个视图控制器的类是SecondViewController,则应在窗口控制器中指出. SecondVC来自哪里?

And if the class of the second view controller is SecondViewController it should be indicated in the window controller. Where does SecondVC come from?

再次建议创建NSStoryboardSegue.Identifier

extension NSStoryboardSegue.Identifier {
    static let secondVC = NSStoryboardSegue.Identifier("SegueIdentifierForSecondVC")
}

并在这两种方法中使用它.并强制施放segue.destinationController.如果一切都正确连接,它一定不会崩溃.

and to use it in these two methods. And force cast the segue.destinationController. It must not crash if everything is hooked up properly.

@IBAction func segueData(_ sender: NSSlider) {
    self.performSegue(withIdentifier: .secondVC, sender: nil)
}

override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
    if let identifier = segue.identifier, identifier == .secondVC {
        let secondViewController = segue.destinationController as! SecondViewController
        secondViewController.imagesQty = slider.integerValue
    }
}  

最后NSImage得到了自己的带有URL的初始化程序

Finally NSImage got its own initializer taking an URL

let image = NSImage(contentsOf: photos[i])

这篇关于Segue在macOS中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆