Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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
Ios Swift-使用Segue将数据从容器传递到TableViewController_Ios_Swift_Uitableview - Fatal编程技术网

Ios Swift-使用Segue将数据从容器传递到TableViewController

Ios Swift-使用Segue将数据从容器传递到TableViewController,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个选项卡栏,其中一个视图的类型为UIViewController。在这个视图中,我添加了两个容器(一个用于即将到来的交付,另一个用于已完成的交付)。这两个容器中的每一个都链接到UITableViewController,以显示相关订单的表格。要访问容器,我有一个分段控件 到目前为止,我无法根据代码和方法将数据从viewcontroller传递到tableviewcontrller。我错过什么了吗 以下是我在视图控制器中的代码: @IBOutlet weak var ordersSegeme

我有一个选项卡栏,其中一个视图的类型为UIViewController。在这个视图中,我添加了两个容器(一个用于即将到来的交付,另一个用于已完成的交付)。这两个容器中的每一个都链接到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
            
        }
    }
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
    }


    
}
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
    }
}
然后,我在即将到来的DeliveriestablevewController中拥有以下内容:

@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
            
        }
    }
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
    }


    
}
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
    }
}
UpComingDeliverieStableController中的print语句将0打印为数组项的计数。到目前为止还没有通过

最后是情节提要快照:


感谢您

prepareForSegue
中只保存
UpcomingDeliveriesTableViewController的实例
,但
订单对象阵列
仅在您获得它时设置

例:

在视图控制器中:

@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
            
        }
    }
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
    }


    
}
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
    }
}

检查是否要在UpComingDeliverieStableController中重新初始化OrderSobjectsArray??我正在这样做:var orderSobjectsArray=[OrderObject]()@VishalSonawane@VishalSonawane对不起,我没听清楚,你能给我看看你即将推出的DeliveryStableViewer控制器吗??就在变量声明中。您在故事板中将segue标识符名称设置为“UpComingDeliveriesGue”了吗?如果不完全理解您所说的内容,Igor
prepareForSegue
会提前执行,那么您会在
ObserveeEventType
中获得
self.OrdersObjectsArray
,明白吗?在上面的prepareForSegue中,我们创建一个新变量tvc,然后呢?如何将其传递到下一个视图?在获取
self.ordersobjectsarray
传递值后,在视图中加载,就像我添加的那样:
tvc.ordersobjectsarray=self.ordersobjectsarray
var-tvc:UpcomingDeliveriesTableViewController在视图中创建控制器类