初学者 swift-user 在这里。我想从主视图 Controller 打开一个连接到单独 View Controller (P2_Gift_Pop_Up) 的弹出窗口。为此,我在主视图 Controller 的代码片段中包含以下内容
let vc = P2_Gift_Pop_Up()
vc.modalPresentationStyle = .overCurrentContext
present(vc, animated: true, completion: nil)
这开始在弹出窗口中运行代码(打印语句无论如何都有效),到目前为止一切顺利。
但是,当我尝试修改连接到 View Controller 的 View 中的某些元素时,我得到一个
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value. Here is the code in its entirety.
import UIKit
class P2_Gift_Pop_Up: UIViewController {
@IBOutlet weak var Slot1: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
Slot1.setImage(UIImage(named: "Card 2 Red"), for: .normal)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
虽然我(通过本网站上的另一个答案)了解消息的含义,但我不明白为什么我会在这种情况下得到它,以及如何解决它。还可能需要强调的是,虽然代码在 P2_Gift_Pop_Up 调用之后开始运行,但相应的 View 并未显示。
最佳答案
您需要像这样展示您的 Controller (您需要设置您的 Storyboard ID,然后将其添加到标识符中):
let storyBoard = UIStoryboard(name: "main", bundle: Bundle.main)
let vc = storyBoard.instantiateViewController(withIdentifier: "your storyboard id") as! P2_Gift_Pop_Up
vc.modalPresentationStyle = .overCurrentContext
present(vc, animated: true, completion: nil)
你的应用程序崩溃是因为你的按钮(Slot1)没有内存所以你需要像上面那样呈现。
关于ios - Swift:无法修改弹出窗口中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50541531/