Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom UIButton class swift

Tags:

swift

I was curious of how to create a custom class for a UI element (say for example, a button)?

I've read the Apple development docs and I followed the instructions, what I thought was to create a separate UIButton class file and link that class file for all buttons on the storyboard.

Here's the separate UIButton class code (file named buttonz.swift):

import UIKit

public class buttonz: UIButton {


    required public init?(coder aDecoder: NSCoder) {

        super.init(coder: aDecoder)

        self.layer.borderWidth = 1
        self.backgroundColor = UIColor.whiteColor()
        self.layer.borderColor = UIColor.blackColor().CGColor

    }

}

And on the storyboard, I linked that specific button to the new UIButton class:

enter image description here

The problem is that when I run the simulator, it gives me an error in the debig console:

this class is not key value coding-compliant for the key aButton

I thought the process was straightforward in the guide but am I missing any extra steps? Please let me know, thanks!

For reference, here is the link to the Apple docs that I used as a guide.

https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson5.html

Edit 1 - Here's the action link to the viewcontroller.swift for the view controller. I just ctrl + dragged the button from the story board into the viewcontroller.swift.

 @IBAction func a(sender: AnyObject) {
        var alertView = UIAlertView();
        alertView.addButtonWithTitle("Ok");
        alertView.title = "title";
        alertView.message = "message";
        alertView.show();
    }
like image 923
rj2700 Avatar asked Mar 18 '16 21:03

rj2700


People also ask

How do you highlight a button when it is clicked Swift?

In the identity inspector in the right corner there is a filed named "class" there type "HighlightedButton" and press enter. Now your button will change color to red when it is highlighted and back to green when you release the button. You can change to any color you want.

How do I make a button in SwiftUI?

SwiftUI's button is similar to UIButton , except it's more flexible in terms of what content it shows and it uses a closure for its action rather than the old target/action system. To create a button with a string title you would start with code like this: Button("Button title") { print("Button tapped!") }


1 Answers

Your code looks ok, I think it's a connection error. Open your storyboard file, select File's Owner (or the yellow circle on top of the view controller) and click on the "Connection Inspector" (arrow on the top right, which is the one next to the size inspector), to see all outlets at once. Look for !s which indicates a missing outlet.

If you're creating custom UIClasses I would suggest having a look at IBInspectables and IBDesignables, there is a lot about them, and enable you to do quite a bit through just storyboards, and it doesn't change your code all that much.

Some articles:

IBInspectable / IBDesignable - NSHipster

Creating a Custom View That Renders in Interface Builder - Apple

Enjoy :)

like image 174
MrHaze Avatar answered Sep 22 '22 21:09

MrHaze