简体   繁体   English

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

[英]Swift Unable to simultaneously satisfy constraints error in prepareForSegue?

I try pass data between controllers 我尝试在控制器之间传递数据

First controller: 第一控制器:

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
        }
    }

Second Controller: 第二控制器:

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
    }

but then i open secont controller i have bug: 但是然后我打开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.

If i remove "prepareForSegue" func, all worked, second view open and close perfectly, but i need send data to filrst controller. 如果我删除“ prepareForSegue”功能,则所有工作正常,第二个视图可以完美打开和关闭,但是我需要将数据发送到filrst控制器。

The error seems to be pretty straight forward unless it is an oversimplification. 除非过于简化,否则错误似乎很简单。

So these are the culprits according to the debugger – 因此,这是调试器的罪魁祸首–

"<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 )>",

In both debugger printouts UIView:0x7fdf9a49ed70 appears. 在两个调试器打印输出中,都会出现UIView:0x7fdf9a49ed70。 It might take some playing around with, but it seems like this view's height is set to 400 explicitly. 可能要花一些时间,但似乎此视图的高度已明确设置为400。 It is then constrained twice to the top of its superview and once to the bottom of the superview. 然后将其两次约束到其超级视图的顶部,一次约束到其超级视图的底部。

Under that printout it is upset about these. 在该打印输出下,对这些感到不安。

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

So again a view is constrained to the top and bottom of its superView. 因此,视图再次被限制在其superView的顶部和底部。 That view also has an explicit height set to 390 points. 该视图的显式高度也设置为390点。

It looks like, at some point during the segue and maybe even after the segue, the superView's height does not equal 390 or 400 respectively and for that reason the constraints cannot be satisfied. 看起来,在segue期间的某个时候甚至在segue之后,superView的高度分别不等于390或400,因此无法满足约束。 If I were you, I would either not explicitly set the views' height and only constrain top and bottom or set the height and constrain them to either the top or the bottom. 如果我是你,我要么没有明确设定的若干意见的高度,只有约束顶部和底部或设置高度和约束他们的顶部底部。 All three constraints aren't necessary and in this case aren't simultaneously possible. 这三个约束不是必需的,在这种情况下,不可能同时实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM