Emit different text and sound when each of three buttons is pressed
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
I would like to use this same code on multiple pages of photos (to help my son recognize people). So instead of reinserting the name of each person, I created the appearingInPhoto
(tuple or array), so I can utilize just .a .b .c, instead of having to go and rename each line of code.
Is there a way to simplify the code further, so I can just have code that checks if a button is pressed (check that sender.currentTitle
is equal to any of the tuple values) and if yes, run this code?
print("pressed (appearingInPhoto.a)")
label.text = appearingInPhoto.a
soundName = appearingInPhoto.a
This is the full function now:
var appearingInPhoto = (a:"omar", b:"john", c:"thomas")
@IBAction func buttonPressed(_ sender: UIButton)
var soundName: String? = nil
if sender.currentTitle == appearingInPhoto.a
print("pressed (appearingInPhoto.a)")
label.text = appearingInPhoto.a
soundName = appearingInPhoto.a
else if sender.currentTitle == appearingInPhoto.b
print("pressed (appearingInPhoto.b)")
label.text = appearingInPhoto.b
soundName = appearingInPhoto.b
else if sender.currentTitle == appearingInPhoto.c
print("pressed (appearingInPhoto.c)")
label.text = appearingInPhoto.c
soundName = appearingInPhoto.c
if let soundName = soundName
playSoundFile(soundName)
swift event-handling
bumped to the homepage by Community⦠2 days ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |Â
up vote
0
down vote
favorite
I would like to use this same code on multiple pages of photos (to help my son recognize people). So instead of reinserting the name of each person, I created the appearingInPhoto
(tuple or array), so I can utilize just .a .b .c, instead of having to go and rename each line of code.
Is there a way to simplify the code further, so I can just have code that checks if a button is pressed (check that sender.currentTitle
is equal to any of the tuple values) and if yes, run this code?
print("pressed (appearingInPhoto.a)")
label.text = appearingInPhoto.a
soundName = appearingInPhoto.a
This is the full function now:
var appearingInPhoto = (a:"omar", b:"john", c:"thomas")
@IBAction func buttonPressed(_ sender: UIButton)
var soundName: String? = nil
if sender.currentTitle == appearingInPhoto.a
print("pressed (appearingInPhoto.a)")
label.text = appearingInPhoto.a
soundName = appearingInPhoto.a
else if sender.currentTitle == appearingInPhoto.b
print("pressed (appearingInPhoto.b)")
label.text = appearingInPhoto.b
soundName = appearingInPhoto.b
else if sender.currentTitle == appearingInPhoto.c
print("pressed (appearingInPhoto.c)")
label.text = appearingInPhoto.c
soundName = appearingInPhoto.c
if let soundName = soundName
playSoundFile(soundName)
swift event-handling
bumped to the homepage by Community⦠2 days ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
You're using the wrong data type. You want an array, not a dictionary.var appearingInPhoto:[String] = ["omar","john","thomas"]
Then loop over it
â Snowbody
Jan 12 at 22:36
1
I feel that you need a model to save all information related to the user that you will show. Also detecting the appearing person by comparing the text of the button is not considered safe. if you can describe more what you want top achieve or a screenshot of the screen that you have implanted to understand the context then we can help to improve the implementation
â iOSGeek
Jan 16 at 20:30
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I would like to use this same code on multiple pages of photos (to help my son recognize people). So instead of reinserting the name of each person, I created the appearingInPhoto
(tuple or array), so I can utilize just .a .b .c, instead of having to go and rename each line of code.
Is there a way to simplify the code further, so I can just have code that checks if a button is pressed (check that sender.currentTitle
is equal to any of the tuple values) and if yes, run this code?
print("pressed (appearingInPhoto.a)")
label.text = appearingInPhoto.a
soundName = appearingInPhoto.a
This is the full function now:
var appearingInPhoto = (a:"omar", b:"john", c:"thomas")
@IBAction func buttonPressed(_ sender: UIButton)
var soundName: String? = nil
if sender.currentTitle == appearingInPhoto.a
print("pressed (appearingInPhoto.a)")
label.text = appearingInPhoto.a
soundName = appearingInPhoto.a
else if sender.currentTitle == appearingInPhoto.b
print("pressed (appearingInPhoto.b)")
label.text = appearingInPhoto.b
soundName = appearingInPhoto.b
else if sender.currentTitle == appearingInPhoto.c
print("pressed (appearingInPhoto.c)")
label.text = appearingInPhoto.c
soundName = appearingInPhoto.c
if let soundName = soundName
playSoundFile(soundName)
swift event-handling
I would like to use this same code on multiple pages of photos (to help my son recognize people). So instead of reinserting the name of each person, I created the appearingInPhoto
(tuple or array), so I can utilize just .a .b .c, instead of having to go and rename each line of code.
Is there a way to simplify the code further, so I can just have code that checks if a button is pressed (check that sender.currentTitle
is equal to any of the tuple values) and if yes, run this code?
print("pressed (appearingInPhoto.a)")
label.text = appearingInPhoto.a
soundName = appearingInPhoto.a
This is the full function now:
var appearingInPhoto = (a:"omar", b:"john", c:"thomas")
@IBAction func buttonPressed(_ sender: UIButton)
var soundName: String? = nil
if sender.currentTitle == appearingInPhoto.a
print("pressed (appearingInPhoto.a)")
label.text = appearingInPhoto.a
soundName = appearingInPhoto.a
else if sender.currentTitle == appearingInPhoto.b
print("pressed (appearingInPhoto.b)")
label.text = appearingInPhoto.b
soundName = appearingInPhoto.b
else if sender.currentTitle == appearingInPhoto.c
print("pressed (appearingInPhoto.c)")
label.text = appearingInPhoto.c
soundName = appearingInPhoto.c
if let soundName = soundName
playSoundFile(soundName)
swift event-handling
edited Jan 13 at 1:12
200_success
123k14143401
123k14143401
asked Jan 12 at 22:31
mikeT
61
61
bumped to the homepage by Community⦠2 days ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community⦠2 days ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
You're using the wrong data type. You want an array, not a dictionary.var appearingInPhoto:[String] = ["omar","john","thomas"]
Then loop over it
â Snowbody
Jan 12 at 22:36
1
I feel that you need a model to save all information related to the user that you will show. Also detecting the appearing person by comparing the text of the button is not considered safe. if you can describe more what you want top achieve or a screenshot of the screen that you have implanted to understand the context then we can help to improve the implementation
â iOSGeek
Jan 16 at 20:30
add a comment |Â
You're using the wrong data type. You want an array, not a dictionary.var appearingInPhoto:[String] = ["omar","john","thomas"]
Then loop over it
â Snowbody
Jan 12 at 22:36
1
I feel that you need a model to save all information related to the user that you will show. Also detecting the appearing person by comparing the text of the button is not considered safe. if you can describe more what you want top achieve or a screenshot of the screen that you have implanted to understand the context then we can help to improve the implementation
â iOSGeek
Jan 16 at 20:30
You're using the wrong data type. You want an array, not a dictionary.
var appearingInPhoto:[String] = ["omar","john","thomas"]
Then loop over itâ Snowbody
Jan 12 at 22:36
You're using the wrong data type. You want an array, not a dictionary.
var appearingInPhoto:[String] = ["omar","john","thomas"]
Then loop over itâ Snowbody
Jan 12 at 22:36
1
1
I feel that you need a model to save all information related to the user that you will show. Also detecting the appearing person by comparing the text of the button is not considered safe. if you can describe more what you want top achieve or a screenshot of the screen that you have implanted to understand the context then we can help to improve the implementation
â iOSGeek
Jan 16 at 20:30
I feel that you need a model to save all information related to the user that you will show. Also detecting the appearing person by comparing the text of the button is not considered safe. if you can describe more what you want top achieve or a screenshot of the screen that you have implanted to understand the context then we can help to improve the implementation
â iOSGeek
Jan 16 at 20:30
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
Slightly modified (using array):
var appearingInPhoto = ["omar", "john", "thomas"]
@IBAction func buttonPressed(_ sender: UIButton)
guard let soundName = sender.currentTitle else
return
if appearingInPhoto.contains(soundName)
print("pressed (soundName)")
label.text = soundName
playSoundFile(soundName)
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Slightly modified (using array):
var appearingInPhoto = ["omar", "john", "thomas"]
@IBAction func buttonPressed(_ sender: UIButton)
guard let soundName = sender.currentTitle else
return
if appearingInPhoto.contains(soundName)
print("pressed (soundName)")
label.text = soundName
playSoundFile(soundName)
add a comment |Â
up vote
0
down vote
Slightly modified (using array):
var appearingInPhoto = ["omar", "john", "thomas"]
@IBAction func buttonPressed(_ sender: UIButton)
guard let soundName = sender.currentTitle else
return
if appearingInPhoto.contains(soundName)
print("pressed (soundName)")
label.text = soundName
playSoundFile(soundName)
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Slightly modified (using array):
var appearingInPhoto = ["omar", "john", "thomas"]
@IBAction func buttonPressed(_ sender: UIButton)
guard let soundName = sender.currentTitle else
return
if appearingInPhoto.contains(soundName)
print("pressed (soundName)")
label.text = soundName
playSoundFile(soundName)
Slightly modified (using array):
var appearingInPhoto = ["omar", "john", "thomas"]
@IBAction func buttonPressed(_ sender: UIButton)
guard let soundName = sender.currentTitle else
return
if appearingInPhoto.contains(soundName)
print("pressed (soundName)")
label.text = soundName
playSoundFile(soundName)
answered Feb 18 at 21:18
Tomasz Czyà ¼ak
1414
1414
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184994%2femit-different-text-and-sound-when-each-of-three-buttons-is-pressed%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
You're using the wrong data type. You want an array, not a dictionary.
var appearingInPhoto:[String] = ["omar","john","thomas"]
Then loop over itâ Snowbody
Jan 12 at 22:36
1
I feel that you need a model to save all information related to the user that you will show. Also detecting the appearing person by comparing the text of the button is not considered safe. if you can describe more what you want top achieve or a screenshot of the screen that you have implanted to understand the context then we can help to improve the implementation
â iOSGeek
Jan 16 at 20:30