Alexander Khitev
Alexander Khitev

Reputation: 6861

segue error in Xcode 6

I have UITableViewController and two UIViewController. Each controller has a UINavigationController. I made two segue the first from UITableViewCell and the second from UIBarButtonItem with help of Storyboard. When I move from UITableViewCell I have a success but when I move from UIBarButtonItem I get a error "fatal error: unexpectedly found nil while unwrapping an Optional value". enter image description here

Still I made the segue into the code. This code

@IBAction func addMusic(sender: UIBarButtonItem) {
    performSegueWithIdentifier("addMusic", sender: self)
}



override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: UITableViewScrollPosition.None)
        performSegueWithIdentifier("listenMusic", sender: nil)
    }

This error enter image description here

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

    var indexPath = tableView.indexPathForSelectedRow()
    var nameOfObjectForPass = listOfMP3Files![indexPath!.row] // default it's name and
    var fileManager = NSFileManager.defaultManager()

    var wayToFile = fileManager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)
    var passMusicFileURL: NSURL? // for pass mp3

    if let documentPath: NSURL = wayToFile.first as? NSURL {
        let musicFile = documentPath.URLByAppendingPathComponent(nameOfObjectForPass)
        println(musicFile)
        passMusicFileURL = musicFile
    }

    var currentTrackPath = tableView.indexPathForSelectedRow()!

    var allIndexTable = tableView.indexPathsForVisibleRows()! as! [NSIndexPath]

    var currentIndexPath = tableView.indexPathForSelectedRow()!.row

    if segue.identifier == "listenMusic" {
        var playerVC = (segue.destinationViewController as! UINavigationController).topViewController as! PlayMusicViewController
        playerVC.nameMusicFile = nameOfObjectForPass // name
        playerVC.mp3URL = passMusicFileURL 

        // test 
        playerVC.currentPath = currentTrackPath

        playerVC.allIndex = allIndexTable
        //
        playerVC.currentIndex = currentIndexPath
        // variant the second
        var curRow = tableView.indexPathForSelectedRow()!.row
        playerVC.arrayOfMP3 = listOfMP3Files
        playerVC.currentRow = curRow
    } else if segue.identifier == "add" {
        var avc = (segue.destinationViewController as! UINavigationController).topViewController as! AddViewController
    }
}

Upvotes: 0

Views: 141

Answers (3)

Julian J. Tejera
Julian J. Tejera

Reputation: 1021

If you have not preselected any row tableView.indexPathForSelectedRow() will return nil and it will crash it this line var nameOfObjectForPass = listOfMP3Files![indexPath!.row].

Try wrapping your operations inside of each conditional statement.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "listenMusic" {
        var indexPath = tableView.indexPathForSelectedRow()
        var nameOfObjectForPass = listOfMP3Files![indexPath!.row] // default it's name and
        var fileManager = NSFileManager.defaultManager()

        var wayToFile = fileManager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)
        var passMusicFileURL: NSURL? // for pass mp3

        if let documentPath: NSURL = wayToFile.first as? NSURL {
            let musicFile = documentPath.URLByAppendingPathComponent(nameOfObjectForPass)
            println(musicFile)
            passMusicFileURL = musicFile
        }

        var currentTrackPath = tableView.indexPathForSelectedRow()!

        var allIndexTable = tableView.indexPathsForVisibleRows()! as! [NSIndexPath]

        var currentIndexPath = tableView.indexPathForSelectedRow()!.row
        var playerVC = (segue.destinationViewController as! UINavigationController).topViewController as! PlayMusicViewController
        playerVC.nameMusicFile = nameOfObjectForPass // name
        playerVC.mp3URL = passMusicFileURL

        // test
        playerVC.currentPath = currentTrackPath

        playerVC.allIndex = allIndexTable
        //
        playerVC.currentIndex = currentIndexPath
        // variant the second
        var curRow = tableView.indexPathForSelectedRow()!.row
        playerVC.arrayOfMP3 = listOfMP3Files
        playerVC.currentRow = curRow
    } else if segue.identifier == "add" {
        var avc = (segue.destinationViewController as! UINavigationController).topViewController as! AddViewController
    }
}

Upvotes: 1

sriram hegde
sriram hegde

Reputation: 2471

Try this code

@IBAction func addMusic(sender: UIBarButtonItem) {
    performSegueWithIdentifier("add", sender: sender)
}

Upvotes: -1

sriram hegde
sriram hegde

Reputation: 2471

Change this ..

performSegueWithIdentifier("listenMusic", sender: nil)

To

 performSegueWithIdentifier("listenMusic", sender: self // or if its button then make it button )

Upvotes: -1

Related Questions