iOS Question Strange DatePicker

Rafael Grossi

Member
Licensed User
Hello guys,

I was working with DatePicker and all was goning well, showing the expected DatePicker.
I don't know what I did, and probably it doesn't make sense, but when I started using XUI Views library, the DatePicker assumed a strange layout, as you can see in the picture:
IMG_0172.PNG

This strange layout doesn't work, and if I try to choose a month or a year, nothing happens - the objects are frozen. If I click in Ok, I have an error (log):

B4X:
Application_Start
Application_Active
Error occurred on line: 444 (Main)
Expected: UIDatePicker, object type: B4IPanelView
Stack Trace: (
  CoreFoundation       1B9B1E61-8CB4-3903-9870-402C3DE959BB + 1227356
  libobjc.A.dylib      objc_exception_throw + 56
  CoreFoundation       1B9B1E61-8CB4-3903-9870-402C3DE959BB + 135616
  PainKit Cadastro     -[B4IObjectWrapper setObject:] + 276
  PainKit Cadastro     -[ResumableSub_main_imgCP5_click resume::] + 2296
  CoreFoundation       1B9B1E61-8CB4-3903-9870-402C3DE959BB + 1252384
  CoreFoundation       1B9B1E61-8CB4-3903-9870-402C3DE959BB + 7472
  PainKit Cadastro     +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1608
  PainKit Cadastro     -[B4IShell runMethod:] + 448
  PainKit Cadastro     -[B4IShell raiseEventImpl:method:args::] + 2172
PainKit Cadastro     -[B4IShellBI raiseEvent:event:params:] + 1580
PainKit Cadastro     -[B4IDelegatableResumableSub resume::] + 380
PainKit Cadastro     -[B4I checkAndRunWaitForEvent:event:params:] + 516
PainKit Cadastro     -[B4IShellBI raiseEvent:event:params:] + 1436
PainKit Cadastro     __33-[B4I raiseUIEvent:event:params:]_block_invoke + 60
libdispatch.dylib    A3849F96-1C9F-36C5-A15F-70C566F14CFF + 374288
libdispatch.dylib    A3849F96-1C9F-36C5-A15F-70C566F14CFF + 377220
libdispatch.dylib    A3849F96-1C9F-36C5-A15F-70C566F14CFF + 57744
CoreFoundation       1B9B1E61-8CB4-3903-9870-402C3DE959BB + 693732
CoreFoundation       1B9B1E61-8CB4-3903-9870-402C3DE959BB + 673240
CoreFoundation       CFRunLoopRunSpecific + 464
GraphicsServices     GSEventRunModal + 104

The code in the line 444 is:
B4X:
        Dim dp As DatePicker = p.GetView(0) 'DatePicker is the first view in the layout

I have tried to disable the XUI Views and forget the itens associated with this library, but after disabled, when I ask the DatePicker, I have another error:
B4X:
Application_Start
Application_Active
Error occurred on line: 440 (Main)
Object was not initialized (UIFont)
Stack Trace: (
  CoreFoundation       1B9B1E61-8CB4-3903-9870-402C3DE959BB + 1227356
  libobjc.A.dylib      objc_exception_throw + 56
  CoreFoundation       1B9B1E61-8CB4-3903-9870-402C3DE959BB + 135616
  PainKit Cadastro     -[B4IObjectWrapper object] + 136
  PainKit Cadastro     +[B4IButtonWrapper build:::] + 1128
  PainKit Cadastro     +[B4ILayoutBuilder dynamicBuild::::] + 220
  PainKit Cadastro     -[B4ILayoutBuilder loadLayoutHelper:::::::::::] + 2300
  PainKit Cadastro     -[B4ILayoutBuilder loadLayoutHelper:::::::::::] + 4696
  PainKit Cadastro     -[B4ILayoutBuilder loadLayout:::] + 1932
  PainKit Cadastro     -[B4IPanelWrapper LoadLayout::] + 204
PainKit Cadastro     -[ResumableSub_main_imgCP5_click resume::] + 1052
PainKit Cadastro     -[b4i_main _imgcp5_click] + 364
CoreFoundation       1B9B1E61-8CB4-3903-9870-402C3DE959BB + 1252384
CoreFoundation       1B9B1E61-8CB4-3903-9870-402C3DE959BB + 7472
PainKit Cadastro     +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1608
PainKit Cadastro     -[B4IShell runMethod:] + 448
PainKit Cadastro     -[B4IShell raiseEventImpl:method:args::] + 2172
PainKit Cadastro     -[B4IShellBI raiseEvent:event:params:] + 1580
PainKit Cadastro     __33-[B4I raiseUIEvent:event:params:]_block_invoke + 60
libdispatch.dylib    A3849F96-1C9F-36C5-A15F-70C566F14CFF + 374288
libdispatch.dylib    A3849F96-1C9F-36C5-A15F-70C566F14CFF + 377220
libdispatch.dylib    A3849F96-1C9F-36C5-A15F-70C566F14CFF + 57744

The line 440 is:
B4X:
    p.LoadLayout("DatePicker")

And the whole code of asking the DatePicker is:
B4X:
Sub imgCP5_click
    Dim cd As CustomLayoutDialog
    Dim p As Panel
    p.Initialize("")
    p.SetLayoutAnimated(0, 1, 0, 0, Page1.RootPanel.Width - 40dip, 170dip)
    p.LoadLayout("DatePicker")
    cd.Initialize(p)
    Wait For (cd.ShowAsync("Escolha a data de nascimento:", "Ok", "Cancelar", "", False)) Dialog_Result(Result As Int)
    If Result = cd.RESULT_POSITIVE Then
        Dim dp As DatePicker = p.GetView(0) 'DatePicker is the first view in the layout
        'Log($"Selected date: $Date{dp.Ticks}"$)
        'Log(DateTime.GetDayOfMonth(dp.Ticks))
   
   
        If DateTime.GetDayOfMonth(dp.Ticks) < 10 Then
            dnasc = "0" &  DateTime.GetDayOfMonth(dp.Ticks)
        Else
            dnasc = DateTime.GetDayOfMonth(dp.Ticks)
        End If
   
        If DateTime.GetMonth(dp.Ticks) < 10 Then
            mnasc = "0" & DateTime.GetMonth(dp.Ticks)
        Else
            mnasc = DateTime.GetMonth(dp.Ticks)
        End If
       
        anasc = DateTime.Getyear(dp.Ticks)
       
        lblCP1.Text = dnasc & "/" & mnasc & "/" & anasc
   
    End If

End Sub

It's the second time it occours, in the first time I don't know what I have done but returned to normally. Now I can't fix it.
I have already tried to reinstall the app and the bridge, clear the project, I closed all windows of b4i...

Could you help me?
Thanks!
 
Last edited:

Rafael Grossi

Member
Licensed User
Complementing, trying to solve the problem, with the XUI library disabled, I changed all "p" (from panel name) to "pdp", this way:

B4X:
Sub imgCP5_click
    Dim cd As CustomLayoutDialog
    Dim pdp As Panel
    pdp.Initialize("")
    pdp.SetLayoutAnimated(0, 1, 0, 0, Page1.RootPanel.Width - 40dip, 170dip)
    pdp.LoadLayout("DatePicker")
    cd.Initialize(pdp)
    Wait For (cd.ShowAsync("Escolha a data de nascimento:", "Ok", "Cancelar", "", False)) Dialog_Result(Result As Int)
    If Result = cd.RESULT_POSITIVE Then
        Dim dp As DatePicker = pdp.GetView(0) 'DatePicker is the first view in the layout
        'Log($"Selected date: $Date{dp.Ticks}"$)
        'Log(DateTime.GetDayOfMonth(dp.Ticks))
    
    
        If DateTime.GetDayOfMonth(dp.Ticks) < 10 Then
            dnasc = "0" &  DateTime.GetDayOfMonth(dp.Ticks)
        Else
            dnasc = DateTime.GetDayOfMonth(dp.Ticks)
        End If
    
        If DateTime.GetMonth(dp.Ticks) < 10 Then
            mnasc = "0" & DateTime.GetMonth(dp.Ticks)
        Else
            mnasc = DateTime.GetMonth(dp.Ticks)
        End If
        
        anasc = DateTime.Getyear(dp.Ticks)
        
        lblCP1.Text = dnasc & "/" & mnasc & "/" & anasc
    
    End If

End Sub

Problem solved!
So, I've thought: let's turn on XUI Views again and... error again.
So I disabled the XUI Views, but the error keep there.

I tried again to change "pdp" for "pdp2" but it doesn't worked.

Suggestions?
 
Upvote 0

Rafael Grossi

Member
Licensed User
Hello Erel,

Thanks for answer.
I could open the date dialog (this topic also helped me: https://www.b4x.com/android/forum/threads/make-dialog-visible.103267/#post-647385).
But in the app, the user will select birth date to make clients registers, so if the user needs to select, for example, 1948 as birth year, this dialog requires many clicks in the arrow to reach the year, so the old style is more practical. Look the will make many register in a day, not just one.

Old style:
olddatepicker.jpg


Is there an alternative to use the old style and not conflict with XUI Views library?

Thanks again!
 
Upvote 0

Rafael Grossi

Member
Licensed User
Hello Erel,

I've done it with B4XDialog, and worked well. I've attached the code with the two different styles (calendar and picker date dialog).

Some questions:

- In calendar style (B4XDateTemplate), using exactly the same code of that link (https://www.b4x.com/android/forum/threads/b4x-light-theme-b4xdatetemplate.111837/#content), the arrows of month and year are white color, and not black/gray. If I want these buttons (btnMonthLeft, btnMonthRight, btnYearLeft and btnYearRight) with white background color, I can't see the arrows. I've tried to change some ways of their TextColor property, without success. How to change it? (you will see in the file attached).

- Yet in the calendar style, the names of the months and week days will show in the language I set with the folder pt.lproj (in my case)? Because the picker style is showing the correct language (portuguese here), but the calendar style is in English.

- Even the picker style probably will work with you - here is working too - sometimes I get an error in line 74 when I call the picker style dialog. The error is similar that when I was trying to use the old CustomLayoutDialog. I swear the code was the same as this file. It was working fine, and then the error below was showed. And then after a few tries, including opening and closing b4i, reestablishing the bridge, reinstall the app, etc, it returned to normal. My project, in which I am implementing this code, the same happens, sometimes works and sometimes this error appears:

Line 74:
B4X:
    pdatepicker.LoadLayout("datepicker")

Error:
B4X:
Application_Start
Application_Active
Error occurred on line: 74 (Main)
Object was not initialized (UIView)
Stack Trace: (
  CoreFoundation       1B9B1E61-8CB4-3903-9870-402C3DE959BB + 1227356
  libobjc.A.dylib      objc_exception_throw + 56
  CoreFoundation       1B9B1E61-8CB4-3903-9870-402C3DE959BB + 135616
  ETD Cadastro         -[B4IObjectWrapper object] + 136
  ETD Cadastro         -[B4IViewWrapper setWidth:] + 52
  ETD Cadastro         -[LS_datepicker LS_general:::] + 252
  CoreFoundation       1B9B1E61-8CB4-3903-9870-402C3DE959BB + 1252384
  CoreFoundation       1B9B1E61-8CB4-3903-9870-402C3DE959BB + 7472
  ETD Cadastro         +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1608
  ETD Cadastro         +[B4ILayoutBuilder runScripts:::] + 980
ETD Cadastro         -[B4ILayoutBuilder loadLayout:::] + 2312
ETD Cadastro         -[B4IPanelWrapper LoadLayout::] + 204
ETD Cadastro         -[B4XViewWrapper LoadLayout::] + 100
CoreFoundation       1B9B1E61-8CB4-3903-9870-402C3DE959BB + 1252384
CoreFoundation       1B9B1E61-8CB4-3903-9870-402C3DE959BB + 7472
ETD Cadastro         +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1608
ETD Cadastro         -[B4IShell runMethod:] + 448
ETD Cadastro         -[B4IShell raiseEventImpl:method:args::] + 2172
ETD Cadastro         -[B4IShellBI raiseEvent:event:params:] + 1580
ETD Cadastro         __33-[B4I raiseUIEvent:event:params:]_block_invoke + 60
libdispatch.dylib    A3849F96-1C9F-36C5-A15F-70C566F14CFF + 374288
libdispatch.dylib    A3849F96-1C9F-36C5-A15F-70C566F14CFF + 377220

Attached file in my Google Drive (as I received the message it is too large, in the forum): link

EDIT: I've just noticed in the project where I am implementing the code maybe a important thing. In Debug mode, I have the error above when I call the picker style date dialog. But without any modification, I build the app in Release mode and there everything works well.

Thanks again!
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I recommend to remove the OK button with the date template.

1. The arrows are buttons. Buttons require different code to change the text color:
B4X:
For Each x As B4XView In Array(DateTemplate.btnMonthLeft, DateTemplate.btnMonthRight, DateTemplate.btnYearLeft, DateTemplate.btnYearRight)
    x.Color = xui.Color_White
    SetButtonTextColor(x, xui.Color_Gray, 0)
Next

'state: 0 = normal, 1 = pressed, 2 = disabled
Sub SetButtonTextColor(btn As Button, clr As Int, state As Int)
    Dim no As NativeObject = btn
    no.RunMethod("setTitleColor:forState:", Array(no.ColorToUIColor(clr), state))
End Sub

2. You can use this code to set the date and time locale:
B4X:
Sub SetDateTimeLocale (locale As String)
    Dim loc As NativeObject
    loc = loc.Initialize("NSLocale").RunMethod("localeWithLocaleIdentifier:", Array(locale))
    Dim no As NativeObject = DateTime
    no.GetField("dateFormat").SetField("locale", loc)
    no.GetField("timeFormat").SetField("locale", loc)
End Sub

Or you can use SetDateTimeLocaleToDeviceLocale to set it to the current device locale. I'm not sure that it is the best idea. For example with Hebrew the two first letters of all days will be identical.

3. No reason to use the old CustomLayoutDialog.
 
Upvote 0
Top