首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Xcode9.2中的playground项目中使用swift获取用户输入

如何在Xcode9.2中的playground项目中使用swift获取用户输入
EN

Stack Overflow用户
提问于 2018-02-28 18:59:34
回答 1查看 577关注 0票数 0

这在ios11上给出了一个错误,你知道吗!?

来自主题:如何在Xcode8.2中的playground项目中使用swift获取用户输入

代码语言:javascript
复制
import UIKit
import PlaygroundSupport 
    // new code user input

class V: UIViewController {
    var textField = UITextField(frame: CGRect(x: 20, y: 20, width: 200,      height: 24))
    override func viewDidLoad() {
        super.viewDidLoad()
        //view.addSubview(textField)
        textField.backgroundColor = .white
        textField.delegate = self
    }
}
extension V: UITextFieldDelegate {
    func textField(_ textField: UITextField, shouldChangeCharactersIn         range: NSRange, replacementString string: String) -> Bool {
    // Do stuff here
     print("Please enter your name")
     var name = readLine()
    print("name: \(name!)")
    return true
    }
}
 let v = V()
v.view.frame = CGRect(x: 0, y: 0, width: 300, height: 300)
PlaygroundPage.current.liveView = v.view
PlaygroundPage.current.needsIndefiniteExecution = true
EN

回答 1

Stack Overflow用户

发布于 2018-02-28 21:03:54

您不能在快速操场上使用readline()。为此,您必须使用命令行工具。

此外,您的代码仅显示为黑色;) ..我对它进行了修改,这样您就可以看到一个文本字段:

代码语言:javascript
复制
import UIKit
import PlaygroundSupport
// new code user input

class V: UIViewController {
    var textField: UITextField!
    override func loadView() {
        //super.viewDidLoad()
        //view.addSubview(textField)

        let view = UIView()
        view.backgroundColor = .white

        textField = UITextField()
        textField.backgroundColor = .white
        textField.delegate = self
        textField.borderStyle = .roundedRect

        view.addSubview(textField)

        textField.text = "Hello world!"

        // Layout

        textField.translatesAutoresizingMaskIntoConstraints = false
        let margins = view.layoutMarginsGuide
        NSLayoutConstraint.activate([
            textField.topAnchor.constraint(equalTo: margins.topAnchor, constant: 20),
            textField.leadingAnchor.constraint(equalTo: margins.leadingAnchor),
            textField.trailingAnchor.constraint(equalTo: margins.trailingAnchor),
        ])

        self.view = view
    }
}
extension V: UITextFieldDelegate {
    func textField(_ textField: UITextField, shouldChangeCharactersIn         range: NSRange, replacementString string: String) -> Bool {
        // Do stuff here
        print("Please enter your name")
        var name = readLine()
        print("name: \(name!)")
        return true
    }
}
let v = V()
//v.view.frame = CGRect(x: 0, y: 0, width: 300, height: 300)
PlaygroundPage.current.liveView = v
//PlaygroundPage.current.needsIndefiniteExecution = true

可以在这里找到一个更好、更完整的示例:https://www.ralfebert.de/ios-examples/uikit/uicatalog-playground/UITextField/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49027958

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档