2016-10-01 5 views
1

을 눌러 내 프로젝트의 여섯 개 버튼을 갖고 싶어하고 항상 제외한 숨길 수 싶습니다. 숨겨진 버튼을 누르면 숨겨져 야하고 다른 버튼이 무작위로 나타나야합니다. 누군가 나를 도울 수 있다면 고맙겠습니다 !!표시 버튼을 무작위로 그들에게

+0

내지 5 스택 오버플로! 질문을 향상 시키려면 [질문하는 방법] (http://stackoverflow.com/help/how-to-ask)을 검토하십시오. 시도한 코드와받은 오류를 게시하십시오. 가능한 한 구체적이어야 더 나은 답변으로 이어질 것입니다. – David

답변

4

는 스토리 보드에서 여섯 buttons 만들기를 그들에 태그를 추가 한 다음에 모든 버튼을 연결 한 Action outlet 다음은 어떻게 만들 다음

@IBAction func button_clicked(_ sender: AnyObject) { 
    // generate a random number which is not the same as the tag that you 
    repeat{ 
     random = Int(arc4random_uniform(6) + 1) 
    } 
    while random == sender.tag 

    // iterate through all subviews in your view to find all buttons 
    for view in self.view.subviews{ 
     // make sure it´s a button 
     if view.isKind(of: UIButton.self){ 
      // create a button from the view you're iterating to 
      let button = view as! UIButton 
      // if the button tag is equal to the random number you just created we want to show that button 
      if button.tag == random{ 
       button.isHidden = false 
      } 
      // else hide it 
      else{ 
       button.isHidden = true 
      } 
     } 
    } 
} 

Here 내가 당신이 시도 할 수있는이 작업을 수행을 만든 샘플 프로젝트입니다. 위의 코드에서 주석을 읽고 무슨 일이 일어나는지 확인하십시오.

+0

나를 위해 일하는 고마워요! 그러나 프로젝트에 버튼을 더 추가 할 때는 6 개의 버튼 중 하나를 눌러 사라지게됩니다. 어떻게 해결할 수 있습니까? –

+0

하나만 제외하고 모든 코드를 숨기려면이 코드를 프로젝트에 추가했습니다. 그 확인은? (_ 애니메이션 : BOOL)를 viewDidAppear FUNC 오버라이드 (override) { BT6.isHidden = 사실 BT5.isHidden = 사실 BT4.isHidden = 사실 BT3.isHidden = 사실 BT2.isHidden = 사실 // 숨기기 하나 개를 제외한 모든 버튼을 때 뷰 컨트롤러로드 } –

+0

당신은이 일을 할 필요가 : 1 : (: AnyObject _ 보낸 사람)'2 : button_clicked은'@IBAction의 FUNC에 스토리 보드에서 버튼 액션에 드래그 스토리 보드 –

0

나는 당신이 당신의 스토리 보드에 당신의 여섯 개 버튼을 넣어 클래스로를 연결했다고 가정합니다. 나는 이것을 아주 빨리 했으므로 아마 가장 효과적인 방법은 아니었을 것입니다.

당신은이 같은보고 클래스 '코드를 원하는 것 :

override func viewDidAppear(_ animated: Bool) { 
    BT6.isHidden = true 
    BT5.isHidden = true 
    BT4.isHidden = true 
    BT3.isHidden = true 
    BT2.isHidden = true 
    //Hiding all but one button when the view controller loads 
} 

@IBOutlet weak var BT6: UIButton! 
@IBOutlet weak var BT5: UIButton! 
@IBOutlet weak var BT4: UIButton! 
@IBOutlet weak var BT3: UIButton! 
@IBOutlet weak var BT2: UIButton! 
@IBOutlet weak var BT1: UIButton! 

@IBAction func BT6(_ sender: AnyObject) { 
    //this checks when BT6 is pressed and then hides it 
    BT6.isHidden = true 
    let random = Int(arc4random_uniform(UInt32(4))) 
    if random == 0 { 
     BT5.isHidden = false 
    } else if random == 1 { 
     BT4.isHidden = false 
    } else if random == 2 { 
     BT3.isHidden = false 
    } else if random == 3 { 
     BT2.isHidden = false 
    } else if random == 4 { 
     BT1.isHidden = false 
    } 
    //this part creates a randomiser between 0-4 and depending on which number turns out, it will hide a certain button 
} 
@IBAction func BT5(_ sender: AnyObject) { 
    BT5.isHidden = true 
    let random = Int(arc4random_uniform(UInt32(4))) 
    if random == 0 { 
     BT6.isHidden = false 
    } else if random == 1 { 
     BT4.isHidden = false 
    } else if random == 2 { 
     BT3.isHidden = false 
    } else if random == 3 { 
     BT2.isHidden = false 
    } else if random == 4 { 
     BT1.isHidden = false 
    } 
} 
@IBAction func BT4(_ sender: AnyObject) { 
    BT4.isHidden = true 
    let random = Int(arc4random_uniform(UInt32(4))) 
    if random == 0 { 
     BT5.isHidden = false 
    } else if random == 1 { 
     BT6.isHidden = false 
    } else if random == 2 { 
     BT3.isHidden = false 
    } else if random == 3 { 
     BT2.isHidden = false 
    } else if random == 4 { 
     BT1.isHidden = false 
    } 
} 
@IBAction func BT3(_ sender: AnyObject) { 
    BT3.isHidden = true 
    let random = Int(arc4random_uniform(UInt32(4))) 
    if random == 0 { 
     BT5.isHidden = false 
    } else if random == 1 { 
     BT4.isHidden = false 
    } else if random == 2 { 
     BT6.isHidden = false 
    } else if random == 3 { 
     BT2.isHidden = false 
    } else if random == 4 { 
     BT1.isHidden = false 
    } 
} 
@IBAction func BT2(_ sender: AnyObject) { 
    BT2.isHidden = true 
    let random = Int(arc4random_uniform(UInt32(4))) 
    if random == 0 { 
     BT5.isHidden = false 
    } else if random == 1 { 
     BT4.isHidden = false 
    } else if random == 2 { 
     BT3.isHidden = false 
    } else if random == 3 { 
     BT6.isHidden = false 
    } else if random == 4 { 
     BT1.isHidden = false 
    } 
} 
@IBAction func BT1(_ sender: AnyObject) { 
    BT1.isHidden = true 
    let random = Int(arc4random_uniform(UInt32(4))) 
    if random == 0 { 
     BT5.isHidden = false 
    } else if random == 1 { 
     BT4.isHidden = false 
    } else if random == 2 { 
     BT3.isHidden = false 
    } else if random == 3 { 
     BT2.isHidden = false 
    } else if random == 4 { 
     BT6.isHidden = false 
    } 
} 
+0

많은 도움을 주셔서 감사합니다. –

2

UI (스토리), 여섯 개 버튼 button 태그 번호마다 0에서 할당 된 제 경우 enter image description here

환영

enter image description here

// 
// ViewController.swift 
// StackOverflow 
// 
// Created by Seoksoon Jang on 2016. 10. 1.. 
// Copyright © 2016년 Seoksoon Jang. All rights reserved. 
// 

import UIKit 

class ViewController: UIViewController { 

var buttonTagNumberArray : Array<Int>? 
var randomIndex : Int? 

@IBOutlet var button1: UIButton! 
@IBOutlet var button2: UIButton! 
@IBOutlet var button3: UIButton! 
@IBOutlet var button4: UIButton! 
@IBOutlet var button5: UIButton! 
@IBOutlet var button6: UIButton! 

@IBAction func button1Action(_ sender: AnyObject) { 

    randomIndex = Int(arc4random_uniform(UInt32(buttonTagNumberArray!.count))) 

    if (randomIndex! == button1.tag) { 
     button1Action(button1) 
    } else { 

     button1.isHidden = true 

     switch randomIndex! { 
      case button1.tag : 
       print("it should happen : \(button1.tag)") 
       break 
      case button2.tag : 
       button2.isHidden = false; 
       break 
      case button3.tag : 
       button3.isHidden = false; 
       break 
      case button4.tag : 
       button4.isHidden = false; 
       break 
      case button5.tag : 
       button5.isHidden = false; 
       break 
      case button6.tag : 
       button6.isHidden = false; 
       break 
      default: 
       // 
       break; 
      } 

     return ; 
    } 
} 

@IBAction func button2Action(_ sender: AnyObject) { 

    randomIndex = Int(arc4random_uniform(UInt32(buttonTagNumberArray!.count))) 

    if (randomIndex! == button2.tag) { 
     button2Action(button2) 
    } else { 

     button2.isHidden = true; 

     switch randomIndex! { 
      case button1.tag : 
       button1.isHidden = false; 
       break 
      case button2.tag : 
       print("it should happen : \(button2.tag)") 
       break 
      case button3.tag : 
       button3.isHidden = false; 
       break 
      case button4.tag : 
       button4.isHidden = false; 
       break 
      case button5.tag : 
       button5.isHidden = false; 
       break 
      case button6.tag : 
       button6.isHidden = false; 
       break 
      default: 
       // 
       break; 
     } 

     return ; 
    } 

} 

@IBAction func button3Action(_ sender: AnyObject) { 

    randomIndex = Int(arc4random_uniform(UInt32(buttonTagNumberArray!.count))) 

    if (randomIndex! == button3.tag) { 
     button3Action(button3) 
    } else { 

     button3.isHidden = true; 

     switch randomIndex! { 
      case button1.tag : 
       button1.isHidden = false; 
       break 
      case button2.tag : 
       button2.isHidden = false; 
       break 
      case button3.tag : 
       print("it should happen : \(button2.tag)") 
       break 
      case button4.tag : 
       button4.isHidden = false; 
       break 
      case button5.tag : 
       button5.isHidden = false; 
       break 
      case button6.tag : 
       button6.isHidden = false; 
       break 
      default: 
       // 
       break; 
     } 

     return ; 
    } 

} 

@IBAction func button4Action(_ sender: AnyObject) { 

    randomIndex = Int(arc4random_uniform(UInt32(buttonTagNumberArray!.count))) 

    if (randomIndex! == button4.tag) { 
     button4Action(button4) 
    } else { 

     button4.isHidden = true; 

     switch randomIndex! { 
      case button1.tag : 
       button1.isHidden = false; 
       break 
      case button2.tag : 
       button2.isHidden = false; 
       break 
      case button3.tag : 
       button3.isHidden = false; 
       break 
      case button4.tag : 
       print("it should happen : \(button2.tag)") 
       break 
      case button5.tag : 
       button5.isHidden = false; 
       break 
      case button6.tag : 
       button6.isHidden = false; 
       break 
      default: 
       // 
       break; 
      } 

     return ; 
    } 

} 

@IBAction func button5Action(_ sender: AnyObject) { 

    randomIndex = Int(arc4random_uniform(UInt32(buttonTagNumberArray!.count))) 

    if (randomIndex! == button5.tag) { 
     button5Action(button5) 
    } else { 

     button5.isHidden = true; 

     switch randomIndex! { 
      case button1.tag : 
       button1.isHidden = false; 
       break 
      case button2.tag : 

       break 
      case button3.tag : 
       button3.isHidden = false; 
       break 
      case button4.tag : 
       button4.isHidden = false; 
       break 
      case button5.tag : 
       print("it should happen : \(button2.tag)") 
       break 
      case button6.tag : 
       button6.isHidden = false; 
       break 
      default: 
       // 
       break; 
     } 

     return ; 
    } 

} 

@IBAction func button6Action(_ sender: AnyObject) { 

    randomIndex = Int(arc4random_uniform(UInt32(buttonTagNumberArray!.count))) 

    if (randomIndex! == button6.tag) { 
     button6Action(button6) 
    } else { 

     button6.isHidden = true; 

     switch randomIndex! { 
      case button1.tag : 
       button1.isHidden = false; 
       break 
      case button2.tag : 
       button2.isHidden = false; 
       break 
      case button3.tag : 
       button3.isHidden = false; 
       break 
      case button4.tag : 
       button4.isHidden = false; 
       break 
      case button5.tag : 
       button5.isHidden = false; 
       break 
      case button6.tag : 
       print("it should happen : \(button2.tag)") 
       break 
      default: 
       // 
       break; 
     } 

     return ; 
    } 

} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 
    buttonTagNumberArray = [button1.tag, button2.tag, button3.tag, button4.tag, button5.tag, button6.tag] 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
} // class end 
+0

그냥 재귀를 사용하고 싶었습니다. :피 – boraseoksoon

관련 문제