如何在swift中为自定义按钮操作制作globe方法

时间:2021-01-20 13:14:16

标签: ios swift xcode uibutton swift5

我有一个登录屏幕,其中两个字段是密码和电子邮件文本字段。 我用stackview添加了两个文本字段。添加文本字段后,我需要添加密码锁定按钮。单击密码锁定按钮后,用户可以看到密码。我从库中取出按钮并尝试添加文本字段,但是由于 stackview,它始终设置为 stackview 的第三个元素。所以我无法添加。 所以我决定添加文本字段的扩展名。所以我在实用程序类中成功添加了按钮, 但无法使用选择器添加操作。它显示一个错误

<块引用>

'UIViewController' 类型的值没有成员 'passwordAction'

我的添加按钮代码是

extension UITextField {

func passwordButton(vc:UIViewController){
    let paddingView = UIView(frame: CGRect(x: 25, y: 25, width: 24, height: 17))
    let passwordButton = UIButton(frame: CGRect(x: 0, y: 0, width: 24, height: 17))
    passwordButton.setImage(UIImage(named: "show_password"), for: .normal)
    passwordButton.addTarget(vc, action: #selector(vc.passwordAction), for: .touchUpInside)
    paddingView.addSubview(passwordButton)
    self.addSubview(paddingView)
}
}
class Loginviewcontroller{
   passwordTextField.passwordButton(vc: self)
 func passwordAction(){
  print("action")
 }
 }

我从登录控制器调用这个方法。

所以我有两个问题:-

  1. 当两个 textfield 附加到 stackview 时,我们不能在带有 storyboard 的 textfield 上放置按钮?
  2. 如何使用全局方法添加按钮并添加可在 uiviewcontroller 中访问的操作?

2 个答案:

答案 0 :(得分:1)

错误是不言自明的,您向 passwordAction 添加了 Loginviewcontroller 方法,但在 func passwordButton 中,您将 UIViewController 作为参数。因此,即使您将 Loginviewcontroller 的实例传递给 passwordButton 函数调用,在函数作用域中,vc 只是另一个 UIViewController 并且显然所有 UIViewController 都是如此没有名为 passwordAction 的方法。

正如您所说,您想使用全局方法来添加按钮并添加可以在 uiviewcontroller 中访问的操作,您可以按照以下方法

第 1 步:声明协议

@objc protocol SecureTextProtocol where Self: UIViewController {
    func passwordAction()
}

第 2 步:更新您的 passwordButton 方法以采用 SecureTextProtocol 而不是普通的 UIViewController

extension UITextField {

    func passwordButton(vc:SecureTextProtocol){
        let paddingView = UIView(frame: CGRect(x: 25, y: 25, width: 24, height: 17))
        let passwordButton = UIButton(frame: CGRect(x: 0, y: 0, width: 24, height: 17))
        passwordButton.setImage(UIImage(named: "show_password"), for: .normal)
        passwordButton.addTarget(vc, action: #selector(vc.passwordAction), for: .touchUpInside)
        paddingView.addSubview(passwordButton)
        self.addSubview(paddingView)
    }
}

第 3 步:让您的 ViewController 想要获得按钮操作以确认 SecureTextProtocol 协议

class Loginviewcontroller: UIViewController, SecureTextProtocol {
    passwordTextField.passwordButton(vc: self)

    func passwordAction(){
        print("action")
    }
}

那应该可以了

答案 1 :(得分:0)

你总是可以用简单的代码解决这个问题,我就是这样做的。

    extension UITextField {
func setPasswordToggleImage(_ button: UIButton) {
    if(isSecureTextEntry){
        button.setImage(UIImage(named: "ic_password_visible"), for: .normal)
    }else{
        button.setImage(UIImage(named: "ic_password_invisible"), for: .normal)

    }
}

func enablePasswordToggle(){
    let button = UIButton(type: .custom)
    setPasswordToggleImage(button)
    button.imageEdgeInsets = UIEdgeInsets(top: 0, left: -16, bottom: 0, right: 0)
    button.frame = CGRect(x: CGFloat(self.frame.size.width - 25), y: CGFloat(5), width: CGFloat(25), height: CGFloat(25))
    button.addTarget(self, action: #selector(self.togglePasswordView), for: .touchUpInside)
    self.rightView = button
    self.rightViewMode = .always
}
@objc func togglePasswordView(_ sender: Any) {
    self.isSecureTextEntry = !self.isSecureTextEntry
    setPasswordToggleImage(sender as! UIButton)
}
}

然后在 viewcontroller 的 viewdidload 上调用故事板上的函数

override func viewDidLoad() {
             super.viewDidLoad()
             txtPassword.enablePasswordToggle()
             txtConfirmPassword.enablePasswordToggle()
         }