swift segue not passing data from tableview(SWIFT段不从表视图传递数据)
本文介绍了SWIFT段不从表视图传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
访问从tableViewController
发送到viewController
的数据时遇到问题。
第一个文件TableView控制器使用以下代码查找选定行并通过段传递数据:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Get the row data for the selected row
var name = self.names[indexPath.row]
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
let selectedIndex = self.tableView.indexPathForCell(sender as UITableViewCell)
if segue.identifier == "PictureSegue"{
let vc = segue.destinationViewController as pictureViewController
vc.recvData = name
}
}
}
第二个文件(ViewController)正在尝试读取数据并打开警报。警报一直显示为空。为什么它没有接收到数据?
class pictureViewController: UIViewController {
var recvData:String!
override func viewDidLoad() {
super.viewDidLoad()
if recvData != nil
{
if (recvData == "Apapne")
{
var alert: UIAlertView = UIAlertView()
alert.title = recvData
alert.message = recvData
alert.addButtonWithTitle("Ok")
alert.show()
}
}
else
{
var alert: UIAlertView = UIAlertView()
alert.title = "no"
alert.message = "stuff"
alert.addButtonWithTitle("Ok")
alert.show()
}
}
}
谢谢
推荐答案
问题出在preparareForSegue方法上。你必须推翻它。在您的例子中,您只是在TableView didSelectRowAtIndexPath方法中定义了一个名为preparareForSegue的函数,但是您应该插入一个覆盖方法,而不是您的内部函数:
override func prepareForSegue(segue:UIStoryboardSegue, sender:AnyObject!) {
// do preparations
}
这篇关于SWIFT段不从表视图传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:SWIFT段不从表视图传递数据


基础教程推荐
猜你喜欢
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01