I have a homework with which I'm stuck. Here it is:
Make a gallery project: a large UIImageView and two buttons below it: Back and Next. Add 10 pictures to the project, and by clicking on the buttons, the previous or next picture should be displayed, respectively.
So I made an array, and some *silly attempt* to go through this array. But i'm 100% sure that there is some simple answer to that.
Thank you!
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var imagesIV: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
let images = [UIImage(named: "1"), UIImage(named: "2"), UIImage(named: "3"),UIImage(named: "4"),UIImage(named: "5"),UIImage(named: "6"),UIImage(named: "7"),UIImage(named: "8"),UIImage(named: "9"),UIImage(named: "10")]
var numberOfImages = images.count
var currentImage = 0
#IBAction func forwardButton(_ sender: Any) {
guard case currentImage + 1 < images.count else {return}
currentImage += 1
}
#IBAction func backButton(_ sender: Any) {
}
}
A better way in my opinion is below, specifically to make your code scalable (e.g. if you have 1000 photos, you don't want to add them manually one by one in the array):
class ViewController: UIViewController {
#IBOutlet weak var imagesIV: UIImageView!
var currentImageIndex = 0
var images : [UIImage] = []
override func viewDidLoad() {
super.viewDidLoad()
for a in 1...10 {
if let newImage = UIImage(named: "\(a)") {
images.append(newImage)
}
}
updateImageInImageView()
}
#IBAction func forwardButton(_ sender: Any) {
if currentImageIndex + 1 != images.count { currentImageIndex += 1} else { currentImageIndex = 0}
updateImageInImageView()
}
#IBAction func backButton(_ sender: Any) {
if currentImageIndex != 0 { currentImageIndex -= 1} else { currentImageIndex = images.count - 1}
updateImageInImageView()
}
func updateImageInImageView() {
imagesIV.image = images[currentImageIndex]
}
}
You can try
#IBAction func forwardButton(_ sender: Any) {
guard currentImage + 1 < images.count else {return}
currentImage += 1
imagesIV.image = images[currentImage]
}
#IBAction func backButton(_ sender: Any) {
guard currentImage - 1 >= 0 else {return}
currentImage -= 1
imagesIV.image = images[currentImage]
}
Related
How do I make a back button? I get it wrong the way I want to do it. Thank you in advance.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView!
let images: [UIImage] = [#imageLiteral(resourceName: "tub"),#imageLiteral(resourceName: "ball"),#imageLiteral(resourceName: "apple"),#imageLiteral(resourceName: "igloo"),#imageLiteral(resourceName: "frog")]
var i : Int = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func nextButton(_ sender: UIButton) {
i = (i+1)%images.count
imageView.image = images[i]
}
#IBAction func backButton(_ sender: UIButton) {
i = (i-1)%images.count
imageView.image = images[i]
}
the back button gives an error
You have 5 images in your array.
When you tap your Back button, suppose i is currently equal to 0:
(i-1) == -1
-1 % 5 == -1
imageView.image = images[-1] // is invalid... there is no array index of -1
If you want the Back button to "wrap around" from 0 (the first image) to 4 (the last image), you should do:
i -= 1
if i < 0 {
i = images.count - 1
}
imageView.image = images[i]
If you want to stop at the first image:
i = max(i - 1, 0)
imageView.image = images[i]
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.
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.
I'm learning swift by building a basic counting app (based on this tutorial https://www.youtube.com/watch?v=3blma4PCRak) and I'm trying to have a function that counts on increments of 3 based on the state of a switch.
It seems a lot of the answers I find in tutorials online and from SO are from many years ago and a lot of the syntax is deprecated, including:
• instead of oddSwitch.On now it is oddSwitch.isOn
• Stating colors changed from UIColor.redColor to just UIColor.red
And so on...below is my code thus far:
//
// ViewController.swift
// TestApp
//
// Created by kawnah on 2/8/17.
// Copyright © 2017 kawnah. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
#IBOutlet var outputLabel: UILabel? = UILabel();
var currentCount = 0;
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 addOneBUtton(_ sender: UIButton) {
currentCount = currentCount + 1
if currentCount <= 1 {
outputLabel?.text = "\(currentCount) click"
} else {
outputLabel?.text = "\(currentCount) clicks"
}
outputLabel?.textColor = UIColor.red
}
#IBOutlet var oddSwitch: UISwitch!
#IBAction func oddToggle(_ sender: UISwitch) {
if oddSwitch!.isOn {
currentCount = currentCount + 3
}
}
}
I'm confused as to the relationship between the #IBOutlet and my function that states to count in increments of three. I've tried including weak storage as well but it's to my understanding that is the default for #IBOutlet. Currently when I toggle the switch on, the counter still increments by 1.
My error log simply shows compression errors for PNGs for my splash screen, nothing with the code. What exactly is happening?
Change your addOneBUtton code to match this.
#IBAction func addOneBUtton(_ sender: UIButton) {
if oddSwitch!.isOn {
currentCount = currentCount + 3
} else {
currentCount = currentCount + 1
}
if currentCount <= 1 {
outputLabel?.text = "\(currentCount) click"
} else {
outputLabel?.text = "\(currentCount) clicks"
}
outputLabel?.textColor = UIColor.red
}
And you can remove this section.
#IBAction func oddToggle(_ sender: UISwitch) {
if oddSwitch!.isOn {
currentCount = currentCount + 3
}
}
So the complete code should look something like this.
import UIKit
class testViewController: UIViewController {
//MARK: IBOutlets
#IBOutlet var outputLabel: UILabel? = UILabel();
#IBOutlet var oddSwitch: UISwitch!
//MARK: Vars
var currentCount = 0;
//MARK: Lifecyle
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
//MARK: IBActions
#IBAction func addOneBUtton(_ sender: UIButton) {
if oddSwitch!.isOn {
currentCount = currentCount + 3
} else {
currentCount = currentCount + 1
}
if currentCount <= 1 {
outputLabel?.text = "\(currentCount) click"
} else {
outputLabel?.text = "\(currentCount) clicks"
}
outputLabel?.textColor = UIColor.red
}
}
Note: This was my twist on keeping the code clean with IBOutlets all in one spots and using //MARK: to the sections of code quicker to jump to.
I am updating my app for swift. I am trying to display a percentage once the "calculate" button is pushed.
So far I have:
#IBAction func calculateButton(sender: UIButton) {
var calculated = (correctNumber / correctNumber + incorrectNumber * 100 )
calculateLabel.text = "\(calculated)"
}
Which I know is incorrect. How do I convert this to a percentage?
Full code below:
import UIKit
class ViewController: UIViewController {
var correctNumber = 0
var incorrectNumber = 0
var verbalNumber = 0
var visualNumber = 0
var tactileNumber = 0
//rightnumber____________________________________
#IBOutlet weak var correctLabel: UILabel!
#IBAction func correctUpButton(sender: UIButton) {
correctNumber += 1
correctLabel.text = "\(correctNumber)"
}
#IBAction func correctDownButton(sender: UISwipeGestureRecognizer) {
correctNumber -= 1
correctLabel.text = "\(correctNumber)"
}
//------------------------------------------------
//incorrectNumber________________________________
#IBOutlet weak var incorrectLabel: UILabel!
#IBAction func incorrectUpButton(sender: UIButton) {
incorrectNumber += 1
incorrectLabel.text = "\(incorrectNumber)"
}
#IBAction func incorrectDownButton(sender: UISwipeGestureRecognizer) {
incorrectNumber -= 1
incorrectLabel.text = "\(incorrectNumber)"
}
//end-------------------------------------------------
//verbalNumber________________________________
#IBOutlet weak var verbalLabel: UILabel!
#IBAction func verbalUpButton(sender: UIButton) {
verbalNumber += 1
verbalLabel.text = "\(verbalNumber)"
}
#IBAction func verbalDownButton(sender: UISwipeGestureRecognizer) {
verbalNumber -= 1
verbalLabel.text = "\(verbalNumber)"
}
//end-------------------------------------------------
//visualNumber________________________________
#IBOutlet weak var visualLabel: UILabel!
#IBAction func visualUpButton(sender: UIButton) {
visualNumber += 1
visualLabel.text = "\(visualNumber)"
}
#IBAction func visualDownButton(sender: UISwipeGestureRecognizer) {
visualNumber -= 1
visualLabel.text = "\(visualNumber)"
}
//end-------------------------------------------------
//tactileNumber________________________________________
#IBOutlet weak var tactileLabel: UILabel!
#IBAction func tactileUpButton(sender: UIButton) {
tactileNumber += 1
tactileLabel.text = "\(tactileNumber)"
}
#IBAction func tactileDownButton(sender: UISwipeGestureRecognizer) {
tactileNumber -= 1
tactileLabel.text = "\(tactileNumber)"
}
//end--------------------------------------------------
//calculate______________________________________________
#IBOutlet weak var calculateLabel: UILabel!
#IBAction func calculateButton(sender: UIButton) {
var calculated = (correctNumber / correctNumber + incorrectNumber * 100 )
calculateLabel.text = "\(calculated)"
}
//end----------------------------------------------------
//reset
#IBAction func resetButton(sender: UIButton) {
correctNumber = 0
correctLabel.text = ""
incorrectNumber = 0
incorrectLabel.text = ""
verbalNumber = 0
verbalLabel.text = ""
visualNumber = 0
visualLabel.text = ""
tactileNumber = 0
tactileLabel.text = ""
calculateLabel.text = ""
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
If i understand it correctly you need change line like this one:
var calculated = correctNumber / (correctNumber + incorrectNumber) * 100