2016-08-31 4 views
0

신속한 코드에서 .disable 메소드에 문제가 있습니다. 나는 일련의 버튼을 가지고 있으며, 올바른 버튼 (대상 버튼)이 눌려 졌을 때 나머지 버튼을 무효로하고 싶다. 내 배열은 Buttons라고 부릅니다! 다음은 버튼에 대한 작업입니다. 버튼에 다른 이름이 있어야합니까? 또는 .count 메서드를 사용할 수 있습니까? 감사합니다.버튼을 눌렀을 때 버튼 배열에서 나머지 버튼을 비활성화하는 방법

@IBAction func btn1(sender: AnyObject) { 
    if answerNumber == 0 { 
     cwLabel.text = "You are Right!" 
     pickQuestion() 
     Buttons.count 

    } else { 
     cwLabel.text = "You are Wrong!" 
     pickQuestion() 
    } 
} 

@IBAction func btn2(sender: AnyObject) { 
    if answerNumber == 1 { 
     cwLabel.text = "You are Right!" 
     pickQuestion() 

    } else { 
     cwLabel.text = "You are Wrong!" 
     pickQuestion() 
    } 
} 

@IBAction func btn3(sender: AnyObject) { 
    if answerNumber == 2 { 
     cwLabel.text = "You are Right!" 
     pickQuestion() 

    } else { 
     cwLabel.text = "You are Wrong!" 
     pickQuestion() 
    } 
} 

@IBAction func btn4(sender: AnyObject) { 
    if answerNumber == 3 { 
     cwLabel.text = "You are Right!" 
     pickQuestion() 

    } else { 
     cwLabel.text = "You are Wrong!" 
     pickQuestion() 
    } 
} 
+0

'tag' 속성에 대해 들었습니까? 모든 버튼을 동일한 액션 핸들러로 보내고, 태그를보고, 답변을 살펴보십시오. 별도의 액션 핸들러가 필요 없습니다. – matt

답변

0

버튼을 모두 사용하지 않도록 설정 했습니까? 토글을 사용할 수 있습니다

func toggleButtons(button: UIButton) { 
    for (var index = 0; index < arrayButton.count; index += 1) { 
     if arrayButton[index] != button { 
      arrayButton[index].enabled = false 
     } 
    } 
} 
+0

압축되지 않은 파일을 사용하지 않고 그대로 둡니다. –

+0

위 코드를 시도 했습니까? – aatalyk

+0

위의 Payal Maniyar가 완성 된 버전을 작성했습니다. – aatalyk

1

아래 코드를 사용하십시오. 도움이 될 수도 있습니다 ...

for (var index = 0; index < arrayButton.count; index += 1) { 
      let btn : UIButton = arrayButton[index] as UIButton 
    btn.addTarget(self, action: #selector(self.toggleButtons toggleButtons(_:)), forControlEvents: .TouchUpInside) 
     } 

    func toggleButtons(button: UIButton) { 
     for (var index = 0; index < arrayButton.count; index += 1) { 
      if arrayButton[index] != button { 
       arrayButton[index].enabled = false 
      } 
     } 
    } 
+0

정말 고맙습니다. :) –

+0

환영합니다 ........ –

관련 문제