gpt4 book ai didi

ios - 在多个 Controller 上执行 segue

转载 作者:搜寻专家 更新时间:2023-11-01 05:41:25 24 4
gpt4 key购买 nike

如何执行从 UITabBarController Child ViewController 到 UITableViewController 的 DetailView 的 segue,中间有 UINavigationController?

enter image description here

有一个 TabBarController 有两个 child ,FirstView(最常查看的符号)和 NavigationController(联系人符号)。

FirstView 有一个按钮,它应该执行一个 segue 以显示配置文件 VC。NavCont 有一个带有子类 All ProfilesTVC 的 TableView,带有一个原型(prototype)单元格作为子级。AllProfilesTVC 有一个包含三个名称的数组,这些名称由重复使用的单元格显示。

我必须在 FirstView (HomeVC) 中的函数 prepareForSegue 实例化和准备哪个 View Controller ,我在 Storyboard中创建的 segue 应该指向哪里?所以我在“约翰的”DetailView。当我对 ShowProfileVC 进行转场时,我是否可以按下后退按钮返回到 All ProfilesTVC?

有一个 github 仓库供那些想尝试的人使用 github repo

FirstView/HomeVC.swift

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

// what do we do here ???
}

AllItemsTVC

class AllItemsTVC: UITableViewController {

let profiles = ["Joe", "John", "Ken"]

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

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return profiles.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
let profile = profiles[indexPath.row] as String
cell.textLabel?.text = profile
return cell
}

@IBAction func cancelFromShowProfile(segue: UIStoryboardSegue) {
}

// MARK: - Navigation

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if let identifier = segue.identifier {
switch identifier {
case "ShowProfile":
if let showProfileVC = segue.destinationViewController as? ShowProfileVC {
// pass the data to the destinationVC
let selectedProfile = profiles[tableView.indexPathForSelectedRow()!.row] as String
showProfileVC.name = selectedProfile
}
default: break
}
}
}

ShowProfileVC

class ShowProfileVC: UIViewController {

@IBOutlet weak var textLabel: UILabel! {
didSet {
textLabel.text = name
}
}

var name = "Label"

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

感谢您的帮助。

最佳答案

你不能用 segue 来做到这一点,但这不是问题,因为你可以在代码中非常简单地做到这一点。只需在 Storyboard中为您的 View Controller 提供一个标识符,然后使用适当的UIStoryboard API 通过此标识符实例化它们。

首先在代码中切换到另一个标签栏项目,然后首先告诉导航 Controller popToRootViewController,之后您可以将所有需要的 Controller 依次推送到导航堆栈上。在推送 Controller 之前,您可以在 prepareForSegue 中执行您通常执行的所有配置。

诀窍是将 animated 设置为 false,最后一步除外。

关于ios - 在多个 Controller 上执行 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29777302/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com