简体   繁体   English

如何在Xcode 9.2的Playground项目中使用swift从用户获取输入

[英]how to get input from user using swift in playground project in xcode 9.2

This is giving an error on ios11, any idea!? 这给ios11一个错误,知道吗!?

From topic:how to get input from user using swift in playground project in xcode 8.2 来自主题:如何在Xcode 8.2的Playground项目中使用Swift获得用户输入

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

You cannot use readline() in a swift playground. 您不能在快速的操场上使用readline() For this you have to use a command line tool. 为此,您必须使用命令行工具。

Also your code only showed black ;) .. I modified it, so you can see a textfield: 另外,您的代码仅显示黑色;)..我对其进行了修改,因此您可以看到一个文本字段:

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

A good and more complete example can be found here: https://www.ralfebert.de/ios-examples/uikit/uicatalog-playground/UITextField/ 一个很好且更完整的示例可以在这里找到: https : //www.ralfebert.de/ios-examples/uikit/uicatalog-playground/UITextField/

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

相关问题 如何在 Xcode 8.2 的操场项目中使用 Swift 从用户那里获取输入? - How to get input from user using Swift in playground project in Xcode 8.2? 如何在 Swift Xcode playgroud 中获取用户的输入 - How to get input from the user in swift Xcode playgroud Xcode 9.2被困在编译Swift 3.2项目中 - Xcode 9.2 stuck in compiling Swift 3.2 Project 我们可以将xcode项目(xcodeproj)转换为Swift Playground(.playground)格式吗? - Can we convert xcode project (xcodeproj) to swift playground (.playground) format? 如何在使用cocoapods时将项目代码暴露给Xcode游乐场? - How to expose your project code to a Xcode playground when using cocoapods? 如何在 Xcode 9.2 中获得新的 iOS SDK? - How to get new iOS SDK in Xcode 9.2? Xcode Project和Playground关于Swift错误处理的区别 - Difference between Xcode Project and Playground about Swift error handling 将 SpriteKit 场景添加到 Xcode 上的 Swift Playground App 项目? - Adding SpriteKit scene to Swift Playground App project on Xcode? 为什么我的Swift代码在操场上可以工作,而在Xcode项目中却不能工作? - Why does my Swift code work in the playground but not in the Xcode project? MapKit(Swift 4)Xcode 9.2 - '无法插入第4角的法律归属' - MapKit (Swift 4) Xcode 9.2 - 'Could not inset legal attribution from corner 4'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM