2016-09-12 3 views
2

UITextFieldinputAccessoryView을 사용하여 두드려 질 때 UIToolbar에 추가 할 버튼이 있습니다. 이 모든 버튼은 제목을 제외하고는 동일하며, 내가 할 수있는 일은 titleColor, frame 등의 UI 속성을 다시 사용하는 것입니다.Swift에서 UIButton의 속성을 재사용하는 가장 좋은 방법은 무엇입니까

위에서 설명한 내용을 수행하는 가장 효율적인 방법은 무엇이 있을까요? 여기

는 ...

// code for first button 
    let button1 = UIButton(); 
    button1.backgroundColor = UIColor.orangeColor() 
    button1.setTitle("button1", forState: .Normal) 
    button1.setTitleColor(UIColor.whiteColor(), forState: .Normal) 
    button1.setTitleColor(UIColor.orangeColor(), forState: .Highlighted) 
    button1.frame = CGRect(x:0, y:0, width:35, height:35) 
    button1.addTarget(self, action: #selector(myFunction), forControlEvents: UIControlEvents.TouchUpInside) 

    // code for second button which is identical to the first button 
    let button2 = UIButton(); 
    button2.backgroundColor = UIColor.orangeColor() 
    button2.setTitle("button2", forState: .Normal) 
    button2.setTitleColor(UIColor.whiteColor(), forState: .Normal) 
    button2.setTitleColor(UIColor.orangeColor(), forState: .Highlighted) 
    button2.frame = CGRect(x:0, y:0, width:35, height:35) 
    button2.addTarget(self, action: #selector(myFunction), forControlEvents: UIControlEvents.TouchUpInside) 

    let barButton = UIBarButtonItem() 
    barButton.customView = button 

    let barButton2 = UIBarButtonItem() 
    barButton2.customView = button2 

    let toolBar = UIToolbar() 
    toolBar.items = [ barButton, barButton2] 
    toolBar.sizeToFit() 

    myTextField.inputAccessoryView = toolBar 

답변

5

팩터 단일 로컬 함수로 중복 코드의 코드이다.

func configure(_ button:UIButton) { 
    button.backgroundColor = UIColor.orangeColor() 
    button.setTitleColor(UIColor.whiteColor(), forState: .Normal) 
    button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted) 
    button.frame = CGRect(x:0, y:0, width:35, height:35) 
    button.addTarget(self, action: #selector(myFunction), forControlEvents: UIControlEvents.TouchUpInside) 
} 

// code for first button 
let button1 = UIButton() 
button1.setTitle("button1", forState: .Normal) 
configure(button1) 

// code for second button which is identical to the first button 
let button2 = UIButton() 
button2.setTitle("button2", forState: .Normal) 
configure(button2) 
+0

고마워요 당신 도와주세요! –

3

매트의 대답 @ 비슷해하지만, 이럴 더 나은 :

다른 곳에서 코드에서
// code for first button 
let button1 = UIButton().configure("button1", frame: CGRect(x: 0, y: 0, width: 35, height: 35), target: self, action: #selector(myFunction)) 

// code for second button which is probably not exactly identical to the first button. They should at minimum have different titles, different frames and activate different functions. 
let button2 = UIButton().configure("button2", frame: CGRect(x: 0, y: 0, width: 35, height: 35), target: self, action: #selector(myFunction)) 

당신은 아마 여러보기 컨트롤러에서 사용하기 때문에 : 대한

extension UIButton { 
    func configure(title: String, frame: CGRect, target: AnyObject, action: Selector) -> UIButton { 
     self.backgroundColor = UIColor.orangeColor() 
     self.setTitle(title, forState: .Normal) 
     self.setTitleColor(UIColor.whiteColor(), forState: .Normal) 
     self.setTitleColor(UIColor.orangeColor(), forState: .Highlighted) 
     self.frame = frame 
     self.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside) 
     return self 
    } 
} 
+0

나는이 접근 방식을 너무 좋아한다. 덕분에 많은 도움을 얻는다! –

+1

@fs_tigre 나는 또한 그의 접근 방식을 좋아하지만 중요한 조정이 필요하다. 기본 경우에 대한 열거 형을 추가하십시오. [this] (http://www.jessesquires.com/enums-as-configs/?utm_campaign=This%2BWeek%2Bin%2BSwift&utm_medium=email&utm_source=This_Week_in_Swift_98) 멋진 링크를 참조하십시오. – Honey

관련 문제