简体   繁体   English

点击文本字段右键时如何显示xib视图

[英]how to show xib view when tap on textfield right button

I want to show a popup view when tap on textfield right button, I tried below code but view is not showing when tap on textfield right button.我想在点击文本字段右侧按钮时显示一个弹出视图,我尝试了下面的代码,但是当点击文本字段右侧按钮时没有显示视图。 , I created xib view with "CustomeView" class name as below and I am , trying to load in alert view ,我使用“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)))
    }

}

below is my custom view下面是我的自定义视图

  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]
           }

        }

How can I show this nib view in view controller?如何在视图控制器中显示此笔尖视图?

I tried with your code snippet and I got bad access error.我尝试使用您的代码片段,但出现错误访问错误。 Try removing the custom class (CustomeView) of that xib file and set the CustomeView view class in File's Owner of that xib file.尝试删除该 xib 文件的自定义类 (CustomeView),并在该CustomeView File's Owner中设置CustomeView视图类。 Also check your messageText and contentView outlet object type should be the File's Owner .还要检查您的messageTextcontentView插座对象类型应该是File's Owner

出口

文件所有者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于MKAnnotationView按钮的自定义视图Xib - Custom View Xib for MKAnnotationView button tap 当我点击没有导航的按钮时,如何在 xib(ViewController) 中打开 xib(ViewController) - How can I open xib(ViewController) in xib(ViewController) when I tap Button without Navigation iOS自定义.xib视图按钮点击在视图控制器中检测(Swift) - iOS Custom .xib view button tap detect in view controller (Swift) 如何为按钮设置动画以使其在点击时向右显示视图并向后弹跳 - How to animate a button to show a view to right and bounce to back when it is tapped 在文本字段点击上显示datepicker - Show datepicker on textfield tap 按下文本字段清除按钮时如何显示我的按钮? - How to show my button when textfield clear button was pressed? 点击单元格时如何显示tableview单元格编辑视图? - How to show tableview cell edit view when tap the cell? 在 swift 中使用键盘 IQKeyboardManagerSwift 点击完成按钮时如何获取放置在 tableview 中的文本字段的索引路径 - How to get Indexpath of textfield which placed in tableview when tap on done button using keyboard IQKeyboardManagerSwift in swift 点击“保存”按钮并存储在字典中时,在tableview单元格中获取文本字段? - get textfield in tableview cell when tap Save button and store in dictionary? 如何在视图中将按钮添加到文本字段 - How to Add Button to Textfield in view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM