【Swift5】퀴즈 앱으로 난이도별로 문제를 표시하는 방법 【초보자】
소개
실무 미경험으로 Swift를 독학으로 공부하고 있습니다.
배운 것을 출력하고 싶습니다.
초학자 때문에 실수가 있을지도 모릅니다.
그 때는 가르쳐 주시면 다행입니다.
환경
macOS Catalina10.15.4
Xcode11.7
하고 싶은 일
'레벨별로 문제 표시'
퀴즈 앱을 만들 때 난이도별이나 종류별로 문제를 만들고 싶었습니다.
그러나, 해당하는 방법을 조사해도 모르기 때문에, 정리하기로 했습니다.
완성형

방법
1, story board로 버튼에 tag 번호를 붙인다


2, 태그 번호 보내기
OneViewController.swift @IBAction func levelBtnAction(sender: UIButton) {
//buttonに設定されたtagを取得
let number = sender.tag
//タグをsenderでprepaerへ送る
self.performSegue(withIdentifier: "nextVC", sender: number)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "nextVC" {
//遷移先のVCを取得
if let secondVC = segue.destination as? TwoViewController {
secondVC.tappedButtonTag = sender as? Int
}
}
}
참고 : TappedButtonTag 변수를 TwoViewController에 만들지 않으면 "secondVC.tappedButtonTag"라고 쓸 때 tapedButtonTag가 나오지 않습니다.
3, 변수 TappedButtonTag 준비
TwoViewContoroller.swift //tagを入れる変数を設定
var tappedButtonTag: Int?
4, tag 번호를 switch 문으로 판정을 한다
TwoViewController.swift override func viewDidLoad() {
super.viewDidLoad()
//タグの値を表示
print(tappedButtonTag as Any)
switch tappedButtonTag {
case 0:
// print("レベル1の問題を表示")
problemLabel.text = "レベル1の問題を表示"
case 1:
// print("レベル2の問題を表示")
problemLabel.text = "レベル2の問題を表示"
case 2:
// print("レベル3の問題を表示")
problemLabel.text = "レベル3の問題を表示"
default:
print("値が設定されていません")
}
}
이하, 코드 전문
OneviewController.swiftimport UIKit
class OneViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func levelBtnAction(sender: UIButton) {
//buttonに設定されたtagを取得
let number = sender.tag
//タグをsenderでprepaerへ送る
self.performSegue(withIdentifier: "nextVC", sender: number)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "nextVC" {
//遷移先のVCを取得
if let secondVC = segue.destination as? TwoViewController {
secondVC.tappedButtonTag = sender as? Int
}
}
}
}
TwoViewController.swiftimport UIKit
class TwoViewController: UIViewController {
@IBOutlet weak var problemLabel: UILabel!
//tagを入れる変数を設定
var tappedButtonTag: Int?
override func viewDidLoad() {
super.viewDidLoad()
//タグの値を表示
print(tappedButtonTag as Any)
switch tappedButtonTag {
case 0:
// print("レベル1の問題を表示")
problemLabel.text = "レベル1の問題を表示"
case 1:
// print("レベル2の問題を表示")
problemLabel.text = "レベル2の問題を表示"
case 2:
// print("レベル3の問題を表示")
problemLabel.text = "レベル3の問題を表示"
default:
print("値が設定されていません")
}
}
}
참고 사이트
htps : // 하고 싶다 l. 코m/쿠에 s치온 s/161439
htps : // 코 m / 미사키 아가타 / ms / b7f6c2f6c9f988에 c38c7
Reference
이 문제에 관하여(【Swift5】퀴즈 앱으로 난이도별로 문제를 표시하는 방법 【초보자】), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/kapiemon/items/5e19362aa3899318a482
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
macOS Catalina10.15.4
Xcode11.7
하고 싶은 일
'레벨별로 문제 표시'
퀴즈 앱을 만들 때 난이도별이나 종류별로 문제를 만들고 싶었습니다.
그러나, 해당하는 방법을 조사해도 모르기 때문에, 정리하기로 했습니다.
완성형

방법
1, story board로 버튼에 tag 번호를 붙인다


