iOS Swift - SwiftUI BasicsWhich of the following is the correct syntax to assign an action method to a UIButton for the .touchUpInside event in Swift?Abutton.addAction(#selector(buttonPressed), for: .touchUpInside)Bbutton.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)Cbutton.setAction(#selector(buttonPressed), for: .touchUpInside)Dbutton.addTarget(self, action: buttonPressed, for: .touchUpInside)Check Answer
Step-by-Step SolutionSolution:Step 1: Use addTarget(_:action:for:) methodThis method is used to add a target and action for a control event.Step 2: Use #selector syntax for the actionThe action parameter requires a selector, which is specified using #selector(methodName).Final Answer:button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside) -> Option BQuick Check:Correct method and selector syntax used [OK]Quick Trick: Use addTarget with #selector for button actions [OK]Common Mistakes:Using addAction instead of addTargetPassing method name without #selectorUsing setAction which doesn't exist
Master "SwiftUI Basics" in iOS Swift9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepUITryChallengeBuildRecallPublish
More iOS Swift Quizzes Swift Language Essentials - Data types (Int, Double, String, Bool) - Quiz 14medium Swift Language Essentials - Enums with associated values - Quiz 2easy SwiftUI Basics - Spacer and padding - Quiz 7medium SwiftUI Layout - LazyVStack and LazyHStack - Quiz 4medium SwiftUI Layout - Overlay and background modifiers - Quiz 5medium SwiftUI Layout - Why layout controls visual structure - Quiz 5medium User Input and Forms - Toggle switch - Quiz 10hard User Input and Forms - Picker and DatePicker - Quiz 4medium iOS Basics and Setup - Xcode interface (navigator, editor, inspector) - Quiz 12easy iOS Basics and Setup - First iOS app - Quiz 14medium