을 눌러 내 프로젝트의 여섯 개 버튼을 갖고 싶어하고 항상 제외한 숨길 수 싶습니다. 숨겨진 버튼을 누르면 숨겨져 야하고 다른 버튼이 무작위로 나타나야합니다. 누군가 나를 도울 수 있다면 고맙겠습니다 !!표시 버튼을 무작위로 그들에게
답변
는 스토리 보드에서 여섯 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 내가 당신이 시도 할 수있는이 작업을 수행을 만든 샘플 프로젝트입니다. 위의 코드에서 주석을 읽고 무슨 일이 일어나는지 확인하십시오.
나를 위해 일하는 고마워요! 그러나 프로젝트에 버튼을 더 추가 할 때는 6 개의 버튼 중 하나를 눌러 사라지게됩니다. 어떻게 해결할 수 있습니까? –
하나만 제외하고 모든 코드를 숨기려면이 코드를 프로젝트에 추가했습니다. 그 확인은? (_ 애니메이션 : BOOL)를 viewDidAppear FUNC 오버라이드 (override) { BT6.isHidden = 사실 BT5.isHidden = 사실 BT4.isHidden = 사실 BT3.isHidden = 사실 BT2.isHidden = 사실 // 숨기기 하나 개를 제외한 모든 버튼을 때 뷰 컨트롤러로드 } –
당신은이 일을 할 필요가 : 1 : (: AnyObject _ 보낸 사람)'2 : button_clicked은'@IBAction의 FUNC에 스토리 보드에서 버튼 액션에 드래그 스토리 보드 –
나는 당신이 당신의 스토리 보드에 당신의 여섯 개 버튼을 넣어 클래스로를 연결했다고 가정합니다. 나는 이것을 아주 빨리 했으므로 아마 가장 효과적인 방법은 아니었을 것입니다.
당신은이 같은보고 클래스 '코드를 원하는 것 :
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
}
}
많은 도움을 주셔서 감사합니다. –
UI (스토리), 여섯 개 버튼 button 태그 번호마다 0에서 할당 된 제 경우
환영
//
// 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
그냥 재귀를 사용하고 싶었습니다. :피 – boraseoksoon
- 1. 자바에서 무작위로 버튼을 선택
- 2. 무작위로 버튼을 시작하는 방법
- 3. 그들에게
- 4. 무작위로 문자열 표시 -
- 5. 무작위로 표시 목록 항목
- 6. 무작위로 문자열을 표시
- 7. 버튼을 통해 애니메이션을 무작위로 순환합니다.
- 8. 어레이에서 as3의 버튼을 무작위로 배열하십시오.
- 9. Im Xcode를 사용하고 무작위로 버튼을 표시하고 싶습니다.
- 10. cocos2d에서 무작위로 다른 스프라이트를 표시
- 11. 데이터베이스에서 무작위로 항목을 내보기로 표시
- 12. 무작위로 기록하고 사용중인 것으로 표시
- 13. 하스켈, 그들에게
- 14. ImmutableJS : 그들에게
- 15. Tkinter GUI에 버튼을 무작위로 추가 하시겠습니까?
- 16. 어떻게하면 버튼을 무작위로 클릭 할 수 있습니까?
- 17. 나는 정적과 같이 UIActionSheet에 버튼을 추가 할 때 동적으로, 그들에게
- 18. 하는 크기를 다시 그들에게
- 19. get_sub_field();를 사용하여 무작위로 두 개의 텍스트 필드와 버튼을?
- 20. PHP - 무작위로
- 21. Tkinter를 사용하여 하나의 창으로 무작위로 이미지 표시
- 22. 7 세그먼트에 무작위로 생성 된 번호 표시
- 23. 배열의 레이블에 무작위로 선택된 텍스트 표시
- 24. 무작위로 생성 된 정수의 빈도 표시
- 25. 노드 표시 순서를 무작위로 지정 하시겠습니까?
- 26. DIV 어린이를 하나씩 또는 무작위로 페이지로드로 표시
- 27. .txt 파일의 텍스트 줄을 무작위로 표시 | rails
- 28. 제출 버튼을 이미지로 표시
- 29. 라디오 버튼을 인라인으로 표시
- 30. 버튼을 표시/숨기기
내지 5 스택 오버플로! 질문을 향상 시키려면 [질문하는 방법] (http://stackoverflow.com/help/how-to-ask)을 검토하십시오. 시도한 코드와받은 오류를 게시하십시오. 가능한 한 구체적이어야 더 나은 답변으로 이어질 것입니다. – David