2, 태그 번호 보내기
OneViewController.swift @IBAction func levelBtnAction(sender: UIButton) {
//buttonに設定されたtagを取得
let number = sender.tag
//タグをsenderでprepaerへ送る
self.performSegue(withIdentifier: "nextVC", sender: number)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "nextVC" {
//遷移先のVCを取得
if let secondVC = segue.destination as? TwoViewController {
secondVC.tappedButtonTag = sender as? Int
}
}
}
참고 : TappedButtonTag 변수를 TwoViewController에 만들지 않으면 "secondVC.tappedButtonTag"라고 쓸 때 tapedButtonTag가 나오지 않습니다.
3, 변수 TappedButtonTag 준비
TwoViewContoroller.swift //tagを入れる変数を設定
var tappedButtonTag: Int?
4, tag 번호를 switch 문으로 판정을 한다
TwoViewController.swift override func viewDidLoad() {
super.viewDidLoad()
//タグの値を表示
print(tappedButtonTag as Any)
switch tappedButtonTag {
case 0:
// print("レベル1の問題を表示")
problemLabel.text = "レベル1の問題を表示"
case 1:
// print("レベル2の問題を表示")
problemLabel.text = "レベル2の問題を表示"
case 2:
// print("レベル3の問題を表示")
problemLabel.text = "レベル3の問題を表示"
default:
print("値が設定されていません")
}
}
이하, 코드 전문
OneviewController.swiftimport UIKit
class OneViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func levelBtnAction(sender: UIButton) {
//buttonに設定されたtagを取得
let number = sender.tag
//タグをsenderでprepaerへ送る
self.performSegue(withIdentifier: "nextVC", sender: number)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "nextVC" {
//遷移先のVCを取得
if let secondVC = segue.destination as? TwoViewController {
secondVC.tappedButtonTag = sender as? Int
}
}
}
}
TwoViewController.swiftimport UIKit
class TwoViewController: UIViewController {
@IBOutlet weak var problemLabel: UILabel!
//tagを入れる変数を設定
var tappedButtonTag: Int?
override func viewDidLoad() {
super.viewDidLoad()
//タグの値を表示
print(tappedButtonTag as Any)
switch tappedButtonTag {
case 0:
// print("レベル1の問題を表示")
problemLabel.text = "レベル1の問題を表示"
case 1:
// print("レベル2の問題を表示")
problemLabel.text = "レベル2の問題を表示"
case 2:
// print("レベル3の問題を表示")
problemLabel.text = "レベル3の問題を表示"
default:
print("値が設定されていません")
}
}
}
참고 사이트
htps : // 하고 싶다 l. 코m/쿠에 s치온 s/161439
htps : // 코 m / 미사키 아가타 / ms / b7f6c2f6c9f988에 c38c7
Reference
이 문제에 관하여(【Swift5】퀴즈 앱으로 난이도별로 문제를 표시하는 방법 【초보자】), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/kapiemon/items/5e19362aa3899318a482
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)

방법
1, story board로 버튼에 tag 번호를 붙인다


