gpt4 book ai didi

ios - 在 swift xcode 中通过 View 发送数据

转载 作者:行者123 更新时间:2023-11-30 14:14:27 25 4
gpt4 key购买 nike

我正在尝试通过 View Controller 发送数据。

如果我点击一个单元格,我就会有一个转场将我转移到另一个 View Controller 。我有一个标签,我想根据单击的单元格中的文本进行更改。

这是我目前的代码:

import UIKit

FirstViewController 类:UIViewController{

var 存储字符串 = String()

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
tableView.deselectRowAtIndexPath(indexPath, animated: true)

var venue : VenueItems

if (tableView == self.searchDisplayController?.searchResultsTableView)
{
venue = self.filteredVenues[indexPath.row]
var name = venue.name
storedStadiumSelected = name
}
else
{
venue = self.venuesArray[indexPath.row]
var name = venue.name
storedStadiumSelected = name
}


println(storedStadiumSelected)

}

这里的代码...

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

var nextViewController: SelectBookingTypeViewController = segue.destinationViewController as SelectBookingTypeViewController

nextViewController.recievedStadiumSelected = storedStadiumSelected

}

我的第二个 View Controller 如下所示:导入 UIKit

类 SelectBookingTypeViewController:UIViewController {

@IBOutlet weak var stadiumLabel: UILabel!

var recievedStadiumSelected = String()


override func viewWillAppear(animated: Bool) {
stadiumLabel.text = recievedStadiumSelected
}

这段代码的问题是我只加载了我之前单击的选项。例如,如果我单击“item1”,则标签文本什么都没有。但如果我返回并单击“item2”,标签现在是 item1。我不知道为什么会发生这种情况!

我对编码比较陌生,所以请原谅,谢谢您,我们将热情地接受任何帮助:)

最佳答案

最好的查找位置是 Apple 提供的 Master-Detail Application 的 Xcode 模板。这是您要查找的 DetailViewController 上的代码。

详细 View Controller

import UIKit

class DetailViewController: UIViewController {

@IBOutlet weak var detailDescriptionLabel: UILabel!


var detailItem: AnyObject? {
didSet {
// Update the view.
self.configureView()
}
}

func configureView() {
// Update the user interface for the detail item.
if let detail: AnyObject = self.detailItem {
if let label = self.detailDescriptionLabel {
label.text = detail.description

}
}
}

主视图 Controller

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
if let indexPath = self.tableView.indexPathForSelectedRow() {
let object = objects[indexPath.row] as! NSDate
let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
controller.detailItem = object
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}

表格 View 代码

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

let object = objects[indexPath.row] as! NSDate

//This Code here tells the label to update for the cell selected.
cell.textLabel!.text = object.description

return cell
}

所以你在第二个ViewController中需要这个

func configureView() {
// Update the user interface for the detail item.
if let detail: AnyObject = self.detailItem {
if let label = self.detailDescriptionLabel {

//Change the label name to match yours.
yourLabelName.text = detail.description
}
}
}

关于ios - 在 swift xcode 中通过 View 发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31361975/

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