簡體   English   中英

未執行Segue iOS8

[英]Segue is not performed iOS8

我正在使用針對iOS 8的XCode 6,並在Swift中對此進行編碼。

我的情節提要控制器如下所示:

Tab Bar > Navigation > Table View > Detail View

Show Detail序列是從Table CellDetail View

單擊表格單元格時不會觸發prepareForSegue方法。 從按鈕到細節的Segue效果很好。 didSelectRowAtIndexPath performSegueWithIdentifier也可以正常工作。

我還創建了一個全新的測試項目來測試此問題-控制器代碼如下所示:

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

知道為什么它不能立即使用嗎?

PS:當使用“ Split View而不是“ Tab Bar ,相同的順序可以正常工作。

當我使用tableView.register(AnyClass?, forCellReuseIdentifier: String)在ViewDidLoad方法中注冊單元格時,這發生在我tableView.register(AnyClass?, forCellReuseIdentifier: String)當我對此行添加注釋並在情節tableView.register(AnyClass?, forCellReuseIdentifier: String)板中添加了單元格類和單元格標識符時,它開始起作用。 再次,如果我取消注釋viewdidload中的代碼,它將停止工作。

我只是用一個示例項目對其進行了測試。 工作正常。 連接電池一定有問題。

這是我的代碼。 僅指示從普通香草標簽欄項目模板進行的更改。

在此處輸入圖片說明

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

要調用方法prepareForSegue,必須添加一個segue以連接原型單元和目標Controller! 如果以編程方式導航到目標控制器,則不會調用該方法!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM