使用SearchController后的DidSelect导致崩溃迅速

时间:2016-09-18 00:23:30

标签: ios swift uitableview uisearchbar uisearchcontroller

我有一个TableView和一个搜索控制器。它们在显示信息,搜索等方面都能很好地工作。当您选择其中一个单元格时会出现问题。

在我的代码中,有4种方法可以选择单元格...如果bool isAboutNewPost == true / false,并且搜索是否有效。如果搜索控制器处于活动状态,我将使用已过滤数组中的数据。

当您选择一个单元格并且搜索控制器处于活动状态时,它可以正常工作。当您选择一个单元格并且搜索控制器 处于活动状态,但是bool为 false 时,它可以正常工作。当您选择一个单元格并且搜索控制器 处于活动状态并且bool为 true 时,它会崩溃。我不知道为什么,因为它几乎是相同的代码。

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

}

这是错误:

   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

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。它与视图加载的方式有关。注释掉的代码是原始代码。通过使其成为模态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

    }
}