IOS Swift:如何在两个控制器之间传输数据
Posted
技术标签:
【中文标题】IOS Swift:如何在两个控制器之间传输数据【英文标题】:IOS Swift : How to transfer data between two controllers 【发布时间】:2015-04-22 10:05:22 【问题描述】:我尝试使用 segue 方法将数据从一个控制器传递到另一个控制器。 我的第一个视图控制器是
class ViewController: UIViewController
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!)
if (segue.identifier == "btnSubmitSegue")
var svc = segue.destinationViewController as ForthViewController;
svc.dataParsed = "this is new text"
在目标视图控制器中 ->
class ForthViewController: UIViewController
@IBOutlet weak var mylabel1: UILabel!
var dataParsed:String!
override func viewDidLoad()
super.viewDidLoad()
NsLog(dataParsed)
mylabel1.text = dataParsed
但数据未加载到目标控制器中的 mylabel1.text。你能帮忙弄清楚吗
【问题讨论】:
尝试将代码移至 viewDidAppeare 方法 NSLog 记录什么? (顺便说一句,你应该使用 swift 函数println()
。)
您的 (segue.identifier == "btnSubmitSegue") 条件是否为真?拼写错误的标识符是我的常见错误
打印出来的数据是什么意思?它打印正确吗?下次使用 println() 代替 NSLog
How do you share data between view controllers and other objects in Swift?的可能重复
【参考方案1】:
您必须在一个表格视图控制器中编写以下代码
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!)
if segue.identifier == "show"
if let indexPath = self.tableView.indexPathForSelectedRow()
let object = self.fetchedResultsController!.objectAtIndexPath(indexPath) as! NSManagedObject
let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController //DetailViewController
controller.detailItem = object
println(object)
在第二个视图控制器中
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view.
configureView()
var detailItem: AnyObject?
didSet
// Update the view.
self.configureView()
func configureView()
// Update the user interface for the detail item.
if let detail: AnyObject = detailItem
if var textView = self.text
textView.text! = detail.valueForKey("mynote")!.description
希望对你有所帮助。
【讨论】:
【参考方案2】:试试看:
class FirstViewController: UIViewController
private var myStringData = "This is my Data"
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
var destView: SecondViewController = segue.destinationViewController as! SecondViewController
destView.data = myStringData
它们是你的目标视图控制器:
class SecondViewController: UIViewController
var data: String?
@IBOutlet weak var labelWhereDataWillAppear: UILabel!
override func viewDidLoad()
labelWhereDataWillAppear.text = data!
print("\(data!)")
【讨论】:
【参考方案3】: override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!)
if segue.identifier == "currencySegue"
let controller = segue.destinationViewController as! SubmitDropMenuDetailedTableViewController
controller.currencyId = Int(self.currencyTypeId)
controller.entity = "Currency"
controller.delegate = self
else if segue.identifier == "categorySegue"
let controller = segue.destinationViewController as! SubmitDropMenuDetailedTableViewController
controller.entity = "Category"
controller.categoryId = Int(expenseTypeId)
controller.delegate = self
【讨论】:
以上是关于IOS Swift:如何在两个控制器之间传输数据的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Swift 3.0 中的控制器之间传递 JSON 数据?