代表们在斯威夫特?一个人如何去做一个代表?NSUserNotificationCenterDelegate在斯威夫特?
3 回答
慕仙森
TA贡献1827条经验 获得超7个赞
class MyClass: NSUserNotificationCenterDelegate
// NSUserNotificationCenterDelegate implementationfunc userNotificationCenter(center: NSUserNotificationCenter, didDeliverNotification notification: NSUserNotification) { //implementation}func userNotificationCenter(center: NSUserNotificationCenter, didActivateNotification notification: NSUserNotification) { //implementation}func userNotificationCenter(center: NSUserNotificationCenter, shouldPresentNotification notification: NSUserNotification) -> Bool { //implementation return true}
NSUserNotificationCenter.defaultUserNotificationCenter().delegate = self;
慕标5832272
TA贡献1966条经验 获得超4个赞
很久以前.。
// MARK: Background to the story// A protocol is like a list of rules that need to be followed.protocol OlderSiblingDelegate: class { // The following command (ie, method) must be obeyed by any // underling (ie, delegate) of the older sibling. func getYourNiceOlderSiblingAGlassOfWater()} // MARK: Characters in the storyclass BossyBigBrother { // I can make whichever little sibling is around at // the time be my delegate (ie, slave) weak var delegate: OlderSiblingDelegate? func tellSomebodyToGetMeSomeWater() { // The delegate is optional because even though // I'm thirsty, there might not be anyone nearby // that I can boss around. delegate?.getYourNiceOlderSiblingAGlassOfWater() }}// Poor little sisters have to follow (or at least acknowledge) // their older sibling's rules (ie, protocol)class PoorLittleSister: OlderSiblingDelegate { func getYourNiceOlderSiblingAGlassOfWater() { // Little sis follows the letter of the law (ie, protocol), // but no one said exactly how she had to respond. print("Go get it yourself!") }}// MARK: The Story// Big bro is laying on the couch watching basketball on TV.let bigBro = BossyBigBrother() // He has a little sister named Sally.let sally = PoorLittleSister()// Sally walks into the room. How convenient! Now big bro // has someone there to boss around.bigBro.delegate = sally// So he tells her to get him some water.bigBro.tellSomebodyToGetMeSomeWater() // Unfortunately no one lived happily ever after...// The end.
这个 协议
,它定义了工人需要做的事情。 这个 老板班
它有一个委托变量,它用来告诉Worker类要做什么。 这个 工人阶级
它采用了协议,并执行了所需的操作。
现实生活
通讯
一个类需要向另一个类发送一些信息。 代码示例1: 代码示例2: 定制化
一个类希望允许另一个类自定义它。
再来一个音符
weak
- 3 回答
- 0 关注
- 487 浏览
添加回答
举报
0/150
提交
取消