I'm doing a QR code scanner quiz app where users must get a score of 10 from doing 10 questions. After a user the 1st qn, the score will plus 1 and it will revert them back to the qr scanner page where they must scan the QR code for the next qn. The problem is the passing of the score data. Is there a way to do it without segue?
This is my qn1controller
import UIKit
class Quiz1Controller: UIViewController {
#IBOutlet var question: UILabel!
#IBOutlet var button1: UIButton!
#IBOutlet var button2: UIButton!
#IBOutlet var button3: UIButton!
#IBOutlet var button4: UIButton!
#IBOutlet var LabelEnd: UILabel!
#IBOutlet var scorelabel: UILabel!
var score = Int()
var CorrectAnswer = String()
override func viewDidLoad() {
super.viewDidLoad()
RandomQuestions()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func RandomQuestions(){
var RandomNumber = arc4random() % 1
RandomNumber += 1
switch(RandomNumber){
case 1:
question.text = "Who is the current Deputy Chairman of People's Association?"
button1.setTitle("Lee Hsien Loong", for: .normal)
button2.setTitle("Chan Chun Sing", for: .normal)
button3.setTitle("Goh Chok Tong", for: .normal)
button4.setTitle("Goh Khen Swee", for: .normal)
CorrectAnswer = "2"
Hide()
break
default:
break
}
}
func Hide(){
LabelEnd.isHidden = true
}
func UnHide(){
LabelEnd.isHidden = false
}
#IBAction func Button1(_ sender: Any) {
UnHide()
if (CorrectAnswer == "1"){
self.performSegue(withIdentifier: "correct", sender: self)
}
else {
LabelEnd.text = "Incorrect Answer! Try again"
}
}
#IBAction func Button2(_ sender: Any) {
UnHide()
if (CorrectAnswer == "2"){
score = score + 1
scorelabel.text = "Score:\(score)"
self.performSegue(withIdentifier: "correct", sender: self)
}
else {
LabelEnd.text = "Incorrect Answer! Try again"
}
}
#IBAction func Button3(_ sender: Any) {
UnHide()
if (CorrectAnswer == "3"){
self.performSegue(withIdentifier: "correct", sender: self)
}
else {
LabelEnd.text = "Incorrect Answer! Try again"
}
}
#IBAction func Button4(_ sender: Any) {
UnHide()
if (CorrectAnswer == "4"){
self.performSegue(withIdentifier: "correct", sender: self)
}
else {
LabelEnd.text = "Incorrect Answer! Try again"
}
}
}
And this is my qn2 controller
import UIKit
class Quiz2Controller: UIViewController {
#IBOutlet var question: UILabel!
#IBOutlet var button1: UIButton!
#IBOutlet var button2: UIButton!
#IBOutlet var button3: UIButton!
#IBOutlet var button4: UIButton!
#IBOutlet var LabelEnd: UILabel!
#IBOutlet var scorelabel: UILabel!
var score = Int()
var CorrectAnswer = String()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
RandomQuestions()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func RandomQuestions(){
var RandomNumber = arc4random() % 1
RandomNumber += 1
switch(RandomNumber){
case 1:
question.text = "Who is the founder of People's Association?"
button1.setTitle("Lee Hsien Loong", for: .normal)
button2.setTitle("Lee Kuan Yew", for: .normal)
button3.setTitle("Goh Chok Tong", for: .normal)
button4.setTitle("Goh Khen Swee", for: .normal)
CorrectAnswer = "1"
Hide()
break
default:
break
}
}
func Hide(){
LabelEnd.isHidden = true
}
func UnHide(){
LabelEnd.isHidden = false
}
#IBAction func Button1(_ sender: Any) {
UnHide()
if (CorrectAnswer == "1"){
score = score + 1
scorelabel.text = "Score:\(score)"
self.performSegue(withIdentifier: "correct", sender: self)
}
else {
LabelEnd.text = "Incorrect Answer! Try again"
}
}
#IBAction func Button2(_ sender: Any) {
UnHide()
if (CorrectAnswer == "2"){
self.performSegue(withIdentifier: "correct", sender: self)
}
else {
LabelEnd.text = "Incorrect Answer! Try again"
}
}
#IBAction func Button3(_ sender: Any) {
UnHide()
if (CorrectAnswer == "3"){
self.performSegue(withIdentifier: "correct", sender: self)
}
else {
LabelEnd.text = "Incorrect Answer! Try again"
}
}
#IBAction func Button4(_ sender: Any) {
UnHide()
if (CorrectAnswer == "4"){
self.performSegue(withIdentifier: "correct", sender: self)
}
else {
LabelEnd.text = "Incorrect Answer! Try again"
}
}
}
Story Board :
Delete Seague and do it like as follow. Assume you want to share NSMutableDictionary.
Just take a variable as type you want to share with viewController.
For Exa. you want to share data with ShareViewController then take a variable like
var Dict_data = NSMutableDictionary()
Now Where you want to navigate, just do like Assume It's your ShareViewController. Remember dont forget to give identifier of ShareViewController into StoryBoard.
let PV = self.storyboard?.instantiateViewController(withIdentifier: "ShareViewController") as! ShareViewController
PV.data = dict_share_date
self.navigationController?.pushViewController(PV, animated: true)
Here dict_share_data is your NSMutableDictionry. You can take any type but remember both side types must same. or you can do type casting.
You can use AppDelegate to share data between multiple Viewcontrollers. You can always save ,update and retrieve data defined in AppDelegate of your App. Here is my previous ans to use it efficiently to use Appdelegate.Do let me know if you need Swift version .
In your AppDelegate define your variable as
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var points : String? // Declare your object
.... other methods
}
//You can access the property to save and retrieve your file. You can save
let app = UIApplication.shared.delegate as! AppDelegate
app.points = "yourString"
Yo can read properties from any viewcontroller as per your requirement as
let app = UIApplication.shared.delegate as! AppDelegate
let points = app.points?
You can also use NSNotificationCenter to share data in multiple viewcontoller. Do let me know if you have queries regrading implementing the same.
If you need to pass data from one viewcontroller to another while navigation you can also use instantiation of viewcontoller using storyboard id as #Jeetendra explains in his ans.
Related
I have stetted up switch in my First View Controller and assigned the following action to it :-
class ThirdViewController: UIViewController {
#IBOutlet weak var Image: UIImageView!
#IBOutlet weak var playerNum1Button: UIButton!
#IBOutlet weak var toggleSwitch: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
self.toggleSwitch.setOn(UserDefaults.standard.bool(forKey: "toggleState"), animated: true)
}
#IBAction func numPlayers1(_ sender: Any) {
performSegue(withIdentifier: "3to8segue", sender: self)
}
IBAction func toggleSwitch(_ sender: UISwitch) {
if (toggleSwitch.isOn == true) {
Image.image = UIImage(named: "Night")
}
else {
Image.image = UIImage(named: "background")
}
UserDefaults.standard.set(sender.isOn, forKey: "toggleState")
}
It is Able to change the image in the First View Controller. But, how can I get that switch to change the image in 2nd View controller as well when it is turned on? Thanks for the help!
Using Notification Center
Example:
In FirstViewController:
#IBAction func onPressButton(_ sender: UIButton) {
UserDefaults.standard.set(sender.isSelected, forKey: "pressedButton")
NotificationCenter.default.post(name: "changeImageBackground", object: nil)
}
In SecondViewController:
NotificationCenter.default.addObserver(target: self, selector: #selector(didChangeImageBackground), name: "changeImageBackground", object: nil)
#objc func didChangeImageBackground() {
let changed = UserDefaults.standard.bool(forKey: "pressedButton")
if changed {
// image when pressed
} else {
// image when haven't pressed
}
}
I have been having trouble synchronizing the values of two UISegments. The First UISegment is suppose to get updated based on the value of the second UISegment (which is in a different ViewController) and then update the value in its own segment based on what is selected in the second ViewController. However, the values are not transferring to the first UISegment, meaning the title and value is not being stored even though I am using the defaults and storing keys.
ViewController
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var tipLabel: UILabel!
#IBOutlet weak var totalLabel: UILabel!
#IBOutlet weak var billField: UITextField!
#IBOutlet weak var tipController: UISegmentedControl!
#IBOutlet weak var splitController: UISegmentedControl!
#IBOutlet weak var splitLabel: UILabel!
let defaults = UserDefaults.standard
override func viewDidLoad() {
super.viewDidLoad()
defaults.synchronize()
billField.becomeFirstResponder()
billField.text = String(defaults.double(forKey: "bill_value"))
print(tipController.selectedSegmentIndex)
if billField.text == "" || (billField.text == nil) {
billField.placeholder = String(0.00)
}
billField.becomeFirstResponder()
tipController.selectedSegmentIndex = defaults.integer(forKey: "default_tip_index")
}
override func viewWillAppear(_ animated: Bool) {
tipController.selectedSegmentIndex = defaults.integer(forKey: "default_tip_index")
}
#IBAction func onTap(_ sender: Any) {
view.endEditing(true)
}
#IBAction func calculateTip(_ sender: Any) {
let tipPercentages = [0.18, 0.20, 0.25]
let splitNumbers = [1,2,3,4]
let bill = Double(billField.text!) ?? 0 //converts bill to double
defaults.set(bill, forKey: "bill_value") //Saves last bill input
let tip = bill * tipPercentages[tipController.selectedSegmentIndex]
let total = bill + tip
tipLabel.text = String(format: "$%.2f", tip)
totalLabel.text = String(format: "$%.2f", total)
splitLabel.text = String(format: "$%.2f", total/Double((splitNumbers[splitController.selectedSegmentIndex])))
}
}
SettingsViewController
class SettingsViewController: UIViewController {
var tipPercentageIndex:Int!
#IBOutlet weak var settingsTipController: UISegmentedControl!
let defaults = UserDefaults.standard
override func viewDidLoad() {
super.viewDidLoad()
let selectedDefaultPercent = defaults.integer(forKey: "default_tip_index")
settingsTipController.selectedSegmentIndex = selectedDefaultPercent
}
#IBAction func tipPercent(_ sender: Any) {
defaults.set(settingsTipController.selectedSegmentIndex, forKey: "default_tip_index")
defaults.synchronize()
}
#IBAction func splitNumber(_ sender: Any) {
}
#IBAction func Back(_ sender: Any) {
self.performSegue(withIdentifier: "Tippy2", sender: self)
}
}
Overall this simple application calculates the tip and updates the first UISegment when the settingsViewController is visited.
#IBOutlet weak var allStocksSelected: UIImageView!
#IBOutlet weak var shortStocksSelected: UIImageView!
#IBOutlet weak var longStocksSelected: UIImageView!
#IBOutlet weak var myStocksTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
myStocksTableView.delegate = self
myStocksTableView.dataSource = self
}
override func viewDidAppear(_ animated: Bool) {
self.shortStocksSelected.isHidden = true
self.longStocksSelected.isHidden = true
self.allStocksSelected.isHidden = true
}
#IBAction func allStocksButtonPressed(_ sender: Any) {
print("ALL!")
self.stockTypeChanged(stockType: allStocksSelected)
}
#IBAction func shortStocksButtonPressed(_ sender: Any) {
print("SHORT!")
self.stockTypeChanged(stockType: shortStocksSelected)
}
#IBAction func longStocksButtonPressed(_ sender: Any) {
print("LONG!")
self.longStocksSelected.isHidden = false
self.shortStocksSelected.isHidden = true
self.allStocksSelected.isHidden = true
//stockTypeChanged(stockType: longStocksSelected)
}
#IBAction func addNewStockButtonPressed(_ sender: Any) {
}
#IBAction func signOutButtonPressed(_ sender: Any) {
}
private func stockTypeChanged(stockType: UIImageView) {
let stockTypes: [UIImageView] = [allStocksSelected, shortStocksSelected, longStocksSelected]
for (stock) in stockTypes {
if (stock == stockType) {
stock.isHidden = false
} else {
stock.isHidden = true
}
}
}
As shown from my code above, I am basically just trying to hide and show certain image views when buttons are pressed.
I am 100% sure that all of the buttons actions and IB outlets are properly connected, yet the image views are still not hiding and showing, and I cannot figure out why.
I was running into this same issue. Placing your isHidden code to execute on the main thread should solve the problem.
DispatchQueue.main.sync {
}
I am learning swift3 programming but after executing my calculator app its crashing in between. Please check the below code.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var aLabel: UILabel!
#IBOutlet weak var commmon_button: UIButton!
var a: Int?
var b: Int?
var sum: Int?
var val = ""
#IBOutlet weak var text_feild: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func clik_button(_ sender: UIButton) {
val=String(sender.tag)
text_feild.text = text_feild.text! + val
}
#IBAction func fn_addition(_ sender: UIButton) {
a = Int(text_feild.text!)
}
#IBAction func fn_answer(_ sender: UIButton) {
b = Int(text_feild.text!)
sum = a! + b!
a = 0
b = 0
text_feild.text = nil
text_feild.text = String(sum!)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
During run time i am getting crash at fn_addition by saying Thread 1 breakpoint 2.1
Initiliaze your variables as below
var a = 0
var b = 0
var sum = 0
Replace your methods with below methods.
#IBAction func clik_button(_ sender: UIButton) {
guard let value = String(sender.tag), let text = text_feild.text
else {
return
}
text_feild.text = text + value
}
#IBAction func fn_addition(_ sender: UIButton) {
guard let aValue = Int(text_feild.text)
else{
return
}
a = aValue
}
#IBAction func fn_answer(_ sender: UIButton) {
guard let bValue = Int(text_feild.text)
else{return}
b = bValue
sum = a + b
a = 0
b = 0
text_feild.text = ""
text_feild.text = String(sum)
}
Suggestion: You are using same text field for taking a , b values and for showing sum result. It is better to use two different text fields for taking a and b values separately. Take a label to show sum value.
Right now i have a purple and red score. I would like to use the 1 and 2 buttons to add the score for the colors. I know how to do this by creating just a sets of buttons to the specific color but doing this again and again will create problems. So what I am trying to do is if purple is selected and if and only if 1 is pressed then the total score of purple will = 1. I guess what I am trying is perform a switch statement for the buttons.
Before purple/red button is pressed.
After purple/red button is pressed.
import UIKit
class ViewController: UIViewController {
var purpleScore = 0
var redScore = 0
#IBOutlet var plus1: UIButton!
#IBOutlet var plus2: UIButton!
#IBOutlet var addRed: UIButton!
#IBOutlet var addPurlple: UIButton!
#IBOutlet var totalScorePurple: UILabel!
#IBOutlet var totalScoreRed: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
plus1.isHidden = true
plus2.isHidden = true
}
#IBAction func enteringRed(_ sender: Any) {
plus1.isHidden = false
plus2.isHidden = false
}
#IBAction func enteringPurple(_ sender: Any) {
plus1.isHidden = false
plus2.isHidden = false
}
#IBAction func plus1(_ sender: Any) {
}
#IBAction func plus2(_ sender: Any) {
}
}
If I understand correctly as the comment above, this should work
Create 2 Bool for red and purple to see weather they are checked or not, this is easiest way, else you can just check the redButton color got changed (if it does) or .selected = true
var redChecked = false
var purpleChecked = false
#IBAction func enteringRed(_ sender: Any) {
redChecked = !redChecked
//change color etc..
}
#IBAction func enteringPurple(_ sender: Any) {
purpleChecked = !purpleChecked
//change color etc..
}
#IBAction func plus1(_ sender: Any) {
if redChecked {
redScore+=1
}
if purpleChecked {
purpleScore+=1
}
}
#IBAction func plus2(_ sender: Any) {
if redChecked {
redScore+=2
}
if purpleChecked {
purpleScore+=2
}
}