Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift3 TableView控制器未向详细信息控制器发送变量_Swift3 - Fatal编程技术网

Swift3 TableView控制器未向详细信息控制器发送变量

Swift3 TableView控制器未向详细信息控制器发送变量,swift3,Swift3,我已经研究过这个问题,我几乎可以肯定我的代码是正确的,但很明显有什么地方出了问题。我有两个控制器。一个是TableView,另一个是带有标签的视图控制器。我希望将用户选择的tableview单元格中的单元格文本标签的值发送到第二个视图控制器。很简单。这是我的控制器: import UIKit import CoreData class FavoritesViewController: UIViewController, UITableViewDelegate, UITableViewDataS

我已经研究过这个问题,我几乎可以肯定我的代码是正确的,但很明显有什么地方出了问题。我有两个控制器。一个是TableView,另一个是带有标签的视图控制器。我希望将用户选择的tableview单元格中的单元格文本标签的值发送到第二个视图控制器。很简单。这是我的控制器:

import UIKit
import CoreData

class FavoritesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var selectedFavorite = ""

var specifiedFavorite:String!

let appDelegate = UIApplication.shared.delegate as! AppDelegate

var passages = [NSManagedObject]()

//This code creates the number of cells equal the number of items in the object
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return passages.count

}


// The code below displays the passage reference as the cell title
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // This creates the cell for the table. The dequeueReusableCell option ensures the table is scrollable
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell

    // this creates a variable for the record defined at the index that is numbered by the indexPath.row.
    let favorite = passages[indexPath.row]
    // We then find the value of the key we want to set for the cell label value
    cell.textLabel!.text = favorite.value(forKey: "reference") as? String

    return cell

}


// This code detects the cell selected and captures a variable that is passed in the segue
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let selectedCell = tableView.cellForRow(at: indexPath) as UITableViewCell!

    selectedFavorite = (selectedCell?.textLabel?.text)!

    print("The text in the selected cell is \(selectedFavorite)")

    performSegue(withIdentifier: "toFavoritesDetailsViewController", sender: nil)

    print("Segue performed")

}

//This code is the segue that passes the variable values to the next view
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "toFavoritesDetailsViewController" {

        let favoritesDetailsViewController = segue.destination as! FavoritesDetailsViewController

        favoritesDetailsViewController.specifiedFavorite = selectedFavorite

        print("The variable for selectedFavorite prior to segue is: \(selectedFavorite)")

        print("The variable for favoritesDetailsViewController.specifiedFavorite prior to segue is: \(favoritesDetailsViewController.specifiedFavorite)")

    }
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

//This code loads the Core Data Entity into the view
// this is working as evidenced by the tableview being populated with data
    let appDelegate = UIApplication.shared.delegate as! AppDelegate

    let managedContext = appDelegate.persistentContainer.viewContext

    let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName:"Passages")
    //
    do {
        let results = try managedContext.fetch(fetchRequest)
        passages = results as! [NSManagedObject]
    } catch let error as NSError {
        print("Could not fetch \(error)")
    }
  }

}
导入UIKit
导入CoreData
类FavoritesViewController:UIViewController、UITableViewDelegate、UITableViewDataSource{
var selectedFavorite=“”
变量指定的avorite:String!
让appDelegate=UIApplication.shared.delegate为!appDelegate
变量通道=[NSManagedObject]()
//此代码创建的单元格数等于对象中的项目数
public func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回通道数
}
//下面的代码将段落引用显示为单元格标题
public func tableView(tableView:UITableView,cellForRowAt indexPath:indexPath)->UITableViewCell{
//这将为表创建单元格。dequeueReusableCell选项确保表可滚动
让cell=tableView.dequeueReusableCell(标识符为“cell”,for:indexPath)作为UITableViewCell
//这将为索引处定义的记录创建一个变量,该索引由indexPath.row编号。
让收藏夹=段落[indexPath.row]
//然后找到要为单元格标签值设置的键的值
cell.textlab!.text=favorite.value(forKey:“reference”)作为?字符串
返回单元
}
//此代码检测所选单元格并捕获在序列中传递的变量
func tableView(tableView:UITableView,didSelectRowAt indexPath:indexPath){
让selectedCell=tableView.cellForRow(at:indexPath)作为UITableViewCell!
selectedFavorite=(selectedCell?.textLabel?.text)!
打印(“所选单元格中的文本为\(selectedFavorite)”)
性能检查(标识符为:“toFavoritesDetailsViewController”,发送方:无)
打印(“已执行的序列”)
}
//此代码是将变量值传递到下一个视图的序列
覆盖功能准备(对于segue:UIStoryboardSegue,发送方:有吗?){
如果segue.identifier==“toFavoritesDetailsViewController”{
让favoritesDetailsViewController=segue.destination为!favoritesDetailsViewController
收藏夹DetailsViewController.specifiedFavorite=selectedFavorite
打印(“segue之前selectedFavorite的变量为:\(selectedFavorite)”)
打印(“segue之前favoritesDetailsViewController.specifiedFavorite的变量为:\(favoritesDetailsViewController.specifiedFavorite)”)
}
}
覆盖函数视图将出现(uo动画:Bool){
超级。视图将显示(动画)
//此代码将核心数据实体加载到视图中
//用数据填充tableview就证明了这一点
让appDelegate=UIApplication.shared.delegate为!appDelegate
让managedContext=appDelegate.persistentContainer.viewContext
let fetchRequest=NSFetchRequest(entityName:“通道”)
//
做{
let results=尝试managedContext.fetch(fetchRequest)
段落=结果为![NSManagedObject]
}将let错误捕获为NSError{
打印(“无法获取\(错误)”)
}
}
}
以下是我的第二个名为FavoritesDetailsViewController的控制器的代码:

