Apple iOS Tutorial: Problems getting UIButton addTarget to Work
There is actually a hierarchy of three views in this story:
- A stack view (not mentioned in the original question)
- The RatingControl
- The button
- The RatingControl
Well, the stack view, a complicated control whose job involves a lot of voodoo with the constraints of its arranged subviews, is reducing the size of the RatingControl to zero. The button is thus outside the RatingControl's bounds (easily proved by giving the RatingControl a background color — the background color doesn't appear). The RatingControl does not clip to its bounds, so the button is still visible — but, being outside its superview, it is not tappable.
As for why the stack view is reducing the size of the RatingControl to zero: it's because the RatingControl has no intrisicContentSize
. The reason why we need this is complicated and has to do with how a stack view manipulates its arranged subviews and gives them constraints. But in essence, it makes a lot of its decisions based on an arranged view's intrinsicContentSize
.
In the comments you said:
In size inspector it has intrinsic size set to placeholder, width 240, height 44. Or is there something I need to do to stop the stack view making it zero sized?
The key thing to understand here is that that setting in the size inspector is only a placeholder. It is merely to shut the storyboard up so that it won't complain. It doesn't actually give the view any intrinsic size at runtime. To do that, you must actually override the intrinsicContentSize
property, in your code, as a computed variable returning an actual size.
UIButton does not have a member named 'target'
Yep—UIControl (the superclass of UIButton) doesn’t expose those as properties, because there can be more than one of them and they can be associated with distinct events. There’s a method instead, -addTarget:action:forControlEvents:
menuButton.addTarget(self.revealViewController(), action: "revealToggle:", forControlEvents: UIControlEvents.TouchUpInside)
Type has no member during Apple tutorial
Initial to get the button action the RatingControl.swift
class will be like
import UIKit
@IBDesignable class RatingControl: UIStackView {
//MARK: Initialization
override init(frame: CGRect) {
super.init(frame: frame)
}
required init(coder: NSCoder) {
super.init(coder: coder)
}
//MARK: Button Action
@objc func ratingButtonTapped(button: UIButton) {
print("Button pressed")
}
}
Create a file named RatingControl.swift
with code and run your project. This issue will be resolved.
if you already have the full class the just add @objc
before the method ratingButtonTapped
.
More info: You can download the full project of the tutorial you currently following. The download link is present at the bottom.
Value of type 'CustomButton' has no member 'touchDown'
If you want to keep your method headers as are in your code, you need to change the selector references to #selector(touchDown(sender:))
, and so on.
(Generally, you have no need to prefix self.
.)
Remember all functions and methods now have consistent label treatment for their first parameters. SE-0046
(You may find many good articles, searching with "swift3 selector".)
If you want to keep the selector references, you need to change the methods like:
func touchDown(_ sender: CustomButton) {
For addition, #selector(touchDown)
would work, if your class has only one touchDown(...)
method.
Why am I getting has no member error
I think it's just a timing error. I ran the test to force a build and it ran fine. I think the executable hadn't been updated since I added the method and it was still showing as missing.
Error when trying to ask for user permission for notification
UIUserNotificationSettings
is available in iOS 8.0 and later: UIUserNotificationSettings Class Reference
Your code crashes because the API is not available in iOS 7.
Related Topics
Custom Back Button For Navigationview'S Navigation Bar in Swiftui
Swift 3 For Loop With Increment
How to Encode Enum Using Nscoder in Swift
Ios 14 Swiftui Keyboard Lifts View Automatically
Swift Equality Operator on Nested Arrays
Does Swift Have Documentation Generation Support
Swift Random Float Between 0 and 1
How Does One Generate a Random Number in Swift
Difference Between 'Let' and 'Var' in Swift
Passing an Array to a Function With Variable Number of Args in Swift
How to Enumerate an Enum With String Type
How to Dispatch_Sync, Dispatch_Async, Dispatch_After, etc in Swift 3, Swift 4, and Beyond
The Use of Swift 3 @Objc Inference in Swift 4 Mode Is Deprecated