致命错误:意外发现零...只在不在模拟器中的设备上

时间:2016-04-14 10:36:27

标签: ios iphone swift

我创建了一个按钮,一旦按下该按钮就会替换另一个按钮。代码在模拟器中工作正常。但不在设备上。一旦我按下doneButton,我就会得到以下错误

  

致命错误:在解包可选值时意外发现nil

所以didPushEditButton函数可以工作,但不是didPushDoneButton函数......而且只出现在不在模拟器中的iPhone上。 xcode 7.3 iOS 9.3 Swift 2.2

错误出现在行

之后
  

self.editButton = UIButton(type:UIButtonType.Custom)

这是我的代码:

@IBAction func didPushEditButton(sender: UIButton)
{
    self.tableView.setEditing(true, animated: true)
    self.editButton.removeFromSuperview()
    self.lowerNavView.viewWithTag(18)?.removeFromSuperview()

    self.doneButton = UIButton(type: UIButtonType.Custom)
    self.doneButton.addTarget(self, action: #selector(MainViewController.didPushDoneButton), forControlEvents: UIControlEvents.AllTouchEvents)
    self.doneButton.setTitle("done", forState: UIControlState.Normal)
    self.doneButton.titleLabel!.font = UIFont.systemFontOfSize(15)
    self.doneButton.frame = CGRectMake(8, 10, 74, 30)
    self.doneButton.setTitleColor(UIColor(red: 0.5, green: 0.0, blue: 0.5, alpha: 1.0), forState: UIControlState.Normal)
    self.doneButton.tag = 17
    print(self.doneButton.tag)
    self.lowerNavView.addSubview(self.doneButton)
}

func didPushDoneButton()
{
    self.tableView.setEditing(false, animated: true)
    self.lowerNavView.viewWithTag(17)?.removeFromSuperview()
    self.editButton = UIButton(type: UIButtonType.Custom)

    self.editButton.addTarget(self, action: #selector(MainViewController.didPushEditButton(_:)), forControlEvents: UIControlEvents.AllTouchEvents)
    self.editButton.setTitle("edit", forState: UIControlState.Normal)
    self.editButton.titleLabel?.font = UIFont.systemFontOfSize(15)
    self.editButton.frame = CGRectMake(8, 10, 92, 30)
    self.editButton.setTitleColor(UIColor(red: 0, green: 122.0/255.0, blue: 1.0, alpha: 1.0), forState: UIControlState.Normal)
    self.editButton.tag = 18
    self.lowerNavView.addSubview(self.editButton)
    }

0 个答案:

没有答案