gpt4 book ai didi

ios - 如何在 UIAlertController 中使用 UITextView

转载 作者:可可西里 更新时间:2023-11-01 03:36:40 26 4
gpt4 key购买 nike

我使用警报 Controller 创建了一个弹出警报,并添加了两个警报操作(确定和取消),如下所示。

UIAlertController * alert=   [UIAlertController
alertControllerWithTitle:@"Cycling"
message:@"Please enter title and description"
preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* ok = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];

}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];

}];

[alert addAction:ok];
[alert addAction:cancel];

[self presentViewController:alert animated:YES completion:nil];

现在,我想添加 UITextView。因为我有两个文本字段,如标题和描述。对于描述,我想使用 UITextView 添加 no.of 行。我试过我不知道如何添加它。

请指教。

最佳答案

Johno2110 的解决方案经过一些清理和调整后对我来说效果最好。在这里发布代码和结果的屏幕截图,以防它帮助其他人。

确认使用 Swift 5。

let textView = UITextView(frame: CGRect.zero)

@IBAction func sendFeedback(_ sender: Any) {
let alertController = UIAlertController(title: "Feedback \n\n\n\n\n", message: nil, preferredStyle: .alert)

let cancelAction = UIAlertAction.init(title: "Cancel", style: .default) { (action) in
alertController.view.removeObserver(self, forKeyPath: "bounds")
}
alertController.addAction(cancelAction)

let saveAction = UIAlertAction(title: "Submit", style: .default) { (action) in
let enteredText = self.textView.text
alertController.view.removeObserver(self, forKeyPath: "bounds")
}
alertController.addAction(saveAction)

alertController.view.addObserver(self, forKeyPath: "bounds", options: NSKeyValueObservingOptions.new, context: nil)
textView.backgroundColor = UIColor.white
textView.textContainerInset = UIEdgeInsets.init(top: 8, left: 5, bottom: 8, right: 5)
alertController.view.addSubview(self.textView)

self.present(alertController, animated: true, completion: nil)
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "bounds"{
if let rect = (change?[NSKeyValueChangeKey.newKey] as? NSValue)?.cgRectValue {
let margin: CGFloat = 8
let xPos = rect.origin.x + margin
let yPos = rect.origin.y + 54
let width = rect.width - 2 * margin
let height: CGFloat = 90

textView.frame = CGRect.init(x: xPos, y: yPos, width: width, height: height)
}
}
}

alert

关于ios - 如何在 UIAlertController 中使用 UITextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28603060/

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