我有UICollectionView,当用户选择单元格时。 我显示放大图像,一个关闭按钮关闭它。
我在这里遇到麻烦,当我用ClosePreviewImg()函数按下关闭按钮时,我随机崩溃。
真的不知道为什么,这是我的代码
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: <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