简体   繁体   English

未执行Segue iOS8

[英]Segue is not performed iOS8

I am using XCode 6, targeting iOS 8 and coding this in Swift. 我正在使用针对iOS 8的XCode 6,并在Swift中对此进行编码。

My storyboard controllers look like this: 我的情节提要控制器如下所示:

Tab Bar > Navigation > Table View > Detail View

The Show Detail segue is from a Table Cell to the Detail View . Show Detail序列是从Table CellDetail View

The prepareForSegue method is not triggered when clicking a table cell. 单击表格单元格时不会触发prepareForSegue方法。 Segue from a button to detail works fine, though. 从按钮到细节的Segue效果很好。 performSegueWithIdentifier in didSelectRowAtIndexPath also works fine. didSelectRowAtIndexPath performSegueWithIdentifier也可以正常工作。

I have also created a brand new test project to test this issue - the controller code looks like this: 我还创建了一个全新的测试项目来测试此问题-控制器代码如下所示:

import UIKit

class TestTableController: UITableViewController
{
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return 2;
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cellId = "testCell";
        var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellId) as? UITableViewCell;

        if (cell == nil)
        {
            cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellId);
        }

        cell!.textLabel!.text = "cell \(indexPath.item)";

        return cell!;
    }
}

Any idea why this does not work out of the box? 知道为什么它不能立即使用吗?

PS: when using Split View instead of a Tab Bar , the same segue works fine. PS:当使用“ Split View而不是“ Tab Bar ,相同的顺序可以正常工作。

This happened with me when I was registering cell in ViewDidLoad method using tableView.register(AnyClass?, forCellReuseIdentifier: String) When i commented this line and added the cell class and cell identifier in storyboard, it started working. 当我使用tableView.register(AnyClass?, forCellReuseIdentifier: String)在ViewDidLoad方法中注册单元格时,这发生在我tableView.register(AnyClass?, forCellReuseIdentifier: String)当我对此行添加注释并在情节tableView.register(AnyClass?, forCellReuseIdentifier: String)板中添加了单元格类和单元格标识符时,它开始起作用。 Again if I uncomment that code in viewdidload, it stops working. 再次,如果我取消注释viewdidload中的代码,它将停止工作。

I just tested it with a sample project. 我只是用一个示例项目对其进行了测试。 It works fine. 工作正常。 There must be something wrong with your wiring up the cell. 连接电池一定有问题。

Here is my code. 这是我的代码。 Only indicating changes from plain vanilla tab bar project template. 仅指示从普通香草标签栏项目模板进行的更改。

在此处输入图片说明

// FirstViewController.swift
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 2
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
    cell.textLabel?.text = "Cell \(indexPath.row + 1)"
    return cell
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let detail = segue.destinationViewController as DetailController
    let cell = sender as UITableViewCell
    let indexPath = self.tableView.indexPathForCell(cell)
    detail.detailItem = "Cell \(indexPath!.row + 1)"
}

//DetailController.swift
class DetailController: UIViewController {

    var detailItem : String!
    @IBOutlet var label : UILabel!

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        label.text = detailItem
    }
}

for calling the method prepareForSegue you must have to add a segue to connect the prototype cell and the destination Controller! 要调用方法prepareForSegue,必须添加一个segue以连接原型单元和目标Controller! method doesn't called if you navigate to destination controller programmatically! 如果以编程方式导航到目标控制器,则不会调用该方法!

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

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