Swift 5.0 Circular UIButton [closed] - swift

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
As the title says, I want to create a circular UIButton. However, the course I'm currently following is (somewhat) outdated and I was not able to find a working solution to why this piece of code is not working. Most google searches ended up showing a somewhat same piece of code as I have here, but gave me the exact same error. Hopefully you guys can explain what I'm doing wrong here.
The piece of code including the error message:

The error is pretty clear and has nothing to do with outdated code. It's probably a code completion mistake.
You cannot use instance variables in a class function, not even in Swift 1.
Remove class in the awakeFromNib line to apply the instance method

remove keyword class from the function's signature and instead of frame use bounds
override func awakeFromNib() {
layer.cornerRadius = max(bounds.maxY,bounds.maxX)/2
layer.masksToBounds = true
}
But better is to add cornerRadius in layout subviews
override func layoutSubviews() {
layer.cornerRadius = max(bounds.maxY,bounds.maxX)/2
}

Related

Call a function from a separate class not working [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am calling a function in a different class that simply removes a lottie animation. I do this by calling...
RedeemController().removeAnimationFromSuperview()
Which enters the function (as proven by a print statement). However the animation does not disappear from the view.
When I call the same function from within the class that the animation is defined, the animation is removed from the view as expected. Here is the function. Very simple.
fileprivate func removeAnimationFromSuperview(){
animationViewDraw.removeFromSuperview()
print("Entered")
}
I would expect this animation to disappear. Thanks for your help!!!!
You are creating new instance of RedeemController that has it's own properties, instead you have to use the instance that you currently have
myControllerInstance.removeAnimationFromSuperview()

Swift: How to make sure that the button returns image to previous state [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm building iOS app I have a button that if pressed once it should change its image. If I press it once again it should go back to the original image. I know I can easily do it by checking if it's the second image on button press but I'm pretty sure that there is a better approach to this.
You can keep one extra Boolean variable to maintain the state of the button. Modify the bool value on every click. On didSet of the variable, you can update your button Image. If you have multiple states of the button you can have one enum.
private var isButtonActive: Bool = true {
didSet {
let image = isButtonActive ? "Image1": "Image2"
button.setImage(image, for: .normal)
}
}
func buttonClick() {
isButtonActive = !isButtonActive
}

Swift: use string variable in other file [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I wrote an app in Swift where I have a page I can set settings for my game. After pressing the button for the settings, the file with the game opens. But how can I get the variable I want to set to my game file?
I tried to work around this by creating a struct in the first file where I set a variable that I can change by pressing the button and which I later call on my game page. But it didn't work (the variable needed to be static, so I couldn't change it). Also it didn't seem like a smart way for me.
struct Globals {
static var score: Int = 0
// Any other global variables you want.
}
Access by:
let score = Globals.score
You can also store information in UserDefaults to persist across launches of the app.

Selector in custom class [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I wrote a custom button which is basically a NSView subclass. The button reacts on mouseDown() and should run a selector/action.
I don't know how to add a target and action variable to the subclass (similar to user interface elements like NSButton). I tried
var target : AnyObject?
var action: Selector?
Also how do I run the selector with the target in my subclass?
The "target/action" pair is archaic and should be avoided. But if you really wanted to use it, have a look at the NSApplication sendAction(_:to:from:) method.
Or if your custom button extends NSControl, you can use its target and action properties and its sendAction(_:to:) method.
A more modern approach would be to provide a closure property for your button class and then call that closure instead of using a target/action pair. Using a closure is safer, cleaner, and probably makes client code of your button class easier to write.

In Swift, should I write a separate class for handling a UI component? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I am new to OOP and Swift. I have some code like this:
class ViewController: UIViewController {
var topMenuScrollView: UIScrollView!
topMenu_doSomething1()
topMenu_doSomething2()
topMenu_doSomething3()
...
}
topMenu is a scrollView I have to do many things with. I found I have to write a lot of code for this top menu in this view controller file. Therefore, should I create a new class (a new swift file) specifically for the top menu handling?
However, I only have one top menu. Is it correct to write a class for it? (because I won't have multiple instances)
What's the right way to arrange this?
The purpose of object-oriented design is as much code-reuse as it is code-comprehensibility. In your case, it sounds like your menu has a lot of detailed behavior which is specific to that menu. Well, imagine at some point you want to change or rewrite the menu; it will certainly be easier if that logic is clearly isolated.
It sounds like this is a good case for another class, but it's entirely up to you.