So, here’s the promised additional bells and whistles example for those interested in exploring the use of Objective-C UIPicker View. Thanks again to @jfperusse, for helping unlock the syntax for using UIPicker View in Codea. The following example adds to his example by illustrating a two-component picker in addition to examples on how to change the formatting and text of the UIPicker view. It includes the use of UILabels and UIButtons. Apple’s auto layout syntax seems quite tedious (Codea’s text and graphical rendering seems much simpler) but does produce a nice GUI layout end-result:
-- UIButtonsLabels&PickerView
function setup()
viewer.mode = FULLSCREEN
--note: the next statement gets the actual UIViewController running in Codea
local vc = objc.viewer
-- setup UIPicker's delegates and their methods ("Delegate" and "DataSource")
-- Picker (note: row and components (Objective C arrays) start at index 0 not 1 like lua so need +/- offset lua tbl index)
Delegate = objc.delegate("UIPickerViewDelegate")
DataSource = objc.delegate("UIPickerViewDataSource")
-- structure table as {{component1},...}
data = {{" Mark "," Lori "," Bob "," Shane "},{" lua ", " javascript ", " R ", " Objective-C "}}
-- for Objective-C delegates, need to ensure that parameter names preceded by type (e.g. obj)
function DataSource:numberOfComponentsInPickerView_(objPickerView)
return #data -- == # of components or 1st order of the lua table (array)
end
function DataSource:pickerView_numberOfRowsInComponent_(objPickerView, intComponent)
return #data[intComponent+1] -- ok for array type tables but won't work for dictionary tables
end
function Delegate:pickerView_viewForRow_forComponent_reusingView_(objPickerView, intRow, intComponent, objView)
-- note can also test for component case and set text characteristics differently per component
local label = objc.UILabel()
label.font = objc.UIFont:systemFontOfSize_weight_(40,500)
label.textColor = color(0,0,0)
label.text = data[intComponent+1][intRow+1]
return label
end
-- note: if you don't need to resize picker text size, use the following simpler Delegate method instead of above method:
-- function Delegate:pickerView_titleForRow_forComponent_(objPickerView, intRow, intComponent)
-- return data[intComponent+1][intRow + 1] -- compensates for lua index being Objc Index + 1
-- end
-- now setup UIPickerView object
picker = objc.UIPickerView()
picker.delegate = Delegate()
picker.dataSource = DataSource()
picker.backgroundColor = color(255,255,255)
picker.textColor = color(0,0,0)
picker.hidden = true
vc.view:addSubview_(picker)
picker.translatesAutoresizingMaskIntoConstraints = false
picker.centerXAnchor:constraintEqualToAnchor_(vc.view.centerXAnchor).active = true
picker.topAnchor:constraintEqualToAnchor_constant_(vc.view.topAnchor, HEIGHT/2 -40).active = true
picker.heightAnchor:constraintEqualToConstant_(300).active = true
picker.widthAnchor:constraintEqualToConstant_(600).active = true
-- setup labels
local nameLabel = objc.UILabel()
local langLabel = objc.UILabel()
local nameLabelResult = objc.UILabel()
local langLabelResult = objc.UILabel()
nameLabel.text = "Name: "
langLabel.text = "Preferred Language: "
nameLabelResult.text = ""
langLabelResult.text = ""
nameLabel.textColor = color(255,255,255)
langLabel.textColor = color(255,255,255)
nameLabelResult.textColor = color(0,0,0)
langLabelResult.textColor = color(0,0,0)
nameLabelResult.backgroundColor = color(255,255,255)
langLabelResult.backgroundColor = color(255,255,255)
nameLabel.font = objc.UIFont:systemFontOfSize_(40)
langLabel.font = objc.UIFont:systemFontOfSize_(40)
nameLabelResult.font = objc.UIFont:systemFontOfSize_(40)
langLabelResult.font = objc.UIFont:systemFontOfSize_(40)
vc.view:addSubview_(nameLabel)
vc.view:addSubview_(langLabel)
vc.view:addSubview_(nameLabelResult)
vc.view:addSubview_(langLabelResult)
nameLabel.translatesAutoresizingMaskIntoConstraints = false
langLabel.translatesAutoresizingMaskIntoConstraints = false
nameLabelResult.translatesAutoresizingMaskIntoConstraints = false
langLabelResult.translatesAutoresizingMaskIntoConstraints = false
nameLabel.topAnchor:constraintEqualToAnchor_constant_(vc.view.topAnchor, 40).active = true
nameLabel.leadingAnchor:constraintEqualToAnchor_constant_(vc.view.leadingAnchor, 80).active = true
langLabel.topAnchor:constraintEqualToAnchor_constant_(nameLabel.bottomAnchor, 20).active = true
langLabel.leadingAnchor:constraintEqualToAnchor_constant_(vc.view.leadingAnchor, 80).active = true
nameLabelResult.topAnchor:constraintEqualToAnchor_constant_(vc.view.topAnchor, 40).active = true
nameLabelResult.trailingAnchor:constraintEqualToAnchor_constant_(vc.view.trailingAnchor, -80).active = true
langLabelResult.topAnchor:constraintEqualToAnchor_constant_(nameLabelResult.bottomAnchor, 20).active = true
langLabelResult.trailingAnchor:constraintEqualToAnchor_constant_(vc.view.trailingAnchor, -80).active = true
-- setup buttons
local uiButton = objc.UIButton
local changeAction = objc.UIAction:actionWithHandler_(function(objAction) picker.hidden, saveButton.hidden, cancelButton.hidden = false,false,false end)
local saveAction = objc.UIAction:actionWithHandler_(function(objAction) nameLabelResult.text = data[1][(picker:selectedRowInComponent_(0))+1]; langLabelResult.text = data[2][(picker:selectedRowInComponent_(1))+1]; end)
local cancelAction = objc.UIAction:actionWithHandler_(function(objAction) picker.hidden, saveButton.hidden, cancelButton.hidden = true,true,true end)
changeButton= uiButton:buttonWithType_primaryAction_(objc.enum.UIButtonType.system, changeAction)
saveButton= uiButton:buttonWithType_primaryAction_(objc.enum.UIButtonType.system, saveAction)
cancelButton= uiButton:buttonWithType_primaryAction_(objc.enum.UIButtonType.system, cancelAction)
changeButton:setTitle_forState_(" change ", objc.enum.UIControlState.normal)
saveButton:setTitle_forState_(" save ", objc.enum.UIControlState.normal)
cancelButton:setTitle_forState_(" cancel ", objc.enum.UIControlState.normal)
changeButton:setTitleColor_forState_(color(255,165,0), objc.enum.UIControlState.normal)
saveButton:setTitleColor_forState_(color(0,255,0))
cancelButton:setTitleColor_forState_(color(255,0,0))
changeButton:setBackgroundColor_(color(220,220,220))
saveButton:setBackgroundColor_(color(220,220,220))
cancelButton:setBackgroundColor_(color(220,220,220))
saveButton.hidden = true
cancelButton.hidden = true
vc.view:addSubview_(changeButton)
vc.view:addSubview_(saveButton)
vc.view:addSubview_(cancelButton)
-- layout using Apple's auto-layout functionality
changeButton.translatesAutoresizingMaskIntoConstraints = false
saveButton.translatesAutoresizingMaskIntoConstraints = false
cancelButton.translatesAutoresizingMaskIntoConstraints = false
changeButton.centerXAnchor:constraintEqualToAnchor_(vc.view.centerXAnchor).active = true
changeButton.bottomAnchor:constraintEqualToAnchor_(picker.topAnchor).active = true
saveButton.bottomAnchor:constraintEqualToAnchor_(picker.topAnchor).active = true
saveButton.leadingAnchor:constraintEqualToAnchor_(picker.leadingAnchor).active = true
cancelButton.bottomAnchor:constraintEqualToAnchor_(picker.topAnchor).active = true
cancelButton.trailingAnchor:constraintEqualToAnchor_(picker.trailingAnchor).active = true
end