我有一个计算器类,第一个用于插入值的ViewController和第二个用于显示计算结果的ViewController.不幸的是,如果我点击按钮,我会收到一个名为"无法解开Optional.None"的错误.我知道语法有问题,但我不知道如何改进它.
第一个Viewcontroller中的按钮在故事板中设置为"Segue:Show(例如Push)",如果他被轻击,则切换到secondViewController.
计算器类是这样的:
class Calculator: NSObject {
func calculate (a:Int,b:Int) -> (Int) {
var result = a * b
return (result)
}
}
Run Code Online (Sandbox Code Playgroud)
Viewcontroller调用该函数,插入一个/ b并想要更改位于secondviewcontroller中的标签:
class ViewController: UIViewController {
@IBAction func myButtonPressed(sender : AnyObject) {
showResult()
}
var numberOne = 4
var numberTwo = 7
var myCalc = Calculator()
func showResult () {
var myResult = myCalc.calculate(numberOne, b: numberTwo)
println("myResult is \(String(myResult))")
var myVC = secondViewController()
myVC.setResultLabel(myResult)
}
Run Code Online (Sandbox Code Playgroud)
这是secondViewController的代码
class secondViewController: UIViewController {
@IBOutlet var myResultLabel : UILabel = nil …
Run Code Online (Sandbox Code Playgroud) 有一个MenuViewController类,其中记录了表中记录的数组:
import Foundation
import UIKit
class MenuViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var menuTableView: UITableView!
let myTitle = ["??????", "??????????", "?????????"]
override func viewDidLoad() {
super.viewDidLoad()
menuTableView.delegate = self
menuTableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myTitle.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = menuTableView.dequeueReusableCell(withIdentifier: "MenuCell") as! MenuTableViewCell
cell.labelText.text = myTitle[indexPath.row]
return cell
}
}
Run Code Online (Sandbox Code Playgroud)
你需要在里面写什么才能进入信息控制器?