代表們在斯威夫特?一個人如何去做一個代表?NSUserNotificationCenterDelegate在斯威夫特?
3 回答

慕仙森
TA貢獻1827條經(jīng)驗 獲得超8個贊
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條經(jīng)驗 獲得超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.
這個 協(xié)議
,它定義了工人需要做的事情。 這個 老板班
它有一個委托變量,它用來告訴Worker類要做什么。 這個 工人階級
它采用了協(xié)議,并執(zhí)行了所需的操作。
現(xiàn)實生活
通訊
一個類需要向另一個類發(fā)送一些信息。 定制化
一個類希望允許另一個類自定義它。
再來一個音符
weak
- 3 回答
- 0 關(guān)注
- 529 瀏覽
添加回答
舉報
0/150
提交
取消