iOS Question When loading a new Layout, the Labels are not initialized

semar

Active Member
Licensed User
Longtime User
Hi all,

I have a project I'm converting from B4A to B4i.

I load the main page using the following statements:

B4X:
NavControl = Nav
.
.
pageMain.Initialize("pageMain")
pageMain.Title = "pageMain"
pageMain.RootPanel.Color = Colors.White
pageMain.RootPanel.LoadLayout("main")
NavControl.ShowPage(pageMain)

Later I want to load another layout, and the statements are similar:
B4X:
Activity.RemoveAllViews
Activity.LoadLayout("training_options")
Activity.Title = "Training Options"

lbl_goMenu.BringToFront ' <--- here occurs the error, even if the label was initialized with a text inside the designer

The code of the module "Activity" (which is merely a wrapper to retain the B4A statements) is:
B4X:
Sub RemoveAllViews
   Main.NavControl.RemoveCurrentPage

End Sub

Sub loadLayout(layout As String)
   Main.pageMain.Initialize(layout)
   Main.pageMain.Title = "page" & layout
   Main.pageMain.RootPanel.Color = Colors.White
   Main.pageMain.RootPanel.LoadLayout(layout)
   Main.NavControl.ShowPage(Main.pageMain)
End Sub

The runtime error occurs at the statement marked with an arrow.
It complains that the label was not initialized.
The same error is arised also with all the other controls (panels, etc.) of the loaded Layout. The Layout is however displayed after the error.

Should I initialize all the controls of a Layout in order to load it ? With B4A is not needed..

Am I missing something obvious here ?

Regards,
Sergio
 
Last edited:

imbault

Well-Known Member
Licensed User
Longtime User
Are the label and other controls from the layout are Declared in Process_Globals of your module?
 
Upvote 0

semar

Active Member
Licensed User
Longtime User
Here is a simple project, I can't figure out why the second layout will not opened.
It uses a module I called "Activity" just to preserve the B4A code.

Main:

B4X:
'Code module
#Region  Project Attributes
   #ApplicationLabel: layout_test
   #Version: 1.0.0
   'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
   #iPhoneOrientations: Portrait
   #iPadOrientations: Portrait
   #Target: iPhone, iPad
   #ATSEnabled: True
   #MinVersion: 7
#End Region

Sub Process_Globals
   Public App As Application
   Public NavControl As NavigationController
   Public pageMain As Page
   Private Button1 As Button
   Private btn_back_to_main As Button
End Sub


Private Sub Application_Start (Nav As NavigationController)

   NavControl = Nav
   showMainMenu
  
End Sub

Sub showMainMenu()
  
 
   Activity.loadLayout("main")
  
End Sub

Sub Button1_Click
   Activity.loadLayout("layout_2")
End Sub

Sub btn_back_to_main_Click
   Activity.loadLayout("main")
End Sub

Activity:

B4X:
'Code module

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'Public variables can be accessed from all modules.
   Public Title As String
End Sub

Sub RemoveAllViews
   Main.NavControl.RemoveCurrentPage
  
End Sub

Sub loadLayout(layout As String)
   Main.NavControl.RemoveCurrentPage
   Main.pageMain.Initialize("pageMain")
   Main.pageMain.Title = "page" & layout
   Main.pageMain.RootPanel.Color = Colors.White
   Main.pageMain.RootPanel.LoadLayout(layout)
   Main.NavControl.ShowPage(Main.pageMain)
End Sub
 
Upvote 0

semar

Active Member
Licensed User
Longtime User
Hi imbault,
many thanks, your suggestion worked !

May I ask, why is it so ? Why that code can't be included in a module ?

Is there othe specific code that can't / should not be included inside a module ?

I ask this because I have lots of functions in .bas modules..
 
Upvote 0

semar

Active Member
Licensed User
Longtime User
Panel.LoadLayout sets the global variables in the current module or class.
That's the reason ! Many thanks Erel - and imbault of course !
The B4A --> B4i conversion goes further..:)
 
Upvote 0
Top