Swift - 使用Segue

时间:2016-08-12 12:23:48

标签: ios swift uitableview

我有一个带有一个UIViewController类型视图的TabBar。在这个视图中,我添加了两个容器(一个用于即将交付,一个用于完成交付)。这两个容器中的每一个都链接到UITableViewController以显示相关订单的表。为了访问容器,我有一个分段控件。

到目前为止,我无法完成基于代码的所有尝试以及将数据从viewcontroller传递到tableviewcontrller的方法。我错过了什么吗?

以下是我在视图控制器中的代码:

@IBOutlet weak var ordersSegementedControl: UISegmentedControl!
@IBOutlet weak var upcomingDeliveriesContainer: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        SwiftLoader.show(title: "Retrieving Orders...", animated: true)
        let currentUser = FIRAuth.auth()?.currentUser
        ref.child("ordersSummary").queryOrderedByChild("userId").queryEqualToValue("\(currentUser!.uid)").queryLimitedToLast(15).observeEventType(.ChildAdded, withBlock: { (snapshot) in

            if(snapshot.value!["orderStatus"] as? String != "In Progress" || snapshot.value!["orderStatus"] as? String != "Pending" )
            {
                let tempOrderObject = OrderObject()
                //let properDate: UnixTime = snapshot.value!["dueDate"] as! Int
                tempOrderObject.dueDateInt = snapshot.value!["dueDate"] as? Int
                let properDate: UnixTime = snapshot.value!["dueDate"] as! Int
                tempOrderObject.dueDate =  properDate.toDay
                tempOrderObject.referenceNumber = snapshot.value!["orderReference"] as? String
                tempOrderObject.status = snapshot.value!["orderStatus"] as? String
                tempOrderObject.userId = snapshot.value!["userId"] as? String
                tempOrderObject.sender = snapshot.value!["sender"] as? String
                self.ordersObjectsArrray.append(tempOrderObject)
                self.ordersObjectsArrray.sortInPlace({ $0.dueDate > $1.dueDate })
                SwiftLoader.hide()
            }

        }){ (error) in
            SwiftLoader.hide()
            print(error.localizedDescription)
        }

    }



    @IBAction func showComponent(sender: UISegmentedControl) {
        if sender.selectedSegmentIndex == 0 {
            UIView.animateWithDuration(0.5, animations: {
                self.upcomingDeliveriesContainer.alpha = 1
            })
        } else {
            UIView.animateWithDuration(0.5, animations: {
                self.upcomingDeliveriesContainer.alpha = 1
            })
        }
    }


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

        if(segue.identifier == "upcomingDeliveriesSegue")
        {
            let destinationVC = segue.destinationViewController as! UpcomingDeliveriesTableViewController
            destinationVC.ordersObjectsArrray = self.ordersObjectsArrray

        }
    }

然后我在UpcomingDeliveriesTableViewController中有以下内容:

class UpcomingDeliveriesTableViewController : UITableViewController {

    let ref = FIRDatabase.database().reference()
    var ordersObjectsArrray:[OrderObject]?
    let orderToPass = OrderObject()

    override func viewDidLoad() {
        super.viewDidLoad()
        print("Array Count: \(self.ordersObjectsArrray!.count)")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return ordersObjectsArrray!.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        // Configure the cell...
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UpcomingDeliveriesTableViewCell

        let orderEntry = self.ordersObjectsArrray![indexPath.row]
        cell.dueDateLabel.text = String(orderEntry.dueDate)
        cell.orderNoLabel.text = orderEntry.referenceNumber
        cell.statusLabel.text = orderEntry.status
        cell.senderLabel.text = orderEntry.sender

        return cell
    }



}

UpcomingDeliveriesTableVieController中的print语句打印0作为数组项的计数。到目前为止还没有通过。

这最终是故事板快照:

the embed segue is called: upcomingDeliveriesSegue

谢谢

1 个答案:

答案 0 :(得分:1)

prepareForSegue仅保存UpcomingDeliveriesTableViewController的实例,但ordersObjectsArrray仅在您获得时设置。

例如:

在视图控制器中:

var tvc: UpcomingDeliveriesTableViewController!

override func viewDidLoad() {
    super.viewDidLoad()

    SwiftLoader.show(title: "Retrieving Orders...", animated: true)
    let currentUser = FIRAuth.auth()?.currentUser
    ref.child("ordersSummary").queryOrderedByChild("userId").queryEqualToValue("\(currentUser!.uid)").queryLimitedToLast(15).observeEventType(.ChildAdded, withBlock: { (snapshot) in

        if(snapshot.value!["orderStatus"] as? String != "In Progress" || snapshot.value!["orderStatus"] as? String != "Pending" )
        {
            let tempOrderObject = OrderObject()
            //let properDate: UnixTime = snapshot.value!["dueDate"] as! Int
            tempOrderObject.dueDateInt = snapshot.value!["dueDate"] as? Int
            let properDate: UnixTime = snapshot.value!["dueDate"] as! Int
            tempOrderObject.dueDate =  properDate.toDay
            tempOrderObject.referenceNumber = snapshot.value!["orderReference"] as? String
            tempOrderObject.status = snapshot.value!["orderStatus"] as? String
            tempOrderObject.userId = snapshot.value!["userId"] as? String
            tempOrderObject.sender = snapshot.value!["sender"] as? String
            self.ordersObjectsArrray.append(tempOrderObject)
            self.ordersObjectsArrray.sortInPlace({ $0.dueDate > $1.dueDate })
            SwiftLoader.hide()
        }

    tvc.ordersObjectsArrray = self.ordersObjectsArrray

    }){ (error) in
        SwiftLoader.hide()
        print(error.localizedDescription)
    }

}


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

    if(segue.identifier == "upcomingDeliveriesSegue")
    {
        tvc = segue.destinationViewController as! UpcomingDeliveriesTableViewController
    }
}