通过扩展UIButton类来创建自定义按钮

时间:2020-12-10 21:10:29

I'm in the process of learning Swift and I have a basic question. I don't want to use a Storyboard and I want to create a button with code. At the moment, I code my button this way:

我正在学习Swift,我有一个基本的问题。我不想使用故事板,我想用代码创建一个按钮。目前,我是这样编码我的按钮:

let button1: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
button1.setImage(UIImage(named: "myImage.png"), forState: UIControlState.Normal)
button1.addTarget(self, action: "myMethod", forControlEvents: UIControlEvents.TouchUpInside)
button1.frame = CGRectMake(0, 0, 25, 25) // then I add this button to a navigation control

This works, but I'm looking for something slightly different. That is: I want to create a class, I want to pass at least the image name, the method the button must call and the frame. Moreover, I want to instantiate the button in my ViewController whenever I want.

这是可行的,但我正在寻找一些稍微不同的东西。也就是说:我要创建一个类,我至少要传递图像名、按钮必须调用的方法和框架。而且,我想要在视图控制器中实例化这个按钮。

I found many posts here but no post was really useful (some code did not work).

我在这里发现了许多文章,但是没有文章是真正有用的(一些代码不起作用)。

Can you help me?

你能帮我吗?

1 个解决方案

#1


2  

enum SPBarButtonTypes {
    case openDrawer
    case loginInfo
}

class SPBarButtonFactory: NSObject {

    class func createBarButtonItemOfType (buttonType: SPBarButtonTypes, withTarget buttonTarget: AnyObject, andAction buttonAction: Selector) -> UIBarButtonItem {
        var returnButton: UIBarButtonItem = UIBarButtonItem()
        returnButton.target = buttonTarget
        returnButton.action = buttonAction

        switch buttonType {
            case .openDrawer:
                returnButton.image = UIImage(named: "ic_menu")
            case .loginInfo:
                returnButton.image = UIImage(named: "ic_info_outline")
        default: NSLog("Wrong BarButton type")
        }

        return returnButton
    }

}

ADDED TO IMPROVE COMMENT

添加到改善评论

It just "seams" like a class nested in another class, but it really is Swifts way to declare a class-method....

它只是“接缝”像一个类嵌套在另一个类,但它确实是雨燕声明一个类方法....

This line declares a class named SPBarButtonFactory

这一行声明了一个名为SPBarButtonFactory的类

class SPBarButtonFactory...

This line declares a class-method of SPBarButtonFactory

这一行声明了SPBarButtonFactory的类方法

class func createBarButtonItemOfType...

#1


2  

enum SPBarButtonTypes {
    case openDrawer
    case loginInfo
}

class SPBarButtonFactory: NSObject {

    class func createBarButtonItemOfType (buttonType: SPBarButtonTypes, withTarget buttonTarget: AnyObject, andAction buttonAction: Selector) -> UIBarButtonItem {
        var returnButton: UIBarButtonItem = UIBarButtonItem()
        returnButton.target = buttonTarget
        returnButton.action = buttonAction

        switch buttonType {
            case .openDrawer:
                returnButton.image = UIImage(named: "ic_menu")
            case .loginInfo:
                returnButton.image = UIImage(named: "ic_info_outline")
        default: NSLog("Wrong BarButton type")
        }

        return returnButton
    }

}

ADDED TO IMPROVE COMMENT

添加到改善评论

It just "seams" like a class nested in another class, but it really is Swifts way to declare a class-method....

它只是“接缝”像一个类嵌套在另一个类,但它确实是雨燕声明一个类方法....

This line declares a class named SPBarButtonFactory

这一行声明了一个名为SPBarButtonFactory的类

class SPBarButtonFactory...

This line declares a class-method of SPBarButtonFactory

这一行声明了SPBarButtonFactory的类方法

class func createBarButtonItemOfType...