import UIKit
import CoreData

class FavoritesDetailsViewController: UIViewController {

@IBOutlet weak var referenceLabel: UILabel!

@IBOutlet weak var passageText: UITextView!

var specifiedFavorite : String = ""

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    referenceLabel.text? = specifiedFavorite

    print("The viewDidLoad")
    // SMC LEFT OFF HERE
    // Can't get selectedFavorite to load from previous controller
    print("The Value of variable specifiedFavorite sent from segue is: \(specifiedFavorite)")

    print(specifiedFavorite)

    let appDelegate = UIApplication.shared.delegate as! AppDelegate

    let context = appDelegate.persistentContainer.viewContext

    let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Passages")

    request.predicate = NSPredicate(format: "reference = %@", specifiedFavorite)

    request.returnsObjectsAsFaults = false

    do {

        let results = try context.fetch(request)

        print("fetch request was successful")

        if results.count > 0 {

            for result in results as! [NSManagedObject] {

                //                    print("We got results!")

                if let returnedText = result.value(forKey: "passagetext") as? String {

                    print("This is the value of returnedText: \(returnedText)")

                    passageText.text? = returnedText

                    print("This is the text of the selectedFavorite after segue is: \(passageText)")

                }

            }

        }
    } catch {

        print("Couldn't fetch results")

    }


}

override func viewDidLoad() {
    super.viewDidLoad()

    print("The viewDidLoad")

    print("The Value of variable specifiedFavorite sent from segue is: \(specifiedFavorite)")

    print(specifiedFavorite)
}


}
导入UIKit
导入CoreData
类收藏夹DetailsViewController:UIViewController{
@IBOutlet弱var引用标签:UILabel!
@IBV passageText:UITextView!
var specifiedFavorite:String=“”
覆盖函数视图将出现(uo动画:Bool){
超级。视图将显示(动画)
referenceLabel.text?=指定的默认值
打印(“视图加载”)
//SMC在这里停了下来
//无法从以前的控制器获取要加载的selectedFavorite
打印(“从segue发送的变量specifiedFavorite的值为:\(specifiedFavorite)”)
打印(指定格式)
让appDelegate=UIApplication.shared.delegate为!appDelegate
让上下文=appDelegate.persistentContainer.viewContext
let request=NSFetchRequest(entityName:“通道”)
request.predicate=NSPredicate(格式:“reference=%@”,specifiedFavorite)
request.returnsObjectsAsFaults=false
做{
let results=try context.fetch(请求)
打印(“提取请求成功”)
如果results.count>0{
对于结果,结果为![NSManagedObject]{
//打印(“我们得到了结果!”)
如果让returnedText=result.value(forKey:“passagetext”)作为?字符串{
打印(“这是returnedText:\(returnedText)”的值)
passageText.text?=返回的文本
打印(“这是segue is:\(passageText)之后所选收藏夹的文本”)
}
}
}
}抓住{
打印(“无法获取结果”)
}
}
重写func viewDidLoad(){
super.viewDidLoad()
打印(“视图加载”)
打印(“从segue发送的变量specifiedFavorite的值为:\(specifiedFavorite)”)
打印(指定格式)
}
}
当我运行应用程序时,日志表明变量“specifiedFavorite”的值设置在segue之前

The variable for selectedFavorite prior to segue is: 
The variable for favoritesDetailsViewController.specifiedFavorite prior to segue is: 
The viewDidLoad
The Value of variable specifiedFavorite sent from segue is: 

The text in the selected cell is John 3:16
The variable for selectedFavorite prior to segue is: John 3:16
The variable for favoritesDetailsViewController.specifiedFavorite prior to segue is: John 3:16
2016-12-10 12:43:54.624 Logos to Mind[4036:770173] <UIView: 0x7ff0bf918010; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0x600000421fa0>>'s window is not equal to <Logos_to_Mind.FavoritesDetailsViewController: 0x7ff0bf9047f0>'s view's window!
Segue performed
The FavoritesDetailsViewController viewDidLoad
The Value of variable specifiedFavorite sent from segue is: 
fetch request was successful
segue之前selectedFavorite的变量为: segue之前favoritesDetailsViewController.specifiedFavorite的变量为: 视图加载 从segue发送的变量specifiedFavorite的值为: 所选单元格中的文本是约翰福音3:16 segue之前selectedFavorite的变量为:John 3:16 segue之前favoritesDetailsViewController.specifiedFavorite的变量为:John 3:16 2016-12-10 12:43:54.624记住的徽标[4036:770173]的窗口不等于视图的窗口! 塞格表演 收藏夹DetailsViewController视图加载 变量的值指定为dfav
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "toFavoritesDetailsViewController" {

        let favoritesDetailsViewController = segue.destination as! FavoritesDetailsViewController

        if let selectedCell = sender as? UITableViewCell {
            let selectedFavorite = (selectedCell.textLabel?.text)!

            favoritesDetailsViewController.specifiedFavorite = selectedFavorite

            print("The variable for selectedFavorite prior to segue is: \(selectedFavorite)")

            print("The variable for favoritesDetailsViewController.specifiedFavorite prior to segue is: \(favoritesDetailsViewController.specifiedFavorite)")
        }

    }
}