ios - Swift:无法修改弹出窗口中的图像

标签 ios swift xcode popup uikit

初学者 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/

相关文章:

iphone - 如何在 Xcode for iPhone 中制作多页面应用程序?

ios - 如何在我从上一个屏幕选择的日期执行 +1 或 -1?

ios - 有什么方法可以在没有安装 Cocoa-pods 的情况下使用 Pod 文件运行项目?

ios - ARKit 世界地图——如何使用自定义 UTI 文件类型保存/访问?

ios - 没有调用 cvGet2D 的匹配函数

ios - 未调用编程式 UIButton 操作

ios - Swift 中的 Double to String 错误

ios - 获取 Xcode 5 以警告新的 API 调用

macos - OSX Swift webview 加载本地文件

ios - 无法为 arm (iOS) 交叉编译 C 库