UISegmentedControl.selectedSegmentIndex
产生错误我尝试使用title属性而不是切换,但它没有帮助,出现了同样的错误。
当我评论整个函数并尝试打印numberOfColumnsSelector.selectedSegmentIndex时,它不会崩溃并打印出正确答案。
控制台输出:"致命错误:在打开可选值时意外发现nil"
我在下面评论了导致该错误的行:
import UIKit
class ViewController: UIViewController {
//Declaring variables
@IBOutlet weak var numberOfColumnsSelector: UISegmentedControl!
@IBOutlet weak var populatedPlacesSelector: UITextField!
var numberOfColumns: Int = 0
var populatedPlaces: [Int] = []
// This makes those previously declared variables accessible
func makeVariablesAccessible () -> ( size: Int, popPlacesArray: [Int] ) {
// THE LINE BELOW IS THE PROBLEMATIC ONE
switch numberOfColumnsSelector.selectedSegmentIndex {
case 0:
numberOfColumns = 2
case 1:
numberOfColumns = 3
case 2:
numberOfColumns = 4
case 3:
numberOfColumns = 5
case 4:
numberOfColumns = 6
case 6:
numberOfColumns = 7
case 8:
numberOfColumns = 8
default:
numberOfColumns = 2
}
if populatedPlacesSelector.text != "" {
var counter = 0
var popPlaceString = ""
for c in populatedPlacesSelector.text!.characters {
if c == "." {
populatedPlaces.append(Int(popPlaceString)!)
print(populatedPlaces)
print(popPlaceString)
popPlaceString = ""
counter++
}
else {
popPlaceString.append(c)
}
}
}
return ( numberOfColumns, populatedPlaces )
}
}
答案 0 :(得分:0)
我发现问题在于我要求另一个ViewController上的值,让它们无法访问,在我修复它之后,它起作用了,谢谢所有帮助过我的人。