Skip to content

UIAlertView与UIActionSheet的类扩展,可使用Bolck完成回调,无需实现代理方法

Notifications You must be signed in to change notification settings

DrewRoy/UIExtension

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

UIExtension

UIAlertView与UIActionSheet的类扩展,可使用Bolck完成回调,无需实现代理方法

使用方法

UIAlertView+Block.swiftUIActionSheet+Block.swift文件加入项目 使用构造方法创建UIAlertView或UIActionSheet(使用原生构造方法亦可):

let alertView = UIAlertView(title: "title", message: "message", style: .Default, cancelButtonTitle: "cancel", otherButtonTitles: ["btn1","btn2"]) { (alertView, buttonIndex) in
	print("tapBlock")
}
alertView.show()

let actionsheet = UIActionSheet(title: "title", cancelButtonTitle: "cancel", destructiveButtonTitle: "destructive", otherButtonTitles: ["btn1","btn2"]) { (actionSheet, buttonIndex) in
	print("tapBlock")
}
actionsheet.showInView(self.view)
        

构造方法后缀的Block为tapBlock,对应的代理方法为clickedButtonAtIndex。每个代理方法都对应一个Block。其中:

tapBlock : 对应 clickedButtonAtIndex方法

willPresentBlock : 对应 willPresentAlertViewwillPresentActionSheet方法

didPresentBlock : 对应 didPresentAlertViewdidPresentActionSheet方法

willDismissBlock : 对应 willDismissWithButtonIndex方法

didDismissBlock : 对应 didDismissWithButtonIndex方法

cancelBlock : 对应 alertViewCancelactionSheetCancel方法

shouldEnableFirstOtherButtonBlock : 对应 alertViewShouldEnableFirstOtherButton方法

为alertView或actionSheet添加Block:

alertView.didDismissBlock { (alertView, buttonIndex) in
            // do something
}
actionsheet.cancelBlock { (actionSheet) in
            // do something
}
actionsheet.didDismissBlock { (actionSheet, buttonIndex) in
            // do something
}

注意

若指定代理并实现代理方法,除alertViewShouldEnableFirstOtherButton方法外支持既执行block回调也执行代理方法回调,alertViewShouldEnableFirstOtherButton方法只能实现一个回调,并以Block回调优先。

About

UIAlertView与UIActionSheet的类扩展,可使用Bolck完成回调,无需实现代理方法

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages