// 文字列を指定してクラスオブジェクト/セレクタを取得する。最初に一回宣言しておけばよい。
Declare Function NSClassFromString Lib "Cocoa" (aClassName As CFStringRef) As Ptr
Declare Function NSSelectorFromString Lib "Cocoa" (aSelName As CFStringRef) As Ptr
// Declare宣言
Declare Function objc_allocateClassPair Lib "Cocoa" (superclass As Ptr, name As CString, extraBytes As Integer) as Ptr
Declare Sub objc_registerClassPair Lib "Cocoa" (cls As Ptr)
Declare Function class_addMethod Lib "Cocoa" (cls As Ptr, name As Ptr, imp As Ptr, types As CString) As Boolean
// クラス名をmyNSUserNotificationCenterDelegate(名前は任意。少なくとも今回のケースでは参照されない。)、メタクラス名をNSObjectにして、生成
Dim newClassId As Ptr = objc_allocateClassPair(NSClassFromString("NSObject"), "myNSUserNotificationCenterDelegate", 0)
// ランタイムに登録(参照を可能とするため)
objc_registerClassPair newClassId
// Delegateの対象となるメソッドを追加(userNotificationCenter:shouldPresentNotification:をXojo側で用意したShouldPresentNotificationメソッドで受け取る。)
if not class_addMethod (newClassId, NSSelectorFromString("userNotificationCenter:shouldPresentNotification:"), AddressOf ShouldPresentNotification, "B@:@@") then
msgBox "error."
return
end if
// 上記で生成したクラスのインスタンスを作成
Declare Function alloc Lib "Cocoa" selector "alloc" (class_id As Ptr) As Ptr
Declare Function init Lib "Cocoa" selector "init" (obj_id As Ptr) As Ptr
Dim delegateId As Ptr = init(alloc(newClassId))
…
// Delegateを設定したいインスタンスに、上記で作成したインスタンスをセット
Declare Sub setDelegate Lib "Cocoa" Selector "setDelegate:" (receiver As Ptr, id As Ptr)
setDelegate center, delegateId // centerはDelegateを設定したいインスタンスで、別途取得しておく
注)class_addMethodの最後の引数("B@:@@")は「type encodings」で、例えば以下を参照。(メソッド名: SendNotification(注:名前は任意)
Declare Function NSClassFromString Lib "Cocoa" (aClassName As CFStringRef) As Ptr
Declare Function NSSelectorFromString Lib "Cocoa" (aSelName As CFStringRef) As Ptr
// Delegateを設定するための仕込み ----------
Declare Function objc_allocateClassPair Lib "Cocoa" (superclass As Ptr, name As CString, extraBytes As Integer) as Ptr
Declare Sub objc_registerClassPair Lib "Cocoa" (cls As Ptr)
Declare Function class_addMethod Lib "Cocoa" (cls As Ptr, name As Ptr, imp As Ptr, types As CString) As Boolean
Dim newClassId As Ptr = objc_allocateClassPair(NSClassFromString("NSObject"), "myNSUserNotificationCenterDelegate", 0)
objc_registerClassPair newClassId
if not class_addMethod (newClassId, NSSelectorFromString("userNotificationCenter:shouldPresentNotification:"), AddressOf ShouldPresentNotification, "B@:@@") then
msgBox "error."
return
end if
Declare Function alloc Lib "Cocoa" selector "alloc" (class_id As Ptr) As Ptr
Declare Function init Lib "Cocoa" selector "init" (obj_id As Ptr) As Ptr
Dim delegateId As Ptr = init(alloc(newClassId))
// ----------
// 以下は(追加分と書かれた行を除いて)前回と同じ
Dim notification As Ptr = NSClassFromString("NSUserNotification")
Declare Function notificationAlloc Lib "Cocoa" Selector "alloc" (receiver As Ptr) As Ptr
notification = notificationAlloc(notification)
Declare Function notificationInit Lib "Cocoa" Selector "init" (receiver As Ptr) As Ptr
notification = notificationInit(notification)
Declare Sub setTitle Lib "Cocoa" Selector "setTitle:" (receiver As Ptr, id As CFStringRef)
setTitle(notification, "Hello World")
Declare Sub setInformativeText Lib "Cocoa" Selector "setInformativeText:" (receiver As Ptr, id As CFStringRef)
setInformativeText(notification, "my message")
Dim date1 As Ptr = NSClassFromString("NSDate")
Declare Function date Lib "Cocoa" Selector "date" (receiver As Ptr) As Ptr
date1 = date(date1)
Dim date2 As Ptr = NSClassFromString("NSDate")
Declare Function dateWithTimeInterval Lib "Cocoa" Selector "dateWithTimeInterval:sinceDate:" (receiver As Ptr, id1 As Double, id2 As Ptr) As Ptr
date2 = dateWithTimeInterval(date2,5,date1)
Declare Sub setDeliveryDate Lib "Cocoa" Selector "setDeliveryDate:" (receiver As Ptr, id As Ptr)
setDeliveryDate(notification, date2)
Declare Sub setSoundName Lib "Cocoa" Selector "setSoundName:" (receiver As Ptr, id As CFStringRef)
setSoundName(notification, "DefaultSoundName")
Dim center As Ptr = NSClassFromString("NSUserNotificationCenter")
Declare Function defaultUserNotificationCenter Lib "Cocoa" Selector "defaultUserNotificationCenter" (receiver As Ptr) As Ptr
center = defaultUserNotificationCenter(center)
Declare Sub setDelegate Lib "Cocoa" Selector "setDelegate:" (receiver As Ptr, id As Ptr) // 追加分
setDelegate center, delegateId // 追加分
Declare Sub scheduleNotification Lib "Cocoa" Selector "scheduleNotification:" (receiver As Ptr, id As Ptr)
scheduleNotification(center, notification)
上記メソッドを、(例えば)Windowに置いたPushButtonのActionイベントから呼び出します。