Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何通过代码打开弹出窗口_Swift_Popup - Fatal编程技术网

Swift 如何通过代码打开弹出窗口

Swift 如何通过代码打开弹出窗口,swift,popup,Swift,Popup,我是一个非常新手的swift程序员,正在尝试做以下工作:我有一个简单的游戏,通过按下某些按钮,通过单独的视图控制器生成弹出窗口。我还想添加一些在某些情况下打开弹出窗口的事件之后运行的代码。为此,我创建了一个新的视图和视图控制器,并将它们链接起来。整个视图控制器如下所示: import UIKit class P2_Gift_Pop_Up: UIViewController { override func viewDidLoad() { super.viewDidLoa

我是一个非常新手的swift程序员,正在尝试做以下工作:我有一个简单的游戏,通过按下某些按钮,通过单独的视图控制器生成弹出窗口。我还想添加一些在某些情况下打开弹出窗口的事件之后运行的代码。为此,我创建了一个新的视图和视图控制器,并将它们链接起来。整个视图控制器如下所示:

import UIKit

class P2_Gift_Pop_Up: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        print("I was here")

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }   
}
现在,我尝试调用此视图控制器,通过在主视图控制器的代码中使用以下行来弹出视图:

P2\u礼物\u弹出

斯威夫特接受这一点,但当我运行应用程序时,什么也没有发生。我做错了什么

class MainViewController: UIViewController {

    let button = UIButton()

    override func viewDidLoad() {
        super.viewDidLoad()

        // setup button frame or constraint.. (unless done in IB)

        // add target to button (or add an IBOutlet from IB)

        button.addTarget(self, action: #selector(buttonClicked(_ :)), for: .touchUpInside)
    }

    @objc func buttonClicked(_ sender: UIButton) {
        let vc = P2_Gift_Pop_Up()
        vc.modalPresentationStyle = .overCurrentContext
        present(vc, animated: true, completion: nil)
    }
}
在P2_礼物_弹出窗口VC中:


在viewController中调用P2_Gift_Pop_不会显示它,在您的情况下,模态显示效果很好,因为您希望P2_Gift_Pop_弹出

因为我不想使用按钮,所以我只在主视图控制器中使用了以下代码段

let vc = P2_Gift_Pop_Up()
vc.modalPresentationStyle = .overCurrentContext 
present(vc, animated: true, completion: nil)
这将在弹出窗口中开始运行代码,不带任何按钮,即我想要的

但是,当我运行以下代码时

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()

}

}
我得到以下错误:线程1:致命错误:在展开可选值时意外发现nil

怎么了?我用主视图控制器中的按钮打开的弹出窗口中的代码也是一样


Ps.抱歉,如果将此作为我自己问题的答案发布是错误的,那么当我包含代码时,此评论变得不可读,编辑原始问题将改变我最初想要知道的内容,并且我确实回答了我最初想要知道的内容

调用P2_Gift_弹出窗口不会显示viewController。将代码添加到MainViewController,以便我们可以帮助您MainViewController中的其余代码运行良好,但与我在此处尝试执行的操作无关。或者我误解了你?P2_Gift_Pop____________________________________________?P2\u Gift\u弹出窗口只创建视图控制器的一个新实例,它不显示或显示它。我在identity spector中将视图的类设置为视图控制器。但你的评论让我明白了一些事情。谢谢。是的,模态演示会很好。但我不想通过按下按钮来实现这一点。你的回答是否强调了这一点?或者它只是模拟一个按钮?如果是这样的话,这是获得想要的效果所必需的。你可以在任何地方调用present和disclose,如果这是你的意思的话,不必是一个按钮。我只是选择了一个按钮,因为它是最常见的,也更容易在示例中显示。你必须将其作为一个新问题添加,没有人可以评论你的答案,因为正如你所说,它变得不可读
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()

}

}