我是 iOS 初学者。
我有一个问题,我创建了一个名为 ModalViewController
的类。为了使它成为一个普通类,我没有给这个类中的 textView 任何文本。所以我在呈现这个 viewController 时尝试更改文本。
但是我收到一个错误。我该如何解决?
此外,如何在 TextView 中更改 margin
值?
主 Storyboard
MainViewController.swift
import UIKit
var modalview : ModalViewController!
class ViewController: UIViewController {
...
@IBAction func Onclick(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myAlert = storyboard.instantiateViewController(withIdentifier: "ModalViewController")
myAlert.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
myAlert.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
modalview.modalCustomAlert.text = "test test test text" // Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
self.present(myAlert, animated: true, completion: nil)
}
ModalViewController.swift
import Foundation
import UIKit
class ModalViewController : UIViewController {
@IBOutlet weak var modalCustomAlert: UITextView!
@IBOutlet weak var cancelButton: UIButton!
@IBOutlet weak var okButton: UIButton!
@IBAction func cancelPress(_ sender: Any) {
}
@IBAction func okPress(_ sender: Any) {
}
override func viewDidLoad() {
super.viewDidLoad()
modalCustomAlert.layer.cornerRadius = 8
}
@IBAction func modalbutton(_ sender: Any) {
}
}
编辑开始
我尝试赋值但是报错没有变化
var modalview = ModalViewController()
...
@IBAction func Onclick(_ sender: Any) {
...
modalview.modalCustomAlert.text = "test test test text" // Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
}
编辑了两个开始
我已经试过了。但是当我看到@PGDev的回答时,我又试了一遍,还是报同样的错误。
@IBAction func Onclick(_ sender: Any) {
...
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myAlert = storyboard.instantiateViewController(withIdentifier: "ModalViewController") as! ModalViewController
myAlert.modalPresentationStyle = .overCurrentContext
myAlert.modalTransitionStyle = .crossDissolve
myAlert.modalCustomAlert.text = "test test test text" // Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
self.present(myAlert, animated: true, completion: nil)
}
最佳答案
fatal error :在隐式展开可选值时意外发现 nil
modalview.modalCustomAlert.text = "test test test text"
异常是因为 modalview
为 nil
或 modalCustomAlert
为 nil
。
1. 您已将 modalview
创建为隐式展开可选,例如,
var modalview : ModalViewController!
在访问 modalview
之前,请检查您是否为它分配了任何值。如果没有,这将导致崩溃。
2. 接下来,您已经创建了 modalCustomAlert
的 outlet
,
@IBOutlet weak var modalCustomAlert: UITextView!
这也是一个隐式展开的可选。检查 outlet
是否连接正确。如果不是,它将引发运行时异常。
If an implicitly unwrapped optional is nil and you try to access its wrapped value, you’ll trigger a runtime error. The result is exactly the same as if you place an exclamation mark after a normal optional that doesn’t contain a value.
编辑-1:
var modalview = ModalViewController()
以上代码不会连接 ModalViewController
中的 outlets
。这就是 modalCustomAlert
为 nil
并导致异常的原因。
使用 Storyboard创建 modalview
,
let modalview = storyboard.instantiateViewController(withIdentifier: "ModalViewController")
但有一个问题。如果 modalview
和 myAlert
都是 ModalViewController
类型,为什么要分别创建它们?
编辑 2:
我认为甚至不需要创建 modalview
。只需在出现异常的行使用 myAlert
View 。所以 ViewController
的代码是这样的,
class ViewController: UIViewController {
//....
@IBAction func Onclick(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myAlert = storyboard.instantiateViewController(withIdentifier: "ModalViewController") as! ModalViewController
myAlert.modalPresentationStyle = .overCurrentContext
myAlert.modalTransitionStyle = .crossDissolve
myAlert.modalCustomAlert.text = "test test test text"
self.present(myAlert, animated: true, completion: nil)
}
}
编辑 3:
明白了。只是没有意识到您在创建导出之前正在访问 modalCustomAlert。
首先,在 ModalViewController
中创建一个属性 text
。并在 viewDidLoad()
中将此 text
添加为 modalCustomAlert.text
,即
class ModalViewController : UIViewController {
//rest of the code....
var text: String? //here....
override func viewDidLoad() {
super.viewDidLoad()
modalCustomAlert.layer.cornerRadius = 8
self.modalCustomAlert.text = text //here....
}
}
现在,在创建alertView
时,替换
myAlert.modalCustomAlert.text = "test test test text"
与
myAlert.text = "test test test text"
关于ios - 模态视图 Controller 中的文本不会在 View Controller 中更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57781882/