gpt4 book ai didi

ios - 如何定位线路?意外发现为零

转载 作者:行者123 更新时间:2023-12-01 23:20:22 25 4
gpt4 key购买 nike

当我尝试运行我的应用程序时,收到错误消息:

fatal error: unexpectedly found nil while unwrapping an Optional value.

有人可以告诉我是否可以找到问题所在的行吗?不幸的是,我没有看到模拟器崩溃的红线。

我粘贴了所有代码,但问题一定与警报功能有关,因为它工作正常,直到我尝试实现它。

import UIKit

var list = ["Visa code: 1234", "Mastercard code: 4321"]

class notesVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var messageLabel: UILabel!

var userMessage = "Sample text"
var theUserText: UITextField?

@IBOutlet weak var tabelView: UITableView!
@IBAction func addItemButton(_ sender: Any) {
let alertController = UIAlertController(title:"title",
message: "message",
preferredStyle: .alert)

alertController.addTextField(configurationHandler: theUserTextFunc)
let okAction = UIAlertAction(title: "OK",
style: .default,
handler: self.okHandler)

let cancleAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(okAction)
alertController.addAction(cancleAction)
self.present(alertController, animated: true)
}

func theUserTextFunc(textField: UITextField){
theUserText = textField
}

func okHandler(alert: UIAlertAction!){
list.append((theUserText?.text)!)
}

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return (list.count)
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")

cell.textLabel?.text = list[indexPath.row]
return(cell)
}


// Swipe to delete an item
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete{
list.remove(at: indexPath.row)
tabelView.reloadData()
}
}

override func viewDidLoad() {
super.viewDidLoad()
messageLabel.text = userMessage
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

func customInit(userMessage: String) {
self.userMessage = userMessage
}
}

最佳答案

我尝试了你的代码。它工作完美,只是您错过了在 OkHandler 上的表上重新加载。

所以我怀疑问题出在您的 IBOutlet 或 IBAction 连接上。检查一下...

import UIKit

var list = ["Visa code: 1234", "Mastercard code: 4321"]


class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{

@IBOutlet weak var messageLabel: UILabel!


var userMessage = "Sample text"
var theUserText: UITextField?


@IBOutlet weak var tabelView: UITableView!

@IBAction func addItemButton(_ sender: Any) {
let alertController = UIAlertController(title:"title",
message: "message",
preferredStyle: .alert)

alertController.addTextField(configurationHandler: theUserTextFunc)
let okAction = UIAlertAction(title: "OK",
style: .default,
handler: self.okHandler)

let cancleAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(okAction)
alertController.addAction(cancleAction)
self.present(alertController, animated: true)

}
func theUserTextFunc(textField: UITextField){
theUserText = textField

}
func okHandler(alert: UIAlertAction!){

list.append((theUserText?.text)!)
self.tabelView.reloadData()
}


public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return (list.count)
}



public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")

cell.textLabel?.text = list[indexPath.row]

return(cell)
}


// Swipe to delete an item
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

if editingStyle == UITableViewCellEditingStyle.delete{
list.remove(at: indexPath.row)
tabelView.reloadData()




}
}


override func viewDidLoad() {
super.viewDidLoad()
messageLabel.text = userMessage


}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()

}
func customInit(userMessage: String) {
self.userMessage = userMessage
}

}

关于ios - 如何定位线路?意外发现为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45468285/

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