一个简单的performSegue抛出和错误准备segue函数

时间:2017-11-12 22:19:27

标签: ios swift uitableview uistoryboardsegue

也许是愚蠢的事,但我试图简单地从tableviewController转移到viewController,不使用

简单移动任何数据
@IBAction func requestPanel(_ sender: Any) {

   performSegue(withIdentifier: "sendMail", sender: AnyObject)
}

然后我在准备segue func时遇到错误。

但是,对于我的其他人来说,这有什么用呢?

据我所知,由于没有数据,segue会遇到麻烦。

正如你在这里看到的,segue正在为segue做准备。

@IBAction func requestPanel(_ sender: Any) {

    self.performSegue(withIdentifier: "sendMail", sender: AnyObject.self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    var indexPath: IndexPath = self.tableView.indexPathForSelectedRow!
    let desti = segue.destination as! DeviceDetailTableViewController

    let selectedRecord = onlineDevices[indexPath.row]

    let panelWidth = selectedRecord.object(forKey: "panelwidth") as? String
    let panelHight = selectedRecord.object(forKey: "panelhight") as? String
    let panelPitch = selectedRecord.object(forKey: "panelpitch") as? String
    let panelPower = selectedRecord.object(forKey: "panelpower") as? String
    let panelWeight = selectedRecord.object(forKey: "panelweight") as? String
    let panelMaker = selectedRecord.object(forKey: "maker") as? String
    let panelModel = selectedRecord.object(forKey: "model") as?      String

    desti.pawidth = panelWidth!
    desti.pahight = panelHight!
    desti.papitch = panelPitch!
    desti.papower = panelPower!
    desti.weight = panelWeight!
    desti.maker = panelMaker!
    desti.model = panelModel!
}



 override func numberOfSections(in tableView: UITableView) -> Int {
 return 1
 }
 override func tableView(_ tableView: UITableView,   numberOfRowsInSection section: Int) -> Int {
 return onlineDevices.count
 }
   override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "DeviceCell", for: indexPath)

    // Configure the cell...

    let noteRecord: CKRecord = onlineDevices[(indexPath as IndexPath).row]

    cell.textLabel?.text = noteRecord.value(forKey: "maker") as? String
    cell.detailTextLabel?.text = noteRecord.value(forKey: "model") as? String


    return cell
}


}

2 个答案:

答案 0 :(得分:1)

错误消息很明确。您没有选择任何行时说nil。因此,该值为prepareForSegue,当您强制解包时会崩溃。

当用户选择表格的一行时触发了您的唯一segue时,您编写了原始prepareForSegue。但 segue 由用户选择表格的一行触发;它由用户点击按钮触发。你必须考虑到这种差异。

(这是整个架构中的一个主要缺陷:来自给定视图控制器的所有segue流入相同的import cv2 import numpy as np image_names = ['original_field_1_0.PNG','original_field_1_1.PNG','original_field_1_3.PNG','original_field_1_4.PNG','original_field_1_5.PNG'] images = [] max_width = 0 # find the max width of all the images total_height = 0 # the total height of the images (vertical stacking) for name in image_names: # open all images and find their sizes images.append(cv2.imread(name)) if images[-1].shape[1] > max_width: max_width = images[-1].shape[1] total_height += images[-1].shape[0] # create a new array with a size large enough to contain all the images final_image = np.zeros((total_height,max_width,3),dtype=np.uint8) current_y = 0 # keep track of where your current image was last placed in the y coordinate for image in images: # add an image to the final array and increment the y coordinate final_image[current_y:image.shape[0]+current_y,:image.shape[1],:] = image current_y += image.shape[0] cv2.imwrite('fin.PNG',final_image) ,这成为瓶颈。)

答案 1 :(得分:1)

viewController中的所有segues都将使用相同的segue.identifier方法。您需要使用sendMail以不同方式处理不同的segue。

您的UIBarButton segue来自override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "sendMail" { // nothing extra to be done here } else { // do your normal path here if let indexPath = self.tableView.indexPathForSelectedRow { // you have a valid selected row so proceed } } } 操作而非选定行,因此请检查该segue并单独处理:

{{1}}