gpt4 book ai didi

ios - 点击文本字段右键时如何显示 xib View

转载 作者:行者123 更新时间:2023-12-01 18:28:02 25 4
gpt4 key购买 nike

我想在点击文本字段右键时显示​​弹出 View ,我尝试过 下面的代码,但点击文本字段右侧按钮时 View 未显示。,我创建了带有“CustomeView”类名的 xib View ,如下所示,尝试在警报 View 中加载

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

}

下面是我的自定义 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]
}

}

如何在 View Controller 中显示此 Nib View ?

最佳答案

我尝试使用您的代码片段,但出现了错误的访问错误。尝试删除该 xib 文件的自定义类 (CustomeView),并在该 xib 文件的 File's Owner 中设置 CustomeView View 类。另请检查您的 messageTextcontentView 导出对象类型应为File's Owner

Outlet

File Owner

关于ios - 点击文本字段右键时如何显示 xib View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59543395/

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