我NSSlider
中的ViewController
必须通过类型为SecondViewController
的segue将整数值传递给Show
,并在每次移动时更新一个视图。因此,移动滑块I以交互方式在SecondViewController
窗口中随机播放图像。
我花了一周时间尝试实现NSSlider
的这种行为,但我失败了。也许我在Interface Builder中建立了错误的连接。我不知道。如果你知道让NSSlider
改变图像的任何其他方法,请告诉我。
有关详细信息,请参阅更新的答案。
非常感谢任何帮助!!!
的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)
}
}
但它不起作用。我的代码有什么问题?
任何帮助表示感谢。
更新的答案
查看控制器
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)
}
}
}
答案 0 :(得分:2)
错误不在代码中。
您必须将segue连接到 ViewController
,而不是WindowController
。
将滑块操作连接到IBAction
(不是segue),将segue从ViewController
连接到SecondViewController
(而不是滑块)。
如果第二个视图控制器的类是SecondViewController
,则应在窗口控制器中指示。 SecondVC
来自哪里?
再一次建议创建NSStoryboardSegue.Identifier
extension NSStoryboardSegue.Identifier {
static let secondVC = NSStoryboardSegue.Identifier("SegueIdentifierForSecondVC")
}
并在这两种方法中使用它。强迫施放segue.destinationController
。如果一切都正常连接,它一定不会崩溃。
@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
获得了自己的初始值设定程序
let image = NSImage(contentsOf: photos[i])
答案 1 :(得分:1)
@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
}
}
}
}
应该是
@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
}
}
}
因为你在另一个方法中有一个方法,而它们应该是一个接一个。
现在,您需要修改passData(_sender:)
。你需要在里面打performSegue(withIdentifier:sender)
如果将在某个时刻prepare(for:sender:)
触发。我在"某些地方说#34;因为在调用shouldPerformSegue(withIdentifier:sender:)
之前至少有prepare(for:sender:)
次调用。
现在这样做:
@IBAction func passData(_ sender: NSSlider) {
self.performSegue(withIdentifier: "SegueIdentifierForSecondVC" sender:nil)
}