[点击文本框右键时如何显示xib视图

问题描述 投票:2回答:1

我想在点击文本框右键时显示​​一个弹出视图,下面的代码,但在点击文本框右键时不显示视图。,我使用“ CustomeView”类名称创建了xib视图,如下所示,尝试加载到警报视图中

  class ViewController: UIViewController, UITextFieldDelegate {

      let textFieldRightButton = UIButton(type: .infoLight)
    var textFieldOne:UITextField? = nil
    var alertview:UIView? = nil

    override func viewWillAppear(_ animated: Bool) {
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.2, execute: {

            self.showAlertViewWithTextField(title: "Test", msg: "Test Custome View", okActionTitle: "continue", cancelActionTitle: "Cancel", success: {
                print("Continue")
            }) {
                print("Cancel")
            }

        })
    }

    func showAlertViewWithTextField(title: String, msg: String, okActionTitle:String, cancelActionTitle:String, success: (() -> Void)? , cancel: (() -> Void)?)
    {
        let alert = UIAlertController(title: title, message: msg, preferredStyle: UIAlertController.Style.alert)
        alertview = alert.view

        textFieldRightButton.tintColor = UIColor.red

        alert.addTextField
            {
                (textField) in
                textField.placeholder = "Enter text."
                textField.delegate = self
                textField.rightViewMode = .always
                textField.rightView = self.textFieldRightButton

        }

        textFieldOne = alert.textFields?.first

         textFieldRightButton.addTarget(self, action: #selector(showPopupMsg), for: .touchUpInside)

        let successAction: UIAlertAction = UIAlertAction(title: okActionTitle, style: .destructive)
        {
            action -> Void in
            success?()
        }
        let cancelAction: UIAlertAction = UIAlertAction(title: cancelActionTitle, style: .cancel)
        {
            action -> Void in
            cancel?()
        }

        alert.addAction(successAction)
        alert.addAction(cancelAction)
        UIApplication.shared.keyWindow?.rootViewController?.present(alert, animated: true, completion: nil)
    }

    @objc func showPopupMsg() {
        alertview?.addSubview(CustomeView(frame: CGRect(x: textFieldOne!.frame.minX, y: textFieldOne!.frame.minX, width: 100, height: 50)))
    }

}

以下是我的自定义视图

  class CustomeView: UIView {
            @IBOutlet var contentView: UIView!
            @IBOutlet weak var messageText: UILabel!

            override init(frame: CGRect) {
                super.init(frame: frame)
                commonInit()
            }

            required init?(coder aDecoder: NSCoder) {
             super.init(coder: aDecoder)
                commonInit()
            }

            private func commonInit(){
                Bundle.main.loadNibNamed("CustomeView", owner: self, options: nil)
                addSubview(contentView)
                contentView.frame = self.bounds
                contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
           }

        }

如何在视图控制器中显示此笔尖视图?

ios swift uitextfield xib nib
1个回答
0
投票

我尝试使用您的代码段,但访问错误。尝试删除该xib文件的自定义类(CustomeView),然后在该xib文件的CustomeView中设置File's Owner视图类。还要检查您的messageTextcontentView插座对象类型应为File's Owner

Outlet

File Owner

© www.soinside.com 2019 - 2024. All rights reserved.