簡體   English   中英

Xcode Swift返回上一個viewController(TableViewController)

[英]Xcode Swift Go back to previous viewController (TableViewController)

在我的TableviewController中,我在右上角有一個酒吧按鈕項。 單擊按鈕后,將顯示一個新的視圖控制器。 在此控制器中,我將一些數據輸入到文本字段中並保存。 要保存此數據,我必須單擊一個按鈕,該按鈕也是右上角的條形按鈕項。

但是現在它將我委派給我的應用程序的第一個視圖控制器。 我該怎么做,將我委派回表視圖控制器?

編輯:

當我使用以下代碼時,應用程序給我一個EXC_Breakpoint: (code=IEXC_386..)錯誤EXC_Breakpoint: (code=IEXC_386..) ,它進入視圖控制器TeamOverviewController

var vc = self.storyboard?.instantiateViewControllerWithIdentifier("TeamOverviewController") as ViewController
self.navigationController?.popToViewController(vc, animated: true)

看來該解決方案可以使用標准的“解開序列”。 關於此問題有很多文章,但我總結了一種希望與您的問題相似的方法。

  1. 我將TableViewController與右上角的“下一步”的條形按鈕一起使用。 單擊“下一步”時,將顯示一個ViewController,您可以在其中放置任何輸入控件。
  2. ViewController在右上角還有一個“保存”按鈕。
  3. 兩個控制器均嵌入NavigationControllers中,以方便導航
  4. 單擊“保存”按鈕后,將在保存需要保存的所有數據后返回到TableViewController。
  5. 在TableViewController中,您需要添加一個IBAction函數,我稱之為unwindToThisViewController,這是您在ViewController中觸摸“保存”按鈕時被定向到的地方
  6. 在ViewController中,您需要按住Ctrl並將其從“保存”按鈕拖動到“退出”圖標(ViewController故事板中的第三個圖標)。 當您執行此操作時,您將獲得一個名稱為unwindToThisViewController的下拉選項,作為您應該選擇的演示文稿 現在,您的ViewController已連接到展開搜索過程。
  7. 在ViewController中,請勿為“保存”按鈕放置IBActon。 而是將保存命令放在函數prepareForSegue中。

這是兩個控制器的代碼:

    //
//  TableViewController.swift

import UIKit

class TableViewController: UITableViewController {

    // add this function. When the detail ViewController is unwound, it executes this 
    // function

    @IBAction func unwindToThisViewController(segue: UIStoryboardSegue) {
        println("Returned from detail screen")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

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

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // Return the number of sections.
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {        
        // Return the number of rows in the section.
        return 3
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

        // Configure the cell...
        cell.textLabel!.text = "Row: \(indexPath.row)"
        return cell
    }


}

    //
//  ViewController.swift
//  Detail View

import UIKit

class ViewController: UIViewController {

    // When Save button is clicked, view controller is segued to the TableViewController to the function
    // unwindToThisViewController

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

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

    // MARK: - Navigation

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Insert your save data statements in this function
        println("Insert your save data statements in this function")
    }


}

如果使用UINavigationController ,則可以直接彈出viewController

示例: [self.navigationController popToViewController:TableViewController animated:YES];

暫無
暫無

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

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