2, 태그 번호 보내기
OneViewController.swift @IBAction func levelBtnAction(sender: UIButton) {
//buttonに設定されたtagを取得
let number = sender.tag
//タグをsenderでprepaerへ送る
self.performSegue(withIdentifier: "nextVC", sender: number)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "nextVC" {
//遷移先のVCを取得
if let secondVC = segue.destination as? TwoViewController {
secondVC.tappedButtonTag = sender as? Int
}
}
}
참고 : TappedButtonTag 변수를 TwoViewController에 만들지 않으면 "secondVC.tappedButtonTag"라고 쓸 때 tapedButtonTag가 나오지 않습니다.
3, 변수 TappedButtonTag 준비
TwoViewContoroller.swift //tagを入れる変数を設定
var tappedButtonTag: Int?
4, tag 번호를 switch 문으로 판정을 한다
TwoViewController.swift override func viewDidLoad() {
super.viewDidLoad()
//タグの値を表示
print(tappedButtonTag as Any)
switch tappedButtonTag {
case 0:
// print("レベル1の問題を表示")
problemLabel.text = "レベル1の問題を表示"
case 1:
// print("レベル2の問題を表示")
problemLabel.text = "レベル2の問題を表示"
case 2:
// print("レベル3の問題を表示")
problemLabel.text = "レベル3の問題を表示"
default:
print("値が設定されていません")
}
}
이하, 코드 전문
OneviewController.swiftimport UIKit
class OneViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func levelBtnAction(sender: UIButton) {
//buttonに設定されたtagを取得
let number = sender.tag
//タグをsenderでprepaerへ送る
self.performSegue(withIdentifier: "nextVC", sender: number)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "nextVC" {
//遷移先のVCを取得
if let secondVC = segue.destination as? TwoViewController {
secondVC.tappedButtonTag = sender as? Int
}
}
}
}
TwoViewController.swiftimport UIKit
class TwoViewController: UIViewController {
@IBOutlet weak var problemLabel: UILabel!
//tagを入れる変数を設定
var tappedButtonTag: Int?
override func viewDidLoad() {
super.viewDidLoad()
//タグの値を表示
print(tappedButtonTag as Any)
switch tappedButtonTag {
case 0:
// print("レベル1の問題を表示")
problemLabel.text = "レベル1の問題を表示"
case 1:
// print("レベル2の問題を表示")
problemLabel.text = "レベル2の問題を表示"
case 2:
// print("レベル3の問題を表示")
problemLabel.text = "レベル3の問題を表示"
default:
print("値が設定されていません")
}
}
}
참고 사이트
htps : // 하고 싶다 l. 코m/쿠에 s치온 s/161439
htps : // 코 m / 미사키 아가타 / ms / b7f6c2f6c9f988에 c38c7
Reference
이 문제에 관하여(【Swift5】퀴즈 앱으로 난이도별로 문제를 표시하는 방법 【초보자】), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/kapiemon/items/5e19362aa3899318a482
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
@IBAction func levelBtnAction(sender: UIButton) {
//buttonに設定されたtagを取得
let number = sender.tag
//タグをsenderでprepaerへ送る
self.performSegue(withIdentifier: "nextVC", sender: number)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "nextVC" {
//遷移先のVCを取得
if let secondVC = segue.destination as? TwoViewController {
secondVC.tappedButtonTag = sender as? Int
}
}
}
//tagを入れる変数を設定
var tappedButtonTag: Int?
override func viewDidLoad() {
super.viewDidLoad()
//タグの値を表示
print(tappedButtonTag as Any)
switch tappedButtonTag {
case 0:
// print("レベル1の問題を表示")
problemLabel.text = "レベル1の問題を表示"
case 1:
// print("レベル2の問題を表示")
problemLabel.text = "レベル2の問題を表示"
case 2:
// print("レベル3の問題を表示")
problemLabel.text = "レベル3の問題を表示"
default:
print("値が設定されていません")
}
}
import UIKit
class OneViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func levelBtnAction(sender: UIButton) {
//buttonに設定されたtagを取得
let number = sender.tag
//タグをsenderでprepaerへ送る
self.performSegue(withIdentifier: "nextVC", sender: number)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "nextVC" {
//遷移先のVCを取得
if let secondVC = segue.destination as? TwoViewController {
secondVC.tappedButtonTag = sender as? Int
}
}
}
}
import UIKit
class TwoViewController: UIViewController {
@IBOutlet weak var problemLabel: UILabel!
//tagを入れる変数を設定
var tappedButtonTag: Int?
override func viewDidLoad() {
super.viewDidLoad()
//タグの値を表示
print(tappedButtonTag as Any)
switch tappedButtonTag {
case 0:
// print("レベル1の問題を表示")
problemLabel.text = "レベル1の問題を表示"
case 1:
// print("レベル2の問題を表示")
problemLabel.text = "レベル2の問題を表示"
case 2:
// print("レベル3の問題を表示")
problemLabel.text = "レベル3の問題を表示"
default:
print("値が設定されていません")
}
}
}
Reference
이 문제에 관하여(【Swift5】퀴즈 앱으로 난이도별로 문제를 표시하는 방법 【초보자】), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/kapiemon/items/5e19362aa3899318a482텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)