标签不会将文本复制为键入文字-尝试了多种解决方案(MacOS应用) [英] Label won't copy Text as typing - tried many solutions (MacOS app)

查看:81
本文介绍了标签不会将文本复制为键入文字-尝试了多种解决方案(MacOS应用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编码新手,即将拔头发.我已经设置了一个拆分视图(macOS应用程序),我只是想在用户输入时在左侧获得一个标签,以更新为在右侧输入的文本.

I'm new to coding and about to tear my hair out. I have a split view set up (macOS app) and I'm just trying to get a label on the left hand side to update to text entered on the right as the user is typing.

我已经为这个问题苦苦挣扎了15个多小时.我已经进行了广泛的搜索并尝试了许多解决方案(尤其是在 Swift3中找到的解决方案:在用户输入时实时更新UiLabel ),以下是所有成功构建的尝试,但没有尝试在输入文本时更改标签的情况.

I’ve been struggling for around 15+ hours with this problem. I’ve searched extensively and tried many solutions (notably those found at Swift3: Live UiLabel update on user input), below are all the attempts that have build successfully but none change the label when entering text.

我实际上是在尝试在左侧创建类似标题的视图,其中仅列出标题",并且在选择后将用户带到右侧文本中的那个点(这个想法是用诸如星号的标题表示标题-因此它只显示以星号开头的文本)-但我知道其范围比一个问题要大得多.

任何帮助将不胜感激.

这只是一个带有拆分视图控制器,一个文本字段和一个标签的普通Xcode文件.

It's just a normal Xcode file with a split view controller, one text field and one label.

注意:代码中未包含尝试"标题,并且分别进行了测试

    class ViewController: NSViewController {

    @IBOutlet weak var leftLabel: NSTextField!
    @IBOutlet weak var rightText: NSTextField!

        override func viewDidLoad() {
            super.viewDidLoad()


...


// Attempt 1
  func attempt(_ sender: NSTextField) {
            leftLabel.stringValue = rightText.stringValue
        }

// Attempt 2   
if let textInputting = rightText {
            //There is a text in your textfield so we get it and put it on the label
            leftLabel.stringValue = textInputting.stringValue
        } else {
            //textfield is nil so we can't put it on the label
            print("textfield is nil")
    }

// Attempt 3
        func testfunc (textField: NSTextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

            leftLabel.stringValue = rightText.stringValue
            return true
        }

// Attempt 4
        func testfunc2 (textField: NSTextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

            rightText2.stringValue = rightText.stringValue
            return true
        }

// Attempt 5
         leftLabel?.stringValue = rightText?.stringValue ?? ""

// Attempt 6
        func copying (_ rightText: NSTextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
            leftLabel.stringValue = rightText.stringValue
            return true
        }

// Attempt 7
 func textField(_ textField: NSTextFieldDelegate?, shouldChangeCharactersIn range: NSRange, replacementString string: NSText) -> Bool {
                leftLabel.stringValue = rightText.stringValue
                return true
        }

推荐答案

实际上,您一直试图做的并不那么简单.以前,我没有阅读整个问题.对此我感到抱歉.

Actually, what you have been trying to do is not that simple. Previously, I did not read the entire question. I'm sorry about that.

以下是使用NSNotification的一种方法,其中应用程序将从一个名为FirstViewController的视图控制器向另一个名为SecondViewController的视图控制器发送文本字符串.在下面,HomeViewController是NSSplitViewController的子类,在这种情况下不参与任何操作.

The following is one approach using NSNotification where the application will send a string of text from one view controller named FirstViewController to another named SecondViewController. In the following, HomeViewController is a subclass of NSSplitViewController which takes no part in this case.

// HomeViewController //
import Cocoa

class HomeViewController: NSSplitViewController {
    // MARK: - Variables

    // MARK: - IBOutlet

    // MARK: - IBAction

    // MARK: - Life cycle
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }
}

// FirstViewController //
import Cocoa

class FirstViewController: NSViewController, NSTextFieldDelegate {
    // MARK: - Variables

    // MARK: - IBOutlet
    @IBOutlet weak var textField: NSTextField!

    // MARK: - IBAction

    // MARK: - Life cycle
    override func viewDidLoad() {
        super.viewDidLoad()

        textField.delegate = self
    }

    func controlTextDidChange(_ notification: Notification) {
        if notification.object as? NSTextField == textField {
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "SecondViewControllerTextDidChange"), object: textField.stringValue)
        }
    }
}

// SecondViewController //
import Cocoa

class SecondViewController: NSViewController {
    // MARK: - Variables

    // MARK: - IBOutlet
    @IBOutlet weak var labelField: NSTextField!

    // MARK: - IBAction

    // MARK: - Life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear() {
        super.viewWillAppear()

        NotificationCenter.default.addObserver(self, selector: #selector(passText), name: NSNotification.Name(rawValue: "SecondViewControllerTextDidChange"), object: nil)
    }

    override func viewWillDisappear() {
        super.viewWillDisappear()
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "SecondViewControllerTextDidChange"), object: nil)
    }

    @objc func passText(notification: NSNotification) {
        let text = notification.object as! String
        labelField.stringValue = text
    }
}

总而言之,应用程序将使用NSNotification将字符串从FirstViewController传递给SecondViewController. SecondViewController将通过其viewWillAppear发布通知.当应用程序离开SecondViewController时,必须删除此通知.重要的是要注意,您需要通过Storyboard选择每个视图控制器,并在Identity inspector下设置类名称.

In summary, the application will pass the string from FirstViewController to SecondViewController with NSNotification. SecondViewController will post a notification through its viewWillAppear. When the application leaves SecondViewController, this notification must be removed. It is important to note that you need to select each view controller through Storyboard and set the class name under the Identity inspector.

这篇关于标签不会将文本复制为键入文字-尝试了多种解决方案(MacOS应用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