iOS Question [OBJ-C] Rising Event from a class

Pxs

Member
Licensed User
Hello

I'm trying to get a rise event call from objective C to a b4i sub

This works:
B4X:
Sub nsdata_sub(x As NativeObject)
Dim NSS As String = x
end sub


#If ObjC
- (void)test{
        uint8_t bytes[] = {1,2,3,6,7,8,11,12,13};
        NSData *data = [NSData dataWithBytes:bytes length:sizeof(bytes)];
        [self.bi raiseUIEvent:nil event:@"nsdata_sub:" params:@[data]];    
}
#end if

However, trying the same within the implementation of a class, doesnt

B4X:
@end
#import <Foundation/Foundation.h>
#import <CoreNFC/CoreNFC.h>
typedef void(^CompletionHandler)(NSError *error);
@interface B4INFC:NSObject <NFCTagReaderSessionDelegate>
@end
@implementation B4INFC


- (void)test{
  NSData *data = nil;

   // [B4IObjectWrapper raiseEvent:self :@"nsdata_sub:" params:@[data]];
    [B4IObjectWrapper raiseUIEvent:self :@"nsdata_sub:" :@[data]];
    //compiles ok but does nothing
}

Whats the correct way to handle this?

already tried raiseEventFromDifferentThread, raiseEvent...same result
Thanks
 
Solution
Your code cannot work. There is no relation between the B4INFC class code and any other class instance.

Working example:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Panel1 As B4XView
    Private nfc As NativeObject
End Sub

Public Sub Initialize
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    nfc = nfc.Initialize("B4INFC").RunMethod("new", Null)
    nfc.RunMethod("initialize:", Array(Me))
End Sub


Private Sub Button1_Click
    nfc.RunMethod("test", Null)
End Sub

Private Sub NSData_Sub(Data As Object)
    Log(Sender & ": " & Data)
End Sub

#if OBJC
@end
#import...

Erel

B4X founder
Staff member
Licensed User
Longtime User
Your code cannot work. There is no relation between the B4INFC class code and any other class instance.

Working example:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Panel1 As B4XView
    Private nfc As NativeObject
End Sub

Public Sub Initialize
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    nfc = nfc.Initialize("B4INFC").RunMethod("new", Null)
    nfc.RunMethod("initialize:", Array(Me))
End Sub


Private Sub Button1_Click
    nfc.RunMethod("test", Null)
End Sub

Private Sub NSData_Sub(Data As Object)
    Log(Sender & ": " & Data)
End Sub

#if OBJC
@end
#import <Foundation/Foundation.h>
#import <CoreNFC/CoreNFC.h>
typedef void(^CompletionHandler)(NSError *error);
@interface B4INFC:NSObject <NFCTagReaderSessionDelegate>
@end
@implementation B4INFC {
    B4I* bi;
}
- (void)initialize:(B4IClass*) me {
    self->bi = me.bi;
}

- (void)test{
  NSData *data = nil;
    [self->bi raiseUIEvent:self event:@"nsdata_sub:" params:@[[B4I nilToNSNull:data]]];
}
#End If
 
  • Thank you
Reactions: Pxs
Upvote 1
Solution
Top