Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.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
Ios Swift游乐场:让ViewController调用presentViewController_Ios_Xcode_Xcode7_Uialertcontroller_Swift Playground - Fatal编程技术网

Ios Swift游乐场:让ViewController调用presentViewController

Ios Swift游乐场:让ViewController调用presentViewController,ios,xcode,xcode7,uialertcontroller,swift-playground,Ios,Xcode,Xcode7,Uialertcontroller,Swift Playground,我想和你在操场上玩。是否可以从XCPlaygroundPage获取ViewController-以便能够调用presentViewController?是的,您当然可以获取视图控制器,但由于没有“附加”视图控制器,您最终会看到如下警告: 不建议在分离的视图控制器上显示视图控制器 在我的例子中,警报控制器在屏幕上短暂闪烁,然后消失。也许你可以在这方面有所改进 无论如何,下面是我如何在操场中创建视图控制器的: import UIKit import XCPlayground class RootV

我想和你在操场上玩。是否可以从XCPlaygroundPage获取ViewController-以便能够调用presentViewController?

是的,您当然可以获取视图控制器,但由于没有“附加”视图控制器,您最终会看到如下警告:

不建议在分离的视图控制器上显示视图控制器

在我的例子中,警报控制器在屏幕上短暂闪烁,然后消失。也许你可以在这方面有所改进

无论如何,下面是我如何在操场中创建视图控制器的:

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

我在这里做的是创建一个“根视图控制器”,添加一个带有表的子视图控制器(试图将某个视图控制器“连接”到其他对象),然后我尝试在该视图控制器上显示一个警报控制器

您可以在演示后将liveView设置为您的窗口,以避免出现警告

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
结果: