使用 SearchController 后的 DidSelect 导致快速崩溃
Posted
技术标签:
【中文标题】使用 SearchController 后的 DidSelect 导致快速崩溃【英文标题】:DidSelect after SearchController is used causes crash swift 【发布时间】:2016-09-18 00:23:30 【问题描述】:我有一个 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
【问题讨论】:
设置断点以识别导致崩溃的行。 @BenJammin 它在汇编代码中的某个地方,在代码中没有具体的地方。在 didSelect 之后,它会进入汇编代码。走出几步后,它崩溃了。但它从来没有跨出到 viewController 代码的任何部分而不崩溃...... 【参考方案1】:我找到了解决方案。它首先与视图的加载方式有关。注释掉的代码是原始代码。通过使其成为模态转场来修复它。
@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
【讨论】:
以上是关于使用 SearchController 后的 DidSelect 导致快速崩溃的主要内容,如果未能解决你的问题,请参考以下文章
使用 SearchController 搜索时使用标签栏出现黑屏
使用 SearchController 推送 ViewController 在搜索栏下显示黑色矩形