简体   繁体   English

Swift playground:获取ViewController来调用presentViewController

[英]Swift playground: get ViewController to call presentViewController

I would like to play with UIAlertController in Playground. 我想在Playground玩UIAlertController。 Is it possible to get the ViewController from XCPlaygroundPage - to be able to call presentViewController? 是否有可能从XCPlaygroundPage获取ViewController - 能够调用presentViewController?

You can set the liveView to your Window after presenting, to avoid the warning. 您可以呈现设置实时查看到你的窗口,以避免该警告。

import UIKit
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true
let window = UIWindow()
let viewController = UIViewController()
window.rootViewController = viewController
window.makeKeyAndVisible()

let alert = UIAlertController(title: "title here", message: "message here", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "action here", style: .default, handler: nil))
viewController.present(alert, animated: true, completion: nil)
PlaygroundPage.current.liveView = window

Result: 结果: 时间轴上的UIAlertController

Yes, you can certainly get a view controller, but because there's no "attached" view controller you'll end up seeing a warning that looks like this: 是的,你当然可以获得一个视图控制器,但因为没有“附加”视图控制器,你最终会看到如下警告:

Presenting view controllers on detached view controllers is discouraged

And in my case, the alert controller flash on screen super briefly and then disappeared. 在我的情况下,警报控制器在屏幕上超短暂闪烁,然后消失。 Perhaps you could improve upon this somewhat? 也许你可以稍微提高一点吗?

Anyways, here is how I created the view controllers in the playground: 无论如何,这是我在操场上创建视图控制器的方式:

import UIKit
import XCPlayground

class RootViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.frame = CGRect(x: 0, y: 0, width: 350, height: 420)
        self.view.backgroundColor = UIColor.redColor()
    }
}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var tableView: UITableView!
    let items = ["Hello 1", "Hello 2", "Hello 3"]
    var alertController : UIAlertController? {
        didSet {
            if alertController == nil {
                print("alertController set to nil")
            } else {
                print("alertController set to \(alertController)")
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
        self.tableView = UITableView(frame:self.view.frame)
        self.tableView!.dataSource = self
        self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
        self.view.addSubview(self.tableView)

    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return self.items.count;
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
        cell.textLabel?.text = "\(self.items[indexPath.row])"
        return cell
    }

    override func viewDidAppear(animated: Bool) {
        alertController = UIAlertController.init(title: "My Title", message: "Testing in Playground", preferredStyle: .Alert)

        let okAction = UIAlertAction(title: "Ok", style: .Default) { (_) -> Void in
            // Some action here
            print("hit okay button")
        }

        alertController!.addAction(okAction)

        ctrl.presentViewController(alertController!, animated: false, completion: {
            print("done presenting")
        })
    }
}

var ctrl = ViewController()
var rootVC = RootViewController()
rootVC.addChildViewController(ctrl)
rootVC.view.addSubview(ctrl.view)
XCPlaygroundPage.currentPage.liveView = rootVC.view

And what I'm doing here is I create a "root view controller", add a child view controller with a table (in an attempt to get some view controller "attached" to something else) and then I tried to present an alert controller on that. 而我在这里做的是创建一个“根视图控制器”,添加一个带有表的子视图控制器(试图让一些视图控制器“附加”到其他东西)然后我试图呈现一个警报控制器在那。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过调用 presentViewController 和 UITransitionView 更改 ViewController - Change ViewController by call presentViewController and UITransitionView 在Swift中的presentViewController之后调用pushViewController - Call pushViewController after presentViewController in Swift iOS如何在已堆叠的ViewController上调用presentViewController - iOS how to call presentViewController upon already stacked ViewController Swift操场错误:调用中额外的参数“超时” - Swift playground error: extra argument 'timeout' in call SWIFT报告“通话中的额外参数”-在Playground中工作 - SWIFT reporting “Extra argument in call” - works in Playground 来自 ViewController 的 Swift 调用计时器 - Swift call timer from ViewController Swift:在另一个ViewController中调用一个函数 - Swift: Call a function in another ViewController Swift:在委托方法中使用presentViewController时,“尝试呈现其视图不在窗口层次结构中的ViewController” - Swift: “Attempt to present ViewController whose view is not in the window hierarchy” when using presentViewController inside a delegate method 如何让 UIButton 在 Swift Playground 中执行操作 - How to get a UIButton to perform action in a Swift Playground C#PresentViewController到storyboard中的viewcontroller - C# PresentViewController to a viewcontroller in storyboard
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM