iOS Question This OC code how to change b4i

chjk

Member
Licensed User
Longtime User
B4X:
//Listen

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(orientationChanged)

name:UIDeviceOrientationDidChangeNotification

object:nil];

//events

-(void) orientationChanged

{

switch ([[UIDevice currentDevice]orientation]) {

case  UIDeviceOrientationPortrait:

NSLog(@"portrait");

break;

case  UIDeviceOrientationPortraitUpsideDown:

NSLog(@"portraitUpSideDown");

break;

case  UIDeviceOrientationLandscapeLeft:

NSLog(@"landscapeLeft");

break;

case  UIDeviceOrientationLandscapeRight:

NSLog(@"landscapeRight");

break;

case  UIDeviceOrientationFaceDown:

NSLog(@"facedown!!");

break;

case  UIDeviceOrientationFaceUp:

NSLog(@"FaceUp");

break;

default:

break;

}

}
Thank everyone!
 

chjk

Member
Licensed User
Longtime User
The Page_Resize event will be raised when the orientation changes. You can check the orientation in Page_Resize with the code from that link.
Thanks! but I set Landscape mode, Page_Resize is not change
#iPhoneOrientations: LandscapeLeft, LandscapeRight
#iPadOrientations:LandscapeLeft, LandscapeRight
 
Upvote 0

Erel

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

#if OBJC
- (void)addListener {
   [[NSNotificationCenter defaultCenter] addObserver:self
     selector:@selector(_orientation_changed)
     name:UIDeviceOrientationDidChangeNotification object:nil];
}
#end if

Private Sub Orientation_Changed
   Log($"Current orientation: ${DeviceOrientation}"$)
End Sub

Private Sub DeviceOrientation As String
  Dim no As NativeObject
  Dim o As Int = no.Initialize("UIDevice").RunMethod("currentDevice", Null).RunMethod("orientation", Null).AsNumber
  Select o
  Case 0
  Return "Unknown"
  Case 1
  Return "Portrait"
  Case 2
  Return "PortraitUpsideDown"
  Case 3
  Return "LandscapeLeft"
  Case 4
  Return "LandscapeRight"
  Case 5
  Return "FaceUp"
  Case 6
  Return "FaceDown"
  End Select
  Return "Unknown"
End Sub
 
Upvote 0

chjk

Member
Licensed User
Longtime User
Thank you very much!!! I wrote this in the first.
B4X:
Dim no As NativeObject
    no.Initialize("NSNotificationCenter").RunMethod("defaultCenter", Null) _
     .RunMethod("addObserver:self selector:@selector(orientationChanged) name:UIDeviceOrientationDidChangeNotification    object:nil", _
       Null)
it's error, so come to help!
 
Upvote 0
Top