Swift无法同时满足prepareForSegue中的约束错误?

问题描述:

我尝试控制器Swift无法同时满足prepareForSegue中的约束错误?

之间传递数据

第一控制器:

class CreateStatmentFieldsController: UITableViewController, DetailsDelegate { 

    @IBOutlet weak var violationLabel: UILabel! 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Uncomment the following line to preserve selection between presentations 
     // self.clearsSelectionOnViewWillAppear = false 

     // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
     // self.navigationItem.rightBarButtonItem = self.editButtonItem() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func violationLabelDelegateMethodWithString(string: String) { 
     violationLabel.text = string 
    } 

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { 
     if (segue.identifier == "violationSegue") { 
      let controller = segue.destinationViewController as CreateStatmentViolationController 
      controller.delegate = self 
     } 
    } 

第二个控制器:

protocol DetailsDelegate { 
    func violationLabelDelegateMethodWithString(string: String) 
} 

class CreateStatmentViolationController: UITableViewController { 

    var delegate: DetailsDelegate! 

    @IBOutlet weak var backButton: UIBarButtonItem! 
    let violations = ["Парковка вторым рядом", "Парковка под знаком \"остановка запрещена\"", "Парковка на тротуаре", "Парковка на пешеходном переходе", "Парковка на месте для инвалидов"] 


    override func viewDidLoad() { 
     super.viewDidLoad() 

    } 

    @IBAction func donePressed(b:UIBarButtonItem) { 
     self.dismissViewControllerAnimated(true, completion: {}) 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     tableView.deselectRowAtIndexPath(indexPath, animated: true) 

     let row = indexPath.row 
     delegate.violationLabelDelegateMethodWithString("sa")//violations[row] 

     self.dismissViewControllerAnimated(true, completion: {}) 
    } 

    // MARK: - Table view data source 
// 
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     // #warning Potentially incomplete method implementation. 
     // Return the number of sections. 
     return 1 
    } 
// 
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     // #warning Incomplete method implementation. 
     // Return the number of rows in the section. 
     return violations.count 
    } 


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCellWithIdentifier("vilolationCell", forIndexPath: indexPath) as UITableViewCell 
     let row = indexPath.row 
     cell.textLabel?.text = violations[row] 

     // Configure the cell... 

     return cell 
    } 

但后来我打开secont控制器我有错误:

Unable to simultaneously satisfy constraints. 
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7fdf9a455e70 H:[UIView:0x7fdf9a49ed70(400)]>", 
    "<NSLayoutConstraint:0x7fdf9a490c80 H:|-(0)-[UIView:0x7fdf9a49ed70] (Names: '|':UIScrollView:0x7fdf9a4958d0)>", 
    "<NSLayoutConstraint:0x7fdf9a490e00 UIScrollView:0x7fdf9a4958d0.centerX == UIView:0x7fdf9a49ed70.centerX>", 
    "<NSLayoutConstraint:0x7fdf9a490730 H:[UIScrollView:0x7fdf9a4958d0]-(0)-| (Names: '|':UIView:0x7fdf9a463bb0)>", 
    "<NSLayoutConstraint:0x7fdf9a491230 H:|-(0)-[UIScrollView:0x7fdf9a4958d0] (Names: '|':UIView:0x7fdf9a463bb0)>", 
    "<NSLayoutConstraint:0x7fdf9a4ae900 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fdf9a463bb0(375)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fdf9a455e70 H:[UIView:0x7fdf9a49ed70(400)]> 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
2015-04-07 01:45:52.768 pin[8690:51798] Unable to simultaneously satisfy constraints. 
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7fdf9a49ef70 H:[UIView:0x7fdf9a5a7250(390)]>", 
    "<NSLayoutConstraint:0x7fdf9a495c00 H:[UIView:0x7fdf9a49ed70]-(0)-| (Names: '|':UIScrollView:0x7fdf9a4958d0)>", 
    "<NSLayoutConstraint:0x7fdf9a490c80 H:|-(0)-[UIView:0x7fdf9a49ed70] (Names: '|':UIScrollView:0x7fdf9a4958d0)>", 
    "<NSLayoutConstraint:0x7fdf9a490e00 UIScrollView:0x7fdf9a4958d0.centerX == UIView:0x7fdf9a49ed70.centerX>", 
    "<NSLayoutConstraint:0x7fdf9a493b00 H:|-(5)-[UIView:0x7fdf9a5a7250] (Names: '|':UIScrollView:0x7fdf9a4958d0)>", 
    "<NSLayoutConstraint:0x7fdf9a480580 H:[UIView:0x7fdf9a5a7250]-(5)-| (Names: '|':UIScrollView:0x7fdf9a4958d0)>", 
    "<NSLayoutConstraint:0x7fdf9a490730 H:[UIScrollView:0x7fdf9a4958d0]-(0)-| (Names: '|':UIView:0x7fdf9a463bb0)>", 
    "<NSLayoutConstraint:0x7fdf9a491230 H:|-(0)-[UIScrollView:0x7fdf9a4958d0] (Names: '|':UIView:0x7fdf9a463bb0)>", 
    "<NSLayoutConstraint:0x7fdf9a4ae900 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fdf9a463bb0(375)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fdf9a49ef70 H:[UIView:0x7fdf9a5a7250(390)]> 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
2015-04-07 01:45:52.774 pin[8690:51798] Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead. 

如果我删除了“prepareForSegue”func,所有工作,第二视图ope n并且完美地接近,但我需要将数据发送给第一控制器。

这个错误似乎是非常直接的,除非它是过分简化。

因此,这些都是根据调试器的元凶 -

"<NSLayoutConstraint:0x7fdf9a455e70 H:[UIView:0x7fdf9a49ed70(400)]>", 
"<NSLayoutConstraint:0x7fdf9a490c80 H:|-(0)-[UIView:0x7fdf9a49ed70] (Names: '|':UIScrollView:0x7fdf9a4958d0)>", 

"<NSLayoutConstraint:0x7fdf9a495c00 H:[UIView:0x7fdf9a49ed70]-(0)-| (Names: '|':UIScrollView:0x7fdf9a4958d0)>", 
"<NSLayoutConstraint:0x7fdf9a490c80 H:|-(0)-[UIView:0x7fdf9a49ed70] (Names: '|':UIScrollView:0x7fdf9a4958d0)>", 

在这两种调试器打印的UIView:出现0x7fdf9a49ed70。这可能需要一些玩弄,但似乎这个视图的高度明确设置为400。然后,它被限制两次到它的超级视图的顶部,一次到超级视图的底部。

在那个打印输出下,它对这些感到沮丧。

"<NSLayoutConstraint:0x7fdf9a49ef70 H:[UIView:0x7fdf9a5a7250(390)]>", 
"<NSLayoutConstraint:0x7fdf9a493b00 H:|-(5)-[UIView:0x7fdf9a5a7250] (Names: '|':UIScrollView:0x7fdf9a4958d0)>", 
"<NSLayoutConstraint:0x7fdf9a480580 H:[UIView:0x7fdf9a5a7250]-(5)-| (Names: '|':UIScrollView:0x7fdf9a4958d0)>", 

所以再次,一个视图被限制在它的superView的顶部和底部。该观点还有一个明确的高度设定为390分。

看起来,在segue期间的某个时间点,甚至在segue之后,superView的高度并不等于390或400,因此约束条件无法满足。如果我是你,我或者不明确地设置视图的高度,只限制顶部和底部或者设置高度并将它们限制在或者顶部或者底部。所有这三个约束都是不必要的,在这种情况下不可能同时进行。