iOS Question Call B4i sub from inline OBJC

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
Private Sub Application_Start (Nav As NavigationController)
   NavControl = Nav
   Page1.Initialize("Page1")
   Nav.ShowPage(Page1)
   Dim no As NativeObject = Me
   no.RunMethod("methodThatRaiseEvent", Null)
End Sub

Sub Some_Sub(x As Int)
   Log(x)
End Sub

#If OBJC
- (void)methodThatRaiseEvent {
   [self.bi raiseEvent:nil event:@"some_sub:" params:@[@10]];
}
#End If

Note that the sub name in the raiseEvent call must be lower cased.
 
Upvote 0

Derek Johnson

Active Member
Licensed User
Longtime User
Here:
B4X:
Private Sub Application_Start (Nav As NavigationController)
   NavControl = Nav
   Page1.Initialize("Page1")
   Nav.ShowPage(Page1)
   Dim no As NativeObject = Me
   no.RunMethod("methodThatRaiseEvent", Null)
End Sub

Sub Some_Sub(x As Int)
   Log(x)
End Sub

#If OBJC
- (void)methodThatRaiseEvent {
   [self.bi raiseEvent:nil event:@"some_sub:" params:@[@10]];
}
#End If

I noticed that when I tried this example that I got this warning in the log:

Unexpected event (missing RaisesSynchronousEvents):some_sub:

Could you tell me please, is there some declaration needed to get rid of this warning?
 
Upvote 0

Derek Johnson

Active Member
Licensed User
Longtime User
Here's the actual code and the responses in the log:

B4X:
Sub Button2_Click
    log("Clicked Me")
    Try
         Dim NativeMe As NativeObject = Me
         NativeMe.RunMethod("test:", Array (True))
    Catch
        rlog("Last exception:" & LastException.Description)
    End Try
End Sub

#If OBJC

- (void) test: (bool) on {
   NSLog(@"this is a message");
   [self.bi raiseEvent:nil event:@"olog:" params:@[@"Testing.."]];
}

#End If

Sub olog(msg As String)
    Log(msg)
    TextView1.Text=TextView1.Text & msg  & CRLF
End Sub

This is the log;

Application_Start
Application_Active
Application_Active
Clicked Me
this is a message
Unexpected event (missing RaisesSynchronousEvents): olog:
Testing..
 
Upvote 0

wineos

Member
Licensed User
Longtime User
B4X:
[self.bi raiseEvent:nil event:@"some_sub:" params:@[@10]];

If I want to call an event without parameter, how to change the above code?

Thank you.
 
Upvote 0

wineos

Member
Licensed User
Longtime User
Thanks Narek,

May I ask once more: If I want to call an event with more than 1 parameter, then how to write it?

Thank you.
 
Upvote 0

wineos

Member
Licensed User
Longtime User
Another question.

B4X:
[self.bi raiseEvent:nil event:@"some_sub:" params:@[@10]];

I change the parameter to 1, i.e.

B4X:
[self.bi raiseEvent:nil event:@"some_sub:" params:@[@1]];

When the code run to

B4X:
Sub Some_Sub(x As Int)

the system prompted error, saying that it is not an array. But when I change the parameter to 10, it works.

Anyone can help?

Thank you.
 
Upvote 0

wineos

Member
Licensed User
Longtime User
Thank you Narek, I solve it, just like what you did.

B4X:
[self.bi raiseEvent:nil event:@"some_sub:" params:@[@((int)1)]];
 
Upvote 0
Top