Skip to content

Instantly share code, notes, and snippets.

@apple-avadhesh
Created September 21, 2021 14:30
Show Gist options
  • Save apple-avadhesh/117cd35d61eb1b0455a88e2ce1279e0c to your computer and use it in GitHub Desktop.
Save apple-avadhesh/117cd35d61eb1b0455a88e2ce1279e0c to your computer and use it in GitHub Desktop.
iOS Control Action Callbacks
// UITextField
let textField = UITextField()
textField.addAction(
UIAction { action in
let textField = action.sender as! UITextField
print(textField.text ?? "")
},
for: .editingChanged
)
// UIButton
// Approach 1
let button = UIButton(primaryAction: UIAction { _ in
print("Button Touched")
})
// Approach 2
let btn = UIButton()
btn.addAction(
UIAction { _ in
print("Button Touched")
},
for: .touchUpInside
)
// UISwitch
let swi = UISwitch()
swi.addAction(
UIAction { action in
let swi = action.sender as! UISwitch
print(swi.isOn)
},
for: .valueChanged
)
// UISlider
let slider = UISlider()
slider.addAction(
UIAction { action in
let slider = action.sender as! UISlider
print(slider.value)
},
for: .valueChanged
)
// UIStepper
let stepper = UIStepper()
stepper.addAction(
UIAction { action in
let stepper = action.sender as! UIStepper
print(stepper.value)
},
for: .valueChanged
)
// UISegmentedControl
let segmentedControl = UISegmentedControl()
segmentedControl.addAction(
UIAction { action in
let segmentedControl = action.sender as! UISegmentedControl
print(segmentedControl.selectedSegmentIndex)
},
for: .valueChanged
)
// UIPageControl
let pageControl = UIPageControl()
pageControl.addAction(
UIAction { action in
let pageControl = action.sender as! UIPageControl
print(pageControl.currentPage)
},
for: .valueChanged
)
// UIDatePicker
let datepicker = UIDatePicker()
datepicker.addAction(
UIAction { action in
let datepicker = action.sender as! UIDatePicker
print(datepicker.date)
},
for: .valueChanged
)
// UIRefreshControl
let tableView = UITableView(frame: UIScreen.main.bounds)
let refreshControl = UIRefreshControl()
refreshControl.addAction(
UIAction { action in
let refreshControl = action.sender as! UIRefreshControl
print(refreshControl.isRefreshing)
},
for: .valueChanged
)
tableView.refreshControl = refreshControl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment