|
| 1 | +# LiveCode Builder Language |
| 2 | +## Objective-C delegate support |
| 3 | +Handlers `CreateObjcDelegate` and `CreateObjcDelegateWithContext` have |
| 4 | +been added to the objc module which allow the creation of custom |
| 5 | +delegate objects with LCB implementations of protocol methods. |
| 6 | + |
| 7 | +In order to create a delegate to handle a particular protocol method, |
| 8 | +pass in the protocol name as the first argument and the mapping from |
| 9 | +method names to LCB handlers as the second argument. For example, to |
| 10 | +create a selectionChanged message for an `NSTextView`, we need to create |
| 11 | +a handler |
| 12 | + |
| 13 | + private handler DidChangeSelection(in pNotification as ObjcObject) returns nothing |
| 14 | + post "selectionChanged" |
| 15 | + end handler |
| 16 | + |
| 17 | +and create a `NSTextViewDelegate`: |
| 18 | + |
| 19 | + variable tDelegate as optional ObjcObject |
| 20 | + put CreateObjcDelegate( \ |
| 21 | + "NSTextViewDelegate", \ |
| 22 | + {"textViewDidChangeSelection:": DidChangeSelection}, \ |
| 23 | + ) into tDelegate |
| 24 | + if tDelegate is not nothing then |
| 25 | + put tDelegate into mTextViewDelegate |
| 26 | + end if |
| 27 | + |
| 28 | +Optionally, a context parameter can be passed in at delegate creation |
| 29 | +time: |
| 30 | + |
| 31 | + put CreateObjcDelegateWithContext( \ |
| 32 | + "NSTextViewDelegate", \ |
| 33 | + {"textViewDidChangeSelection:": DidChangeSelectionContext}, \ |
| 34 | + tContext) into tDelegate |
| 35 | + |
| 36 | + if tDelegate is not nothing then |
| 37 | + put tDelegate into mTextViewDelegate |
| 38 | + end if |
| 39 | + |
| 40 | +In this case the context variable will be passed as first argument of |
| 41 | +the corresponding LCB callback: |
| 42 | + |
| 43 | + private handler DidChangeSelectionContext(in pContext, in pNotification as ObjcObject) returns nothing |
| 44 | + post "selectionChanged" with [pContext] |
| 45 | + end handler |
| 46 | + |
| 47 | +Some protocols consist of purely optional methods. In this case the |
| 48 | +information about the protocol's methods are not available from the |
| 49 | +objective-c runtime API. For this eventuality there are also handlers |
| 50 | +`CreateObjcInformalDelegate` and `CreateObjcInformalDelegateWithContext`. |
| 51 | + |
| 52 | +These handlers take a list of foreign handlers as their first argument |
| 53 | +instead of a protocol name. The foreign handlers' information is used to |
| 54 | +resolve incoming selectors so that the desired LCB callback is called. |
| 55 | +For example the `NSSoundDelegate` protocol has only one method, and it |
| 56 | +is optional, |
| 57 | + |
| 58 | + - (void)sound:(NSSound *)sound didFinishPlaying:(BOOL)aBool; |
| 59 | + |
| 60 | +So in order to create an `NSSoundDelegate`, we need to create a list of |
| 61 | +foreign handlers, in this case just the following: |
| 62 | + |
| 63 | + foreign handler NSSoundDidFinishPlaying(in pSound as ObjcId, in pDidFinish as CSChar) binds to "objc:.-sound:didFinishPlaying:" |
| 64 | + |
| 65 | +and create the informal delegate |
| 66 | + |
| 67 | + handler DidSoundFinish(in pSound as ObjcId, in pDidFinish as Boolean) returns nothing |
| 68 | + if pDidFinish then |
| 69 | + post "soundFinished" |
| 70 | + end if |
| 71 | + end handler |
| 72 | + |
| 73 | + foreign handler Objc_SetSoundDelegate(in pSound as ObjcId, in pDelegate as ObjcId) returns nothing \ |
| 74 | + binds to "objc:NSSound.-setDelegate:" |
| 75 | + ... |
| 76 | + |
| 77 | + variable tDelegate as optional ObjcObject |
| 78 | + put CreateObjcInformalDelegate( \ |
| 79 | + [NSSoundDidFinishPlaying], \ |
| 80 | + {"textViewDidChangeSelection:": DidChangeSelection}) \ |
| 81 | + into tDelegate |
| 82 | + end if |
| 83 | + if tDelegate is not nothing then |
| 84 | + put tDelegate into mSoundDelegate |
| 85 | + Objc_SetSoundDelegate(tSound, tDelegate) |
| 86 | + end if |
| 87 | + |
| 88 | +> *Note:* Delegate properties are usually 'assigned' rather than |
| 89 | +> 'retained', so it is necessary to store them in module variables |
| 90 | +> until they are no longer needed. Generally the pattern required is |
| 91 | +> as follows: |
| 92 | +
|
| 93 | + handler OnOpen() |
| 94 | + -- Create native view and set native layer |
| 95 | + -- Set native view delegate property |
| 96 | + -- Store view and delegate in module vars |
| 97 | + end handler |
| 98 | + |
| 99 | + handler OnClose() |
| 100 | + -- Set native view delegate property to nothing |
| 101 | + -- Put nothing into view and delegate module vars |
| 102 | + -- Set native layer to nothing |
| 103 | + end handler |
0 commit comments