gpt4 book ai didi

ios - 为什么我的委托(delegate)仅在定义为 VC 而不是外部类时才起作用?

转载 作者:行者123 更新时间:2023-11-30 13:42:44 25 4
gpt4 key购买 nike

我已按照 tutorail 了解如何将 textField 限制为特定长度和字符集

这是我的代码,但未调用委托(delegate):

VC类:这个被调用(调试时验证)

import UIKit
import CoreLocation

public class InlineMainViewController: UIViewController, CLLocationManagerDelegate, UITextFieldDelegate, MainUiDelegate {

...

var nameTextVerifier = NameTextVerifier()
@IBOutlet weak var enterName_text: UITextField!
...

override public func viewDidLoad() {
enterName_text.delegate = nameTextVerifier
}

NameTextVerifier 类:

    import Foundation

public class NameTextVerifier: NSObject, UITextFieldDelegate {

public func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if string.characters.count == 0 {
return true
}
let currentText = textField.text ?? ""
let prospectiveText = (currentText as NSString).stringByReplacingCharactersInRange(range, withString: string)


return prospectiveText.containsOnlyCharactersIn("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") &&
prospectiveText.characters.count <= 6
}
}

当我输入enterName_text时,此代码永远不会被调用

另一方面,当我将相同的 shouldChangeCharactersInRange 放入 VC 类中并将 VC 设置为委托(delegate)时

func onViewDidLoad() { EnterName_text.delegate = self }

使shouldChangeCharactersInRange执行

如何检查问题所在?

最佳答案

您是否应该使用 TextManager 代替 NameTextVerifier

var nameTextVerifier = TextManager()
in place of
var nameTextVerifier = NameTextVerifier()

关于ios - 为什么我的委托(delegate)仅在定义为 VC 而不是外部类时才起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35377844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com