执行segues到非默认选项卡视图控制器模态不同的故事板

时间:2016-05-16 23:36:36

标签: ios swift storyboard uitabbarcontroller segue

我想执行segue(从一个视图控制器到另一个选项卡视图控制器的模态)。但问题是它是加载默认(在索引0处)视图控制器。这是代码:

   override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if (segue.identifier == "toRateSegue") {

        let nav = segue.destinationViewController as! UINavigationController

        let svc = nav.topViewController as! RateViewController;
        svc.cell = self.cellSelected
    }

    if (segue.identifier == "toChat") {
        let tabIndex = 1
        let segue = segue.destinationViewController as! UITabBarController

        if let navVC = segue.viewControllers?[0] as? UINavigationController {
            let vc = navVC.viewControllers.first as! CustomProfileViewController
            vc.event = self.event

        }

        if let navVC = segue.viewControllers?[tabIndex] as? UINavigationController {
            let vc = navVC.viewControllers.first as! ChatViewController
            vc.event = self.event
            print("Event is \(self.event.eventId)")
            // if self is the event creator
            if (self.userName == self.event.createdBy.objectForKey("username") as? String)
            {


                let query = PFQuery(className:"Event")
                query.getObjectInBackgroundWithId(self.event.eventId!) {
                    (curevent: PFObject?, error: NSError?) -> Void in
                    if error == nil && curevent != nil {

                        let userObject:PFUser = curevent!["joinedBy"] as! PFUser

                        print("userObject.objectId \(userObject.objectId)")

                        vc.otherName =  String(userObject.objectForKey("firstname")!)
                        vc.otherProfileName = String(userObject.objectForKey("firstname")!)
                        //print("vc.otherProfileName \(vc.otherProfileName)")
                        let senderuserName = PFUser.currentUser()!["username"] as? String
                        //print("senderuserName \(senderuserName)")

                        vc.roomid = self.event.geteventID()  + vc.otherProfileName + senderuserName!

                       // print("roomid \(vc.roomid)")

                    } else {
                        print(error)
                    }

                }

            }else
            {

                vc.otherName = (self.event.createdBy.objectForKey("firstname") as? String)!
                vc.otherProfileName = (self.event.createdBy.objectForKey("firstname") as? String)!
                let senderuserName = PFUser.currentUser()!["username"] as? String

                vc.roomid = event.geteventID() + senderuserName! + vc.otherProfileName

            }


             self.tabBarController?.selectedIndex = tabIndex

    }
    }

}

我在谈论这个问题。它在tableview函数中调用如下:

let connect = UITableViewRowAction(style: .Normal, title: "Connect") { action, index in

            self.event = cell.event

        self.performSegueWithIdentifier("toChat", sender: nil)



    }`

我想转到索引1的标签视图而不是索引0。

1 个答案:

答案 0 :(得分:0)

要移至UITabBarController中的prepareForSegue中的其他标签,请将selectedIndex设为1destinationViewControllerUITabBarController }:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let dvc = segue.destinationViewController as? UITabBarController {
        dvc.selectedIndex = 1
    }
}