Anthony Saltarelli
Anthony Saltarelli

Reputation: 345

DidSelect after SearchController is used causes crash swift

I have a TableView and a Search Controller. They both work perfectly with regards to displaying the info, searching, etc. The problem arises when you go to select one of the cells.

In my code, there are 4 ways you can select the cell... If a bool isAboutNewPost == true / false, and if the search is active or not. If the search controller is active, I am using the data from a filtered array.

When you select a cell and the search controller is not active, it works perfectly. When you select a cell and the search controller is active, but the bool is false, it works perfectly. When you select a cell and the search controller is active and the bool is true, it crashes. I have no clue why, because it is almost identical code.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    //
    self.resultSearchController.endEditing(true)

    switch(segmentControl.selectedSegmentIndex)
    {
    case 0:
        if self.theResultSearchController.active {


            self.theResultSearchController.hidesNavigationBarDuringPresentation = false

            print("it is in search")
            let indexPath = tableView.indexPathForSelectedRow

            let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as! NewAddPostTableViewCell
            theResultSearchController.active = false

            print("Going to Did Select Name!")
            //            self.performSegueWithIdentifier("transferToAddChinUp", sender: currentCell.name.text)

            let sb = UIStoryboard(name: "Main", bundle: nil)

            if self.isAboutNewPost == true {

                print("There was a new post.")
                let messagesVC = sb.instantiateViewControllerWithIdentifier("AddChinUpScreen") as! AddChinUpViewController
                messagesVC.userObjectId = currentCell.nameLabel.text
                messagesVC.thereWasJustANewPost = true
                print("i got to right here.")
                self.navigationController?.pushViewController(messagesVC, animated: true)

                // new line
                //self.performSegueWithIdentifier("transferToAddChinUp", sender: currentCell.nameLabel.text)

            } else {
                print("There wasn't a new post.")
                let messagesVC = sb.instantiateViewControllerWithIdentifier("AddChinUpScreen") as! AddChinUpViewController
                messagesVC.userObjectId = currentCell.nameLabel.text
                self.navigationController?.pushViewController(messagesVC, animated: true)
                //self.performSegueWithIdentifier("didSelectName", sender: currentCell.nameLabel.text)

            }

        } else {

            if self.isAboutNewPost == true {
                print("it is not in search")
                let indexPath = tableView.indexPathForSelectedRow

                let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as! NewAddPostTableViewCell

                print("Going to transferToAddChinUp!")
                self.performSegueWithIdentifier("transferToAddChinUp", sender: currentCell.nameLabel.text)
            } else {
                print("it is not in search")
                let indexPath = tableView.indexPathForSelectedRow

                let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as! NewAddPostTableViewCell

                print("Going to Did Select Name!")
                self.performSegueWithIdentifier("didSelectName", sender: currentCell.nameLabel.text)
            }



        }
        break

    case 1:
        if self.theResultSearchController.active {


            self.theResultSearchController.hidesNavigationBarDuringPresentation = false

            print("it is in search")
            let indexPath = tableView.indexPathForSelectedRow

            let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as! NewAddPostTableViewCell
            theResultSearchController.active = false

            print("Going to Did Select Name!")
            //            self.performSegueWithIdentifier("transferToAddChinUp", sender: currentCell.name.text)

            let sb = UIStoryboard(name: "Main", bundle: nil)

            if isAboutNewPost == true {
                print("There was a new post!!!!!!")
                let messagesVC = sb.instantiateViewControllerWithIdentifier("AddChinUpScreen") as! AddChinUpViewController
                messagesVC.userObjectId = currentCell.nameLabel.text
                messagesVC.thereWasJustANewPost = true
                self.navigationController?.pushViewController(messagesVC, animated: true)
               // self.performSegueWithIdentifier("transferToAddChinUp", sender: currentCell.nameLabel.text)
            } else {
                let messagesVC = sb.instantiateViewControllerWithIdentifier("AddChinUpScreen") as! AddChinUpViewController
                messagesVC.userObjectId = currentCell.nameLabel.text
                self.navigationController?.pushViewController(messagesVC, animated: true)
              //  self.performSegueWithIdentifier("didSelectName", sender: currentCell.nameLabel.text)

            }

        } else {

            if isAboutNewPost == true {
                print("it is not in search")
                let indexPath = tableView.indexPathForSelectedRow

                let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as! NewAddPostTableViewCell

                print("Going to transferToAddChinUp!")
                self.performSegueWithIdentifier("transferToAddChinUp", sender: currentCell.nameLabel.text)
            } else {
                print("it is not in search")
                let indexPath = tableView.indexPathForSelectedRow

                let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as! NewAddPostTableViewCell

                print("Going to Did Select Name!")
                self.performSegueWithIdentifier("didSelectName", sender: currentCell.nameLabel.text)
            }


        }
        break

    default:
        break
    }

    //AddChinUpScreen

}

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

        let completeSignUpVC = segue.destinationViewController as! AddChinUpViewController
        let selectedRowIndex = self.theTableView.indexPathForSelectedRow
        let currentCell = theTableView.cellForRowAtIndexPath(selectedRowIndex!) as! NewAddPostTableViewCell
        completeSignUpVC.userObjectId = currentCell.nameLabel.text

    } else if segue.identifier == "transferToAddChinUp" {
        let completeSignUpVC = segue.destinationViewController as! AddChinUpViewController
        //let selectedRowIndex = self.theTableView.indexPathForSelectedRow
        //let currentCell = theTableView.cellForRowAtIndexPath(selectedRowIndex!) as! NewAddPostTableViewCell

        completeSignUpVC.userObjectId = sender as! String
        completeSignUpVC.thereWasJustANewPost = true
    } else if segue.identifier == "tappedOnProfilePicture" {
        let completeSignUpVC = segue.destinationViewController as! AddChinUpViewController

        completeSignUpVC.userObjectId = sender as! String
        if self.isAboutNewPost == true {
            completeSignUpVC.thereWasJustANewPost = true

        } else {
            //completeSignUpVC
        }
    }

}

