如果我使用多个 "if statements" 然后在 swift 中遇到错误(由于未捕获的异常而终止应用程序)
If i use multiple "if statements" then hitting error (Terminating app due to uncaught exception) in swift
我正在使用 PopoverView
在表格视图单元格按钮中显示选项,例如 this screen shot
这里如果我这样使用没有错误..但是如果用if statements
检查条件
然后崩溃和错误
@objc func showOptions(sender:UIButton) {
popoverView?.show(with: ["View", "View Proposal", "Delete"], sender: sender, showDirection: .up)
}
但是我需要根据以下条件为每个单元格显示不同的选项
单元格按钮操作代码:这里打开,关闭,进行中..都是分段模式
@objc func showOptions(sender:UIButton) {
if mode == .Open{
popoverView?.show(with: ["View", "View Proposal", "Delete"], sender: sender, showDirection: .up)
}
if (mode == .Open) && (bidsCount > 0){
popoverView?.show(with: ["View", "View Proposal", "Edit", "Delete"], sender: sender, showDirection: .up)
}
if mode == .In_progress{
popoverView?.show(with: ["View", "View Proposal", "Delete"], sender: sender, showDirection: .up)
}
if mode == .Awarded{
popoverView?.show(with: ["View", "Delete"], sender: sender, showDirection: .up)
}
if mode == .Closed{
popoverView?.show(with: ["View"], sender: sender, showDirection: .up)
}
}
PopoverView 代码:
extension PageContentViewController: PopoverViewDelegate {
func value(didSelect item: String, at index: Int, indexPath: IndexPath, sender: UIView) {
if item == "View" {
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "VC1") as? VC1
self.navigationController?.pushViewController(vc!, animated: true)
}
if item == "View Proposal" {
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "VC2") as? VC2
self.navigationController?.pushViewController(vc!, animated: true)
}
if item == "Post Review" {
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "VC3") as? VC3
self.navigationController?.pushViewController(vc!, animated: true)
}
}
错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally a view controller <UITableViewController: 0x7fdb37092420> that is already being presented by <TestApp.SideMenuController: 0x7fdb36659370>.'
if mode == .Open{
popoverView?.show(with: ["View", "View Proposal", "Delete"], sender: sender, showDirection: .up)
}
if (mode == .Open) && (bidsCount > 0){
popoverView?.show(with: ["View", "View Proposal", "Edit", "Delete"], sender: sender, showDirection: .up)
}
这会显示一个弹出窗口,然后显示第二个弹出窗口,这是不允许的。任何时候 (mode == .Open) && (bidsCount > 0)
为真,mode == .Open
也为真。
您可能打算重新排序并使用 else if
而不仅仅是 if
。
然而,一般来说,这将通过 switch
语句完成,例如:
switch mode {
case .Open where bidsCount > 0:
...
case .Open:
...
case .In_progress:
...
...
}
(请注意,在 Swift 中,枚举大小写通常是驼峰式大小写。因此这些通常是 .open
和 .inProgress
。)
我正在使用 PopoverView
在表格视图单元格按钮中显示选项,例如 this screen shot
这里如果我这样使用没有错误..但是如果用if statements
检查条件
然后崩溃和错误
@objc func showOptions(sender:UIButton) {
popoverView?.show(with: ["View", "View Proposal", "Delete"], sender: sender, showDirection: .up)
}
但是我需要根据以下条件为每个单元格显示不同的选项
单元格按钮操作代码:这里打开,关闭,进行中..都是分段模式
@objc func showOptions(sender:UIButton) {
if mode == .Open{
popoverView?.show(with: ["View", "View Proposal", "Delete"], sender: sender, showDirection: .up)
}
if (mode == .Open) && (bidsCount > 0){
popoverView?.show(with: ["View", "View Proposal", "Edit", "Delete"], sender: sender, showDirection: .up)
}
if mode == .In_progress{
popoverView?.show(with: ["View", "View Proposal", "Delete"], sender: sender, showDirection: .up)
}
if mode == .Awarded{
popoverView?.show(with: ["View", "Delete"], sender: sender, showDirection: .up)
}
if mode == .Closed{
popoverView?.show(with: ["View"], sender: sender, showDirection: .up)
}
}
PopoverView 代码:
extension PageContentViewController: PopoverViewDelegate {
func value(didSelect item: String, at index: Int, indexPath: IndexPath, sender: UIView) {
if item == "View" {
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "VC1") as? VC1
self.navigationController?.pushViewController(vc!, animated: true)
}
if item == "View Proposal" {
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "VC2") as? VC2
self.navigationController?.pushViewController(vc!, animated: true)
}
if item == "Post Review" {
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "VC3") as? VC3
self.navigationController?.pushViewController(vc!, animated: true)
}
}
错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally a view controller <UITableViewController: 0x7fdb37092420> that is already being presented by <TestApp.SideMenuController: 0x7fdb36659370>.'
if mode == .Open{
popoverView?.show(with: ["View", "View Proposal", "Delete"], sender: sender, showDirection: .up)
}
if (mode == .Open) && (bidsCount > 0){
popoverView?.show(with: ["View", "View Proposal", "Edit", "Delete"], sender: sender, showDirection: .up)
}
这会显示一个弹出窗口,然后显示第二个弹出窗口,这是不允许的。任何时候 (mode == .Open) && (bidsCount > 0)
为真,mode == .Open
也为真。
您可能打算重新排序并使用 else if
而不仅仅是 if
。
然而,一般来说,这将通过 switch
语句完成,例如:
switch mode {
case .Open where bidsCount > 0:
...
case .Open:
...
case .In_progress:
...
...
}
(请注意,在 Swift 中,枚举大小写通常是驼峰式大小写。因此这些通常是 .open
和 .inProgress
。)