Passing Data into an Array- Swift
I am looking to pass data through a text box, and into an array (Second View Controller), so it can be added to a pool of data and randomly chosen.
For instance, you would input your name (and/or another name) into the text field in the First VC and and it would go into an array that would tell them which color they are shown in the Second VC.
Essentially, I'm trying to pass the data to a variable (which will hold all the names inputed), and append the variable to the array, then click a button to generate and randomly pull from the array via arc4random. My problem is its not appending. Any help would be greatly appreciated.
Here is my code:
VC 1:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func StartTapped(_ sender: Any) {
performSegue(withIdentifier: "SecondViewControllerSegue", sender: self)
}
@IBOutlet weak var AddPlayerTextField: UITextField!
@IBAction func AddTexttoArrayBtn(_ sender: Any) {
if AddPlayerTextField.text == nil
{
performSegue(withIdentifier: "SecondViewControllerSegue", sender: self)
}
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let SecondViewController = segue.destination as! ViewController2
SecondViewController.myString = AddPlayerTextField.text!
VC 2:
import UIKit
class ViewController2: UIViewController {
var myString = String ()
var TeamBuildingQuestions = [ "is Red", "is Blue", "is Green", "is Purple","is Teal","is Orange", "is Red", "is Grey", "is Pink"]
var MasterTeamBuildingQuestionsArray = [ "Hello", "beep bop bbeep", "Boop", "Bap","Bospop","bob the builder"]
@IBOutlet weak var DisplayArray: UILabel!
@IBAction func NextQuestionBtn(_ sender: Any) {
let RandomArrayNumber = Int (arc4random_uniform(UInt32(TeamBuildingQuestions.count)))
let QuestionDisplayed = TeamBuildingQuestions[RandomArrayNumber]
DisplayArray?.text = QuestionDisplayed
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
TeamBuildingQuestions.append(myString)
}
ios arrays swift segue
add a comment |
I am looking to pass data through a text box, and into an array (Second View Controller), so it can be added to a pool of data and randomly chosen.
For instance, you would input your name (and/or another name) into the text field in the First VC and and it would go into an array that would tell them which color they are shown in the Second VC.
Essentially, I'm trying to pass the data to a variable (which will hold all the names inputed), and append the variable to the array, then click a button to generate and randomly pull from the array via arc4random. My problem is its not appending. Any help would be greatly appreciated.
Here is my code:
VC 1:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func StartTapped(_ sender: Any) {
performSegue(withIdentifier: "SecondViewControllerSegue", sender: self)
}
@IBOutlet weak var AddPlayerTextField: UITextField!
@IBAction func AddTexttoArrayBtn(_ sender: Any) {
if AddPlayerTextField.text == nil
{
performSegue(withIdentifier: "SecondViewControllerSegue", sender: self)
}
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let SecondViewController = segue.destination as! ViewController2
SecondViewController.myString = AddPlayerTextField.text!
VC 2:
import UIKit
class ViewController2: UIViewController {
var myString = String ()
var TeamBuildingQuestions = [ "is Red", "is Blue", "is Green", "is Purple","is Teal","is Orange", "is Red", "is Grey", "is Pink"]
var MasterTeamBuildingQuestionsArray = [ "Hello", "beep bop bbeep", "Boop", "Bap","Bospop","bob the builder"]
@IBOutlet weak var DisplayArray: UILabel!
@IBAction func NextQuestionBtn(_ sender: Any) {
let RandomArrayNumber = Int (arc4random_uniform(UInt32(TeamBuildingQuestions.count)))
let QuestionDisplayed = TeamBuildingQuestions[RandomArrayNumber]
DisplayArray?.text = QuestionDisplayed
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
TeamBuildingQuestions.append(myString)
}
ios arrays swift segue
1
Is this your actual code? There are parentheses missing inAddTexttoArrayBtn
(which should beaddTexTtoArrayBtn
), soprepare(for segue:)
is not being called because it is insideAddTexttoArrayBtn
– Paulw11
Nov 11 at 23:04
Why would you append your text in didRecieveMemoryWarning???
– HAK
Nov 12 at 4:34
Yes it's the actual code, I had some mistakes in it, I'm still new so my apologies. Thanks for catching that for me.
– Victor
Nov 12 at 7:14
add a comment |
I am looking to pass data through a text box, and into an array (Second View Controller), so it can be added to a pool of data and randomly chosen.
For instance, you would input your name (and/or another name) into the text field in the First VC and and it would go into an array that would tell them which color they are shown in the Second VC.
Essentially, I'm trying to pass the data to a variable (which will hold all the names inputed), and append the variable to the array, then click a button to generate and randomly pull from the array via arc4random. My problem is its not appending. Any help would be greatly appreciated.
Here is my code:
VC 1:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func StartTapped(_ sender: Any) {
performSegue(withIdentifier: "SecondViewControllerSegue", sender: self)
}
@IBOutlet weak var AddPlayerTextField: UITextField!
@IBAction func AddTexttoArrayBtn(_ sender: Any) {
if AddPlayerTextField.text == nil
{
performSegue(withIdentifier: "SecondViewControllerSegue", sender: self)
}
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let SecondViewController = segue.destination as! ViewController2
SecondViewController.myString = AddPlayerTextField.text!
VC 2:
import UIKit
class ViewController2: UIViewController {
var myString = String ()
var TeamBuildingQuestions = [ "is Red", "is Blue", "is Green", "is Purple","is Teal","is Orange", "is Red", "is Grey", "is Pink"]
var MasterTeamBuildingQuestionsArray = [ "Hello", "beep bop bbeep", "Boop", "Bap","Bospop","bob the builder"]
@IBOutlet weak var DisplayArray: UILabel!
@IBAction func NextQuestionBtn(_ sender: Any) {
let RandomArrayNumber = Int (arc4random_uniform(UInt32(TeamBuildingQuestions.count)))
let QuestionDisplayed = TeamBuildingQuestions[RandomArrayNumber]
DisplayArray?.text = QuestionDisplayed
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
TeamBuildingQuestions.append(myString)
}
ios arrays swift segue
I am looking to pass data through a text box, and into an array (Second View Controller), so it can be added to a pool of data and randomly chosen.
For instance, you would input your name (and/or another name) into the text field in the First VC and and it would go into an array that would tell them which color they are shown in the Second VC.
Essentially, I'm trying to pass the data to a variable (which will hold all the names inputed), and append the variable to the array, then click a button to generate and randomly pull from the array via arc4random. My problem is its not appending. Any help would be greatly appreciated.
Here is my code:
VC 1:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func StartTapped(_ sender: Any) {
performSegue(withIdentifier: "SecondViewControllerSegue", sender: self)
}
@IBOutlet weak var AddPlayerTextField: UITextField!
@IBAction func AddTexttoArrayBtn(_ sender: Any) {
if AddPlayerTextField.text == nil
{
performSegue(withIdentifier: "SecondViewControllerSegue", sender: self)
}
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let SecondViewController = segue.destination as! ViewController2
SecondViewController.myString = AddPlayerTextField.text!
VC 2:
import UIKit
class ViewController2: UIViewController {
var myString = String ()
var TeamBuildingQuestions = [ "is Red", "is Blue", "is Green", "is Purple","is Teal","is Orange", "is Red", "is Grey", "is Pink"]
var MasterTeamBuildingQuestionsArray = [ "Hello", "beep bop bbeep", "Boop", "Bap","Bospop","bob the builder"]
@IBOutlet weak var DisplayArray: UILabel!
@IBAction func NextQuestionBtn(_ sender: Any) {
let RandomArrayNumber = Int (arc4random_uniform(UInt32(TeamBuildingQuestions.count)))
let QuestionDisplayed = TeamBuildingQuestions[RandomArrayNumber]
DisplayArray?.text = QuestionDisplayed
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
TeamBuildingQuestions.append(myString)
}
ios arrays swift segue
ios arrays swift segue
edited Nov 11 at 22:56
Paulw11
67k1081101
67k1081101
asked Nov 11 at 22:47
Victor
61
61
1
Is this your actual code? There are parentheses missing inAddTexttoArrayBtn
(which should beaddTexTtoArrayBtn
), soprepare(for segue:)
is not being called because it is insideAddTexttoArrayBtn
– Paulw11
Nov 11 at 23:04
Why would you append your text in didRecieveMemoryWarning???
– HAK
Nov 12 at 4:34
Yes it's the actual code, I had some mistakes in it, I'm still new so my apologies. Thanks for catching that for me.
– Victor
Nov 12 at 7:14
add a comment |
1
Is this your actual code? There are parentheses missing inAddTexttoArrayBtn
(which should beaddTexTtoArrayBtn
), soprepare(for segue:)
is not being called because it is insideAddTexttoArrayBtn
– Paulw11
Nov 11 at 23:04
Why would you append your text in didRecieveMemoryWarning???
– HAK
Nov 12 at 4:34
Yes it's the actual code, I had some mistakes in it, I'm still new so my apologies. Thanks for catching that for me.
– Victor
Nov 12 at 7:14
1
1
Is this your actual code? There are parentheses missing in
AddTexttoArrayBtn
(which should be addTexTtoArrayBtn
), so prepare(for segue:)
is not being called because it is inside AddTexttoArrayBtn
– Paulw11
Nov 11 at 23:04
Is this your actual code? There are parentheses missing in
AddTexttoArrayBtn
(which should be addTexTtoArrayBtn
), so prepare(for segue:)
is not being called because it is inside AddTexttoArrayBtn
– Paulw11
Nov 11 at 23:04
Why would you append your text in didRecieveMemoryWarning???
– HAK
Nov 12 at 4:34
Why would you append your text in didRecieveMemoryWarning???
– HAK
Nov 12 at 4:34
Yes it's the actual code, I had some mistakes in it, I'm still new so my apologies. Thanks for catching that for me.
– Victor
Nov 12 at 7:14
Yes it's the actual code, I had some mistakes in it, I'm still new so my apologies. Thanks for catching that for me.
– Victor
Nov 12 at 7:14
add a comment |
2 Answers
2
active
oldest
votes
In VC2, Instead of didReceiveMemoryWarning, implement TeamBuildingQuestions.append(myString) in viewDidLoad.
Thanks so much for catching it, I'll input it and see how it works.
– Victor
Nov 12 at 7:14
add a comment |
Here is a bug:
if AddPlayerTextField.text == nil{
}
should be
if AddPlayerTextField.text != nil && AddPlayerTextField.text.count > 0{
}
Thanks I'm just changing everything right now, I'll update on how it goes.
– Victor
Nov 12 at 7:13
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254009%2fpassing-data-into-an-array-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In VC2, Instead of didReceiveMemoryWarning, implement TeamBuildingQuestions.append(myString) in viewDidLoad.
Thanks so much for catching it, I'll input it and see how it works.
– Victor
Nov 12 at 7:14
add a comment |
In VC2, Instead of didReceiveMemoryWarning, implement TeamBuildingQuestions.append(myString) in viewDidLoad.
Thanks so much for catching it, I'll input it and see how it works.
– Victor
Nov 12 at 7:14
add a comment |
In VC2, Instead of didReceiveMemoryWarning, implement TeamBuildingQuestions.append(myString) in viewDidLoad.
In VC2, Instead of didReceiveMemoryWarning, implement TeamBuildingQuestions.append(myString) in viewDidLoad.
answered Nov 11 at 23:11
yuoku
361
361
Thanks so much for catching it, I'll input it and see how it works.
– Victor
Nov 12 at 7:14
add a comment |
Thanks so much for catching it, I'll input it and see how it works.
– Victor
Nov 12 at 7:14
Thanks so much for catching it, I'll input it and see how it works.
– Victor
Nov 12 at 7:14
Thanks so much for catching it, I'll input it and see how it works.
– Victor
Nov 12 at 7:14
add a comment |
Here is a bug:
if AddPlayerTextField.text == nil{
}
should be
if AddPlayerTextField.text != nil && AddPlayerTextField.text.count > 0{
}
Thanks I'm just changing everything right now, I'll update on how it goes.
– Victor
Nov 12 at 7:13
add a comment |
Here is a bug:
if AddPlayerTextField.text == nil{
}
should be
if AddPlayerTextField.text != nil && AddPlayerTextField.text.count > 0{
}
Thanks I'm just changing everything right now, I'll update on how it goes.
– Victor
Nov 12 at 7:13
add a comment |
Here is a bug:
if AddPlayerTextField.text == nil{
}
should be
if AddPlayerTextField.text != nil && AddPlayerTextField.text.count > 0{
}
Here is a bug:
if AddPlayerTextField.text == nil{
}
should be
if AddPlayerTextField.text != nil && AddPlayerTextField.text.count > 0{
}
answered Nov 12 at 2:57
E.Coms
1,6302412
1,6302412
Thanks I'm just changing everything right now, I'll update on how it goes.
– Victor
Nov 12 at 7:13
add a comment |
Thanks I'm just changing everything right now, I'll update on how it goes.
– Victor
Nov 12 at 7:13
Thanks I'm just changing everything right now, I'll update on how it goes.
– Victor
Nov 12 at 7:13
Thanks I'm just changing everything right now, I'll update on how it goes.
– Victor
Nov 12 at 7:13
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254009%2fpassing-data-into-an-array-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Is this your actual code? There are parentheses missing in
AddTexttoArrayBtn
(which should beaddTexTtoArrayBtn
), soprepare(for segue:)
is not being called because it is insideAddTexttoArrayBtn
– Paulw11
Nov 11 at 23:04
Why would you append your text in didRecieveMemoryWarning???
– HAK
Nov 12 at 4:34
Yes it's the actual code, I had some mistakes in it, I'm still new so my apologies. Thanks for catching that for me.
– Victor
Nov 12 at 7:14