Here is the Error:

   Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_UIFullscreenPresentationController adaptivePresentationController]: unrecognized selector sent to instance 0x7febc34cdf60'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001072e3f65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001069d5deb objc_exception_throw + 48
    2   CoreFoundation                      0x00000001072ec58d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x0000000107239f7a ___forwarding___ + 970
    4   CoreFoundation                      0x0000000107239b28 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x00000001084fb389 -[UISearchController _searchPresentationController] + 134
    6   UIKit                               0x00000001080d7755 -[_UISearchControllerTransplantSearchBarAnimator animateTransition:] + 215
    7   UIKit                               0x0000000107c81ede __56-[UIPresentationController runTransitionForCurrentState]_block_invoke + 2638
    8   UIKit                               0x0000000107b2e4be _runAfterCACommitDeferredBlocks + 317
    9   UIKit                               0x0000000107b407ee _cleanUpAfterCAFlushAndRunDeferredBlocks + 95
    10  UIKit                               0x0000000107b4c4e6 _afterCACommitHandler + 90
    11  CoreFoundation                      0x000000010720f9d7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    12  CoreFoundation                      0x000000010720f947 __CFRunLoopDoObservers + 391
    13  CoreFoundation                      0x000000010720559b __CFRunLoopRun + 1147
    14  CoreFoundation                      0x0000000107204e98 CFRunLoopRunSpecific + 488
    15  GraphicsServices                    0x000000010a6c0ad2 GSEventRunModal + 161
    16  UIKit                               0x0000000107b22676 UIApplicationMain + 171
    17  Chin Up 2                           0x0000000104d180fd main + 109
    18  libdyld.dylib                       0x0000000109a5792d start + 1
    19  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Upvotes: 0

Views: 316

Answers (1)

Anthony Saltarelli
Anthony Saltarelli

Reputation: 345

I found the solution. It had to do with how the view was loaded in the first place. Commented out code was the original code. Fixed it by making it a modal segue.

@IBAction func goToUnlockPost(sender: AnyObject) {

//        let vc = self.storyboard!.instantiateViewControllerWithIdentifier("NewAddPost") as! NewAddPostViewController
//        vc.isAboutNewPost = true
//        vc.comingFromUnlockPost = true
//        self.presentViewController(vc, animated: true, completion: nil)
    self.performSegueWithIdentifier("tappedUnlockPost", sender: nil)

}


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

        let destinationNavigationController = segue.destinationViewController as! UINavigationController
        let targetController = destinationNavigationController.topViewController as! NewAddPostViewController

        //targetController = segue.destinationViewController as! NewAddPostViewController
        targetController.isAboutNewPost = true
        targetController.comingFromUnlockPost = true

    }
}

Upvotes: 1

Related Questions