簡體   English   中英

如何在Xcode 9.2的Playground項目中使用swift從用戶獲取輸入

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

這給ios11一個錯誤,知道嗎!?

來自主題:如何在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

您不能在快速的操場上使用readline() 為此,您必須使用命令行工具。

另外,您的代碼僅顯示黑色;)..我對其進行了修改,因此您可以看到一個文本字段:

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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM