Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default Detail View Controller when nothing is selected in iPad in Swift (in Split View Controller)?

I want to know that how can I set an default Detail View Controller when nothing is selected in master view controller in iPad in Swift.

For example, if the app just load inside and user didn't select anything, the detail view controller will be ViewControllerDefault, if user select any of the rows in master view controller, the detail view controller will be ViewControllerResult. So how can I do that in Swift? Thanks!

like image 637
He Yifei 何一非 Avatar asked Aug 09 '15 12:08

He Yifei 何一非


1 Answers

Over a long time has been passed, I hope it helps someone.

func viewDidLoad() {
        //..
        let initialIndexPath = NSIndexPath(forRow: 0, inSection: 0)
        self.tableView.selectRowAtIndexPath(initialIndexPath, animated: true, scrollPosition:UITableViewScrollPosition.None)

        if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
            self.performSegueWithIdentifier("ShowDetail", sender: initialIndexPath)
        }
}

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "ShowDetail" {
            if sender!.isKindOfClass(UITableViewCell) {
                if let indexPath = self.tableView.indexPathForCell(sender as! UITableViewCell) {

                    // Get row from selected row in tableview

                }
            } else if sender!.isKindOfClass(NSIndexPath) {
                let tableCell = self.tableView.cellForRowAtIndexPath(sender as! NSIndexPath)
                let indexPath = self.tableView.indexPathForCell(tableCell!)

                // Get row from sender, which is an NSIndexPath

            }
        }
}
like image 180
etnclp Avatar answered Nov 07 '22 20:11

etnclp