当我通过方法present(viewController(), animated: true, completion: nil)
呈现一个包含选择器的视图控制器时,会抛出错误unexpectedly found nil while unwrapping an Optional value
。有人能告诉我如何解决这个问题吗?
回溯:
* thread #1: tid = 0x848b34, 0x000000010eb3bc50 libswiftCore.dylib`function signature specialization <preserving fragile attribute, Arg[2] = Dead, Arg[3] = Dead> of Swift._fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt, flags : Swift.UInt32) -> Swift.Never + 96, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
frame #0: 0x000000010eb3bc50 libswiftCore.dylib`function signature specialization <preserving fragile attribute, Arg[2] = Dead, Arg[3] = Dead> of Swift._fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt, flags : Swift.UInt32) -> Swift.Never + 96
* frame #1: 0x000000010c19c231 Exercise Generator`createNew.viewDidLoad(self=0x00007f9d12d69b30) -> () + 177 at createNew.swift:43
frame #2: 0x000000010c19c722 Exercise Generator`@objc createNew.viewDidLoad() -> () + 34 at createNew.swift:0
frame #3: 0x000000010d32ea3d UIKit`-[UIViewController loadViewIfRequired] + 1258
frame #4: 0x000000010d32ee70 UIKit`-[UIViewController view] + 27
frame #5: 0x000000010dbef6a4 UIKit`-[_UIFullscreenPresentationController _setPresentedViewController:] + 87
frame #6: 0x000000010d309702 UIKit`-[UIPresentationController initWithPresentedViewController:presentingViewController:] + 141
frame #7: 0x000000010d341e97 UIKit`-[UIViewController _presentViewController:withAnimationController:completion:] + 3956
frame #8: 0x000000010d34526b UIKit`-[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 530
frame #9: 0x000000010d344d51 UIKit`-[UIViewController presentViewController:animated:completion:] + 179
frame #10: 0x000000010c1b18fa Exercise Generator`openRecent.myaction(sender=0x00007f9d12c45f50, self=0x00007f9d12c451e0) -> () + 1706 at openRecent.swift:57
frame #11: 0x000000010c1b1afa Exercise Generator`@objc openRecent.myaction(sender : UIButton!) -> () + 58 at openRecent.swift:0
frame #12: 0x000000010d18e8bc UIKit`-[UIApplication sendAction:to:from:forEvent:] + 83
frame #13: 0x000000010d314c38 UIKit`-[UIControl sendAction:to:forEvent:] + 67
frame #14: 0x000000010d314f51 UIKit`-[UIControl _sendActionsForEvents:withEvent:] + 444
frame #15: 0x000000010d313e4d UIKit`-[UIControl touchesEnded:withEvent:] + 668
frame #16: 0x000000010d6be304 UIKit`_UIGestureEnvironmentSortAndSendDelayedTouches + 5645
frame #17: 0x000000010d6b8fcb UIKit`_UIGestureEnvironmentUpdate + 1472
frame #18: 0x000000010d6b89c3 UIKit`-[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 521
frame #19: 0x000000010d6b7ba6 UIKit`-[UIGestureEnvironment _updateGesturesForEvent:window:] + 286
frame #20: 0x000000010d1fdc1d UIKit`-[UIWindow sendEvent:] + 3989
frame #21: 0x000000010d1aa9ab UIKit`-[UIApplication sendEvent:] + 371
frame #22: 0x000000010d99772d UIKit`__dispatchPreprocessedEventFromEventQueue + 3248
frame #23: 0x000000010d990463 UIKit`__handleEventQueue + 4879
frame #24: 0x000000010cd0d761 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
frame #25: 0x000000010ccf298c CoreFoundation`__CFRunLoopDoSources0 + 556
frame #26: 0x000000010ccf1e76 CoreFoundation`__CFRunLoopRun + 918
frame #27: 0x000000010ccf1884 CoreFoundation`CFRunLoopRunSpecific + 420
frame #28: 0x00000001113f1a6f GraphicsServices`GSEventRunModal + 161
frame #29: 0x000000010d18cc68 UIKit`UIApplicationMain + 159
frame #30: 0x000000010c1af2ef Exercise Generator`main + 111 at AppDelegate.swift:12
frame #31: 0x000000011046768d libdyld.dylib`start + 1
相关守则: 现在的方法:
func myaction(sender: UIButton!) { //several buttons are created programmatically, all trigger this function
print("button pressed")
print(sender.currentTitleColor)
switch sender.tag {
case 0 :
print("First Button")
case 1 :
print("Second Button")
default:
print("button action from undefined button")
}
present(createNew(), animated: true, completion: nil)
}
目的地(createNew()
)
@IBOutlet weak var nameField: UITextField!{
didSet {
nameField.delegate = self
}
}
@IBOutlet weak var tagButton: UIButton!
@IBOutlet var PatientPicker: UIPickerView!
@IBOutlet var OrientationPicker: UIPickerView!
@IBOutlet var tagLabel: UILabel!
@IBOutlet weak var saveButton: UIButton!
var patientPickerData : [String] = [String]()
var orientationPickerData: [String] = [String]()
var exerciseName: String = "Untitled"
var orientation : String = String()
var tag: String = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//Picker info
self.PatientPicker.dataSource = self //Error happens here
self.PatientPicker.delegate = self
self.OrientationPicker.dataSource = self
self.OrientationPicker.delegate = self
patientPickerData = ["--Select Patient--", "No Patient"]
orientationPickerData = ["--Select Orientation--","Standing","Reclining"]
//End of viewDidLoad()
}
这应该是与问题相关的所有代码。如果需要的话,还有一些我可以发布的内容,但正如我之前所说,这与故事板segues有关,并且由于某些原因在以编程方式呈现时不起作用。
答案 0 :(得分:0)
这适用于storyboard segues
是的,因为当你做一个segue时,你正在使用故事板中的视图控制器,它有一个视图,并且该视图包含一个选择器视图,所有的出口都被连接起来。
但是你的createNew
创建了一个不同的视图控制器,一个视图控制器没有选择器视图或其他任何东西。所以你的网点永远不会被连接,它们是零,你崩溃了。
您需要执行segue所做的事情:从故事板中拉出视图控制器 。