在swift中使用removeFromSuperview后iOS随机崩溃
我有UICollectionView,当用户选择单元格时。 我显示放大图像,一个关闭按钮关闭它 我在这里遇到了麻烦,当我用closePreviewMg()函数按下close按钮时,随机崩溃 真的不知道为什么,这是我的密码在swift中使用removeFromSuperview后iOS随机崩溃,ios,crash,Ios,Crash,我有UICollectionView,当用户选择单元格时。 我显示放大图像,一个关闭按钮关闭它 我在这里遇到了麻烦,当我用closePreviewMg()函数按下close按钮时,随机崩溃 真的不知道为什么,这是我的密码 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { self.previewingid =
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.previewingid = FunPics[selectedtab]![indexPath.row]
var alertfrm = UIView();
//alertfrm.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)
alertfrm.frame = self.view.frame
alertfrm.backgroundColor = UIColor(rgb: 0x000000, alpha: 0.8)
alertfrm.tag = 201
var PreviewImg = UIImageView()
PreviewImg.frame = alertfrm.frame
PreviewImg.image = UIImage(named: FunPics[selectedtab]![indexPath.row])
PreviewImg.contentMode = UIViewContentMode.ScaleAspectFit;
PreviewImg.userInteractionEnabled = true
var taptocopy = UITapGestureRecognizer(target: self, action:Selector("CopyIt"))
taptocopy.numberOfTapsRequired = 1;
PreviewImg.addGestureRecognizer(taptocopy)
alertfrm.addSubview(PreviewImg)
var CloseBtn = UIButton.buttonWithType(UIButtonType.System) as UIButton
CloseBtn.titleLabel?.font = UIFont(name: "RobotoSlab-Regular", size: 15)
CloseBtn.backgroundColor = UIColor(rgb: 0x000000, alpha: 0.4)
CloseBtn.layer.cornerRadius = 5
CloseBtn.layer.borderWidth = 1
CloseBtn.layer.borderColor = UIColor(rgb: 0xffffff, alpha: 0.4).CGColor
CloseBtn.frame = CGRectMake(60, 10, 50, 30)
CloseBtn.setTitle("Close", forState: UIControlState.Normal)
CloseBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
CloseBtn.addTarget(self, action: "ClosePreviewImg", forControlEvents: UIControlEvents.TouchUpInside)
alertfrm.addSubview(CloseBtn)
// constraint
//CopyBtn.setTranslatesAutoresizingMaskIntoConstraints(false)
CloseBtn.setTranslatesAutoresizingMaskIntoConstraints(false)
/*
var CC = NSLayoutConstraint(item: CopyBtn, attribute: .CenterX, relatedBy: .Equal, toItem: alertfrm, attribute: .CenterX, multiplier: 0.5, constant: 0)
var CW = NSLayoutConstraint(item: CopyBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0.0, constant: 50)
var CR = NSLayoutConstraint(item: CopyBtn, attribute: .Right, relatedBy: .Equal, toItem: CloseBtn, attribute: .Left, multiplier: 1.0, constant: -5)
var CB = NSLayoutConstraint(item: CopyBtn, attribute: .Bottom, relatedBy: .Equal, toItem: alertfrm, attribute: .Bottom, multiplier: 1.0, constant: -10)
alertfrm.addConstraints([CW,CR,CB])*/
var CC1 = NSLayoutConstraint(item: CloseBtn, attribute: .CenterX, relatedBy: .Equal, toItem: alertfrm, attribute: .CenterX, multiplier: 1.0, constant: 0)
var CW1 = NSLayoutConstraint(item: CloseBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0.0, constant: 50)
//var CL = NSLayoutConstraint(item: CloseBtn, attribute: .Left, relatedBy: .Equal, toItem: CopyBtn, attribute: .Right, multiplier: 1.0, constant: 0)
var CB1 = NSLayoutConstraint(item: CloseBtn, attribute: .Bottom, relatedBy: .Equal, toItem: alertfrm, attribute: .Bottom, multiplier: 1.0, constant: -10)
alertfrm.addConstraints([CC1,CW1,CB1])
self.view.addSubview(alertfrm)
}
func ClosePreviewImg()
{
self.view.viewWithTag(201)?.removeFromSuperview()
NSLog("ClosePreviewImg")
}
这是使用removeFromSuperView()后我的错误日志报告的一部分
(无法全部粘贴,因为太长)
Objective:{Objective 0x79ec0660:+*0x79ea3b50.negerro{id:134}+*0x79ea3b50.posErrorMarker{id:133}+*UIButton:0x79ea1e50'changekey'。宽度{id:10}+*testobeOptimizedTozero{id:848}
10月28日00:14:47*-MacBook-Pro.local KeyboardExt[1832]:***由于未捕获异常“NSInternalinconsistenceException”而终止应用程序,原因:“{Rows:
0x79e94730.negError{id:126}==600+-0.0027778*UIButton:0x79ea1e50'changekey'。宽度{id:10}+1*0x79e94730.posErrorMarker{id:125}
0x79ea3610.negError{id:130}==179+-0.0027778*UIButton:0x79fee8e0'happy'。宽度{id:16}+1*0x79ea3610.posErrorMarker{id:129}
0x79ea3650.negError{id:132}==89+1*0x79EADB0.marker{id:17}+-1*0x79eadd70.marker{id:19}+-1*0x79eb4380.marker{id:122}+1*0x79ea3650.posErrorMarker{id:131}
0x79ea3b90.negError{id:136}==89+1*0x79eade50.marker{id:27}+-1*0x79eadee0.marker{id:29}+-1*0x79eb4380.marker{id:122}+1*0x79ea3b90.posErrorMarker{id:135}
0x79ea3ec0.negError{id:138}==173+-0.0027778*UIButton:0x7a18c2a0'Anger'。宽度{id:36}+1*0x79ea3ec0.posErrorMarker{id:137}
libsystem\u kernel.dylib`\u pthread\u kill:
0x3387694:movl$0x80148,%eax
0x3387699:调用0x33894dc;_sysenter_trap
0x338769e:jae 0x33876ae;_pthread_kill+26
0x33876a0:调用0x33876a5;_pthread_kill+17
0x33876a5:popl%edx
0x33876a6:movl 0x697b(%edx),%edx
0x33876ac:jmpl*%edx
0x33876ae:retl
0x33876af:否
您能为崩溃添加堆栈跟踪吗?堆栈跟踪?崩溃发生在我按下关闭按钮和ClosePreviewMg()后是called@TomSawyer您似乎从superview中删除了标记为201
的视图,但没有删除与该视图相关的NSLayoutConstraint
s,并且在尝试布局视图时,AutoLayout会引发异常。我不这么认为,当移动此视图时,UIKit会自动删除与视图相关的约束视图外层次结构。我意识到这不是你的问题,我几乎只是点击了一下,但我不得不说。请不要硬编码标记。事实上,除了一些情有可原的情况(即:一个VC中有多个alertView响应)外,根本不要使用标记。它很难阅读,以后会引起一系列问题。祝你好运找到你的bug!:)
Objective: {objective 0x79ec0660: <1:-1.55701e-05> + <1:9.85447e-08>*0x79ea3b50.negError{id: 134} + <1:-9.85447e-08>*0x79ea3b50.posErrorMarker{id: 133} + <1:0>*UIButton:0x79ea1e50'changekey'.Width{id: 10} + <1:1>*tempToBeOptimizedToZero{id: 848}}
Oct 28 00:14:47 *-MacBook-Pro.local KeyboardExt[1832]: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<NSISEngine: 0x79eaefd0>{ Rows:
0x79e94730.negError{id: 126} == 600 + -0.00277778*UIButton:0x79ea1e50'changekey'.Width{id: 10} + 1*0x79e94730.posErrorMarker{id: 125}
0x79ea3610.negError{id: 130} == 179 + -0.00277778*UIButton:0x79fee8e0'happy'.Width{id: 16} + 1*0x79ea3610.posErrorMarker{id: 129}
0x79ea3650.negError{id: 132} == 89 + 1*0x79eadbd0.marker{id: 17} + -1*0x79eadd70.marker{id: 19} + -1*0x79eb4380.marker{id: 122} + 1*0x79ea3650.posErrorMarker{id: 131}
0x79ea3b90.negError{id: 136} == 89 + 1*0x79eade50.marker{id: 27} + -1*0x79eadee0.marker{id: 29} + -1*0x79eb4380.marker{id: 122} + 1*0x79ea3b90.posErrorMarker{id: 135}
0x79ea3ec0.negError{id: 138} == 173 + -0.00277778*UIButton:0x7a18c2a0'angry'.Width{id: 36} + 1*0x79ea3ec0.posErrorMarker{id: 137}
libsystem_kernel.dylib`__pthread_kill:
0x3387694: movl $0x80148, %eax
0x3387699: calll 0x33894dc ; _sysenter_trap
0x338769e: jae 0x33876ae ; __pthread_kill + 26
0x33876a0: calll 0x33876a5 ; __pthread_kill + 17
0x33876a5: popl %edx
0x33876a6: movl 0x697b(%edx), %edx
0x33876ac: jmpl *%edx
0x33876ae: retl
0x33876af: nop