Swift: 无法修改弹出窗口中的图像
Swift: Cannot modify images in popup
初学者 swift - 此处为用户。我想从主视图控制器打开一个连接到单独视图控制器 (P2_Gift_Pop_Up) 的弹出窗口。为此,我在主视图控制器的代码片段中包含以下内容
let vc = P2_Gift_Pop_Up()
vc.modalPresentationStyle = .overCurrentContext
present(vc, animated: true, completion: nil)
这将在弹出窗口 window 中启动 运行 代码(无论如何打印语句都有效),到目前为止一切顺利。
但是,当我尝试修改连接到视图控制器的视图中的某些元素时,我得到一个
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 调用之后开始 运行,但未显示相应的视图。
您需要像这样展示您的控制器(您需要设置您的故事板 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)没有内存所以你需要像上面那样呈现。
@JogendarChoudhary 告诉你该怎么做,但没有真正解释原因。
当您使用
创建 P2_Gift_Pop_Up
时
let vc = P2_Gift_Pop_Up()
您没有正确初始化它。它没有机会从它的 XIB file/storyboard.
加载它的视图
假设您在主应用故事板中定义了视图控制器,您需要从故事板加载视图控制器。
您应该在情节提要中为视图控制器添加一个唯一标识符,然后使用该标识符加载它。 (使用 class 的名称作为标识符是一个不错的选择。)
UIViewController
class 有一个 属性 storyboard
,它将包含从中加载它的故事板。通常这是您应用程序的主要故事板,也是您想要的。因此:
if let vc = storyboard?.instantiateViewController(withIdentifier: "P2_Gift_Pop_Up id")
as? P2_Gift_Pop_Up {
vc.modalPresentationStyle = .overCurrentContext
present(vc, animated: true, completion: nil)
} else {
print("error creating P2_Gift_Pop_Up")
}
初学者 swift - 此处为用户。我想从主视图控制器打开一个连接到单独视图控制器 (P2_Gift_Pop_Up) 的弹出窗口。为此,我在主视图控制器的代码片段中包含以下内容
let vc = P2_Gift_Pop_Up()
vc.modalPresentationStyle = .overCurrentContext
present(vc, animated: true, completion: nil)
这将在弹出窗口 window 中启动 运行 代码(无论如何打印语句都有效),到目前为止一切顺利。
但是,当我尝试修改连接到视图控制器的视图中的某些元素时,我得到一个
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 调用之后开始 运行,但未显示相应的视图。
您需要像这样展示您的控制器(您需要设置您的故事板 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)没有内存所以你需要像上面那样呈现。
@JogendarChoudhary 告诉你该怎么做,但没有真正解释原因。
当您使用
创建P2_Gift_Pop_Up
时
let vc = P2_Gift_Pop_Up()
您没有正确初始化它。它没有机会从它的 XIB file/storyboard.
加载它的视图假设您在主应用故事板中定义了视图控制器,您需要从故事板加载视图控制器。
您应该在情节提要中为视图控制器添加一个唯一标识符,然后使用该标识符加载它。 (使用 class 的名称作为标识符是一个不错的选择。)
UIViewController
class 有一个 属性 storyboard
,它将包含从中加载它的故事板。通常这是您应用程序的主要故事板,也是您想要的。因此:
if let vc = storyboard?.instantiateViewController(withIdentifier: "P2_Gift_Pop_Up id")
as? P2_Gift_Pop_Up {
vc.modalPresentationStyle = .overCurrentContext
present(vc, animated: true, completion: nil)
} else {
print("error creating P2_Gift_Pop_Up")
}