这是典型的UIAlertVIEw的样子:
这就是我想要的样子:
这是我目前正在制作我的自定义ConfirmationVIEwControllerd出窗口的方法:
let confirmationVIEwController = ConfirmationVIEwController()confirmationVIEwController.delegate = selfconfirmationVIEwController.setTitleLabel("Are you sure you want to remove \(firstname)?")confirmationVIEwController.modalPresentationStyle = UIModalPresentationStyle.PopoverconfirmationVIEwController.preferredContentSize = CGSizeMake(230,130)let popoverConfirmationVIEwController = confirmationVIEwController.popoverPresentationControllerpopoverConfirmationVIEwController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)popoverConfirmationVIEwController?.delegate = selfpopoverConfirmationVIEwController?.sourceVIEw = self.vIEwpopoverConfirmationVIEwController?.sourceRect = CGRectMake(CGRectGetMIDX(self.vIEw.bounds),CGRectGetMIDY(self.vIEw.bounds),0)presentVIEwController( confirmationVIEwController,animated: true,completion: nil)
这是我按下CANCEL或REMOVE按钮时收到通知的方式:
extension UserProfiletableVIEwController: ConfirmationVIEwControllerDelegate { func cancelbuttonpressed() { print("Cancel button pressed") } func confirmationbuttonpressed(objectToDelete: AnyObject?) { print("Delete button pressed") }}
但是,我喜欢使用UIAlertVIEw是因为我可以在按下特定按钮时执行我想要执行的 *** 作的硬编码,如下所示:
let alertController = UIAlertController(Title: nil,message: nil,preferredStyle: .Alert)let cancelAction = UIAlertAction(Title: "Cancel",style: .Default,handler: {(ACTION) in print("Perform cancel action")})let deleteAction = UIAlertAction(Title: "Remove",style: .Destructive,handler: {(ACTION) in print("Perform delete action")})alertController.addAction(cancelAction)alertController.addAction(deleteAction)presentVIEwController(alertController,completion: nil)
所以我的问题是,如何以这样的方式创建一个完成处理程序(内联),当我使用我的自定义ConfirmationVIEwController按下CANCEL或REMOVE按钮时,我可以触发动作,就像我已经展示了如何使用UIAlertController一样而不是目前的方式,我正在与代表团这样做?
答案是使用UIAlertController创建我正在寻找的自定义d出窗口吗?如果是这样,我怎样才能将其定制到我正在寻找的程度?
在此先感谢并为长篇帖子抱歉:)
附:这是我的ConfirmationVIEwController和ConfirmationVIEwControllerDelegate的样子:
protocol ConfirmationVIEwControllerDelegate { func cancelbuttonpressed() func confirmationbuttonpressed(objectToDelete: AnyObject?)}class ConfirmationVIEwController: UIVIEwController { var dIDSetupConstraints = false let TitleLabel = UILabel.newautoLayoutVIEw() let buttonContainer = UIVIEw.newautoLayoutVIEw() let cancelbutton = buttonWithPressingEffect.newautoLayoutVIEw() let confirmationbutton = buttonWithPressingEffect.newautoLayoutVIEw() var delegate: ConfirmationVIEwControllerDelegate? var objectToDelete: AnyObject? overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() vIEw.backgroundcolor = UIcolor.whitecolor() TitleLabel.numberOflines = 0 cancelbutton.backgroundcolor = UIcolor.colorFromCode(0x7f7f7f) cancelbutton.layer.cornerRadius = 5 cancelbutton.setAttributedTitle(NSMutableAttributedString( string: "CANCEL",attributes: [ NSFontAttributename: UIFont(name: "AvenirNextLTPro-Demi",size: 12)!,NSForegroundcolorAttributename: UIcolor.whitecolor(),NSKernAttributename: 0.2 ] ),forState: UIControlState.normal) cancelbutton.addTarget(self,action: #selector(cancelbuttonpressed),forControlEvents: .touchUpInsIDe) confirmationbutton.backgroundcolor = Application.redcolor confirmationbutton.layer.cornerRadius = 5 confirmationbutton.setAttributedTitle(NSMutableAttributedString( string: "REMOVE",forState: UIControlState.normal) confirmationbutton.addTarget(self,action: #selector(confirmationbuttonPresssed),forControlEvents: .touchUpInsIDe) vIEw.addSubvIEw(TitleLabel) vIEw.addSubvIEw(buttonContainer) buttonContainer.addSubvIEw(cancelbutton) buttonContainer.addSubvIEw(confirmationbutton) updateVIEwConstraints() } func cancelbuttonpressed() { delegate?.cancelbuttonpressed() dismissVIEwControllerAnimated(false,completion: nil) } func confirmationbuttonPresssed() { delegate?.confirmationbuttonpressed(objectToDelete) dismissVIEwControllerAnimated(false,completion: nil) } func setTitleLabel(text: String) { let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = NSTextAlignment.Center paragraphStyle.linespacing = 4.5 TitleLabel.attributedText = NSMutableAttributedString( string: text,attributes: [ NSFontAttributename: UIFont(name: "AvenirNextLTPro-Regular",size: 14)!,NSForegroundcolorAttributename: UIcolor.colorFromCode(0x151515),NSKernAttributename: 0.5,NSParagraphStyleAttributename: paragraphStyle ] ) } overrIDe func updateVIEwConstraints() { if !dIDSetupConstraints { TitleLabel.autopinEdgesToSupervIEwEdgesWithInsets(UIEdgeInsets(top: 10,left: 10,bottom: 0,right: 10),excludingEdge: .Bottom) TitleLabel.autoAlignAxisToSupervIEwAxis(.Vertical) buttonContainer.autopinEdge(.top,toEdge: .Bottom,ofVIEw: TitleLabel,withOffset: 3) buttonContainer.autoAlignAxisToSupervIEwAxis(.Vertical) buttonContainer.autopinEdgetoSupervIEwEdge(.Bottom,withInset: 10) let contactVIEws: NSArray = [cancelbutton,confirmationbutton] contactVIEws.autodistributeVIEwsAlongAxis(.Horizontal,alignedTo: .Horizontal,withFixedSpacing: 7,insetSpacing: true,matchedSizes: false) cancelbutton.autopinEdgetoSupervIEwEdge(.top) cancelbutton.autopinEdgetoSupervIEwEdge(.Bottom) cancelbutton.autoSetDimensionsToSize(CGSize(wIDth: 100,height: 50)) confirmationbutton.autopinEdgetoSupervIEwEdge(.top) confirmationbutton.autopinEdgetoSupervIEwEdge(.Bottom) confirmationbutton.autoSetDimensionsToSize(CGSize(wIDth: 100,height: 50)) dIDSetupConstraints = true } super.updateVIEwConstraints() }}解决方法 像下面这样的东西应该允许它.请注意,可以进行相当多的改进.例如,您可以对要删除的对象使用泛型而不是AnyObject.如果你无论如何都传递了内联闭包,你也不一定需要传入它,所以你可能只是删除它.
你也可以使你的按钮更可重复使用,而不是硬编码取消和删除,但现在我们离开主题:)
class ConfirmVIEwController : UIVIEwController { var onCancel : (() -> VoID)? var onConfirm : ((AnyObject?) -> VoID)? var objectToDelete : AnyObject? func cancelbuttonpressed() { // defered to ensure it is performed no matter what code path is taken defer { dismissVIEwControllerAnimated(false,completion: nil) } let onCancel = self.onCancel // deliberately set to nil just in case there is a self reference self.onCancel = nil guard let block = onCancel else { return } block() } func confirmationbuttonPresssed() { // defered to ensure it is performed no matter what code path is taken defer { dismissVIEwControllerAnimated(false,completion: nil) } let onConfirm = self.onConfirm // deliberately set to nil just in case there is a self reference self.onConfirm = nil guard let block = onConfirm else { return } block(self.objectToDelete) }}let confirm = ConfirmVIEwController()confirm.objectToDelete = NSObject()confirm.onCancel = { // perform some action here}confirm.onConfirm = { objectToDelete in // delete your object here}总结
以上是内存溢出为你收集整理的ios – Swift Custom UIAlertView全部内容,希望文章能够帮你解决ios – Swift Custom UIAlertView所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)