swift images array code back button error - ios

I want to slide from the pictures. My code is:
#IBOutlet weak var image: UIImageView!
var resimler=["01.jpg","02.jpg","03.jpg","04.jpg","05.jpg"]
var index=0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func gerileBttn(sender: UIButton) {
index--
if index==resimler.count {index=0}
image.image=UIImage(named: resimler[index])
}
#IBAction func ilerle(sender: UIButton) {
index++
if index==resimler.count {index=0}
image.image=UIImage(named: resimler[index])
}
When I press forward moving pictures. However, when it comes to the first picture when I press the back key "array" error whether the program crashes..
How can I solve this problem.
Thanks for the help..

Looks like your index may be going negative. Perhaps this (assuming you want it to wrap)?
#IBAction func gerileBttn(sender: UIButton) {
index--
if index<0 {index=resimler.count-1}
image.image=UIImage(named: resimler[index])
}

Related

My Question is about changing a UIImage when a button is pressed

So I am quite new to coding, learning Swift 3 on Udemy. I'm trying to test my skills by building a music app that contains 3 sound files, at the moment I am struggling to get the image of the current song that should be playing once my UIButton is pressed. I have created an array containing the image files but for some reason it only shows 2 out of the 3 images and will not go further nor will it let me loop the images, any and all suggestions are welcome.
I have tried a for-in loop which is not what I want at the moment. I am trying to get the function to update songImage to accept my array and link to the sender.tag property to cycle through the images
class ViewController: UIViewController {
// Instance Variables
var playTheSong : AVAudioPlayer!
var imageArray = ["songImage1", "songImage2", "songImage3"]
var allSongNamesAndDescriptions = MusicClassBank()
var nextImage = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
albumArtwork.image = UIImage(named: "songImage3")
}
#IBAction func buttonPressed(_ sender: UIButton) {
updateSongImage(selectedImageFile: imageArray[sender.tag - 1])
nextImage = nextImage + 1
}
#IBOutlet weak var albumArtwork: UIImageView!
#IBOutlet weak var nameOfSong: UILabel!
#IBOutlet weak var songDescription: UILabel!
// Cycle through images upon button being pressed.
func updateSongImage(selectedImageFile : String) {
if nextImage <= 3 {
albumArtwork.image = UIImage(named: selectedImageFile)
}
else {
nextImage = 0
}
}
Right now the code is showing just the image displayed upon view load and the next image in the array. I cannot get it to go through the entire array and keep going when the button is pressed.
You are using the "next" UIButton sender.tag as a parameter to change the image, but that tag never changes. So:
In your viewDidLoad() method, you show the third image
When you press the "next" button, you update the image to 1 (probably the sender.tag is 1)
Pressing the button again don't update the image
You can try something like this:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//Set the first image
updateSongImage(selectedImageFile : imageArray[0])
}
#IBAction func buttonPressed(_ sender: UIButton) {
//If nextImage is less than imageArray.count - 1 (because arrays start with 0), add one to nextImage. Else, nextImage return to zero
nextImage = nextImage < imageArray.count - 1 ? nextImage + 1 : 0
updateSongImage(selectedImageFile: imageArray[nextImage])
}
func updateSongImage(selectedImageFile : String) {
//Here we only need to update the image, because the if is outside
albumArtwork.image = UIImage(named: selectedImageFile)
}

iOS app crashes on Segue

I'm trying to build an app for school but I keep getting error messages. It's probably a really obvious mistake I made.
Basically I am trying to build a view that displays a UIWebView and changes to a 2nd view if a segment controller switch is pressed.
My Code is:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var myWebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "http://google.de")
myWebView.loadRequest(URLRequest(url: url!))
}
#IBAction func Heute(_ sender: UISegmentedControl) {
performSegue(withIdentifier: "Switch", sender: self)
}
}
//Vertretung2
class Vertretung2: UIViewController {
#IBOutlet weak var UIWebView1: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "http://google.de")
UIWebView1.loadRequest(URLRequest(url: url!))
}
#IBAction func Morgen(_ sender: UISegmentedControl) {
performSegue(withIdentifier: "Switch", sender: self)
}
}
My app keeps crashing when I switch from 1st view to 2nd view.
Is that UIWebView1 in Vertretung2 nil when viewDidLoad() is called on Vertretung2?
Ok, Sorry. I just read your crash log above, and it looks like you maybe forgot to set the Custom Class name in the storyboard for the viewController? Check that here.
The Class text entry field should have your custom class in it.

Change the text of a label with a button in iOS

Firstly, let me say I'm new to Swift and Xcode. I have a label that displays some text and I want that text to change when a button is pressed.
Here is the code in the UIViewController:
var txt = "Hey"
#IBOutlet weak var Text: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
showText()
}
func showText(){
Text.text = txt
}
#IBAction func Button(_ sender: Any) {
txt = "Hello"
}
The buttons and labels are correctly linked to their main.storyboard counterparts. When I run this and press the button the text doesn't change.
viewDidLoad() is only called one time when the ViewController is created. showText() needs to be called after txt is set if you want to display it. One way to automate this is to add a property observer to txt to call showText() when txt is updated:
var txt = "Hey" {
didSet {
showText()
}
}
I will add that it is more flexible and perhaps elegant to have your showText() method take the actual text to show as an argument, instead of depending on some internal state (the value of the property txt: String, that isn't immediately evident at the call site) to accomplish its task:
#IBOutlet weak var textLabel: UILabel!
func showText(_ text: String){
self.textLabelabel?.text = text
// (optional chaining to avoid crash if accidentally
// called before viewDidLoad())
}
#IBAction func buttonAction(_ sender: Any) {
self.showText("Hello") // From here it is clear what text will be shown
}
change your code as per below
var txt = "Hey"
#IBOutlet weak var Text: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
showText()
}
func showText(){
Text.text = txt
}
#IBAction func Button(_ sender: UIButton) {
txt = "Hello"
showText()
}
Change Text of label with button just need to pass the text in label;
//Outlets
#IBOutlet weak var firstLabel: UILabel!
#IBOutlet weak var secondLabel: UILabel!
//Button Action
#IBAction func textChabge(_ sender: Any) {
firstLabel.text = "newText"
secondLabel.text = "newText"
}
just change your IBAction like this.
#IBAction func Button(_ sender: Any) {
txt = "Hello"
showText()
}

How to use textfield to enter numbers into a array (swift3)

I have a textfield that I would like the user to be able to enter numbers for a array in. Ideally the button would save the number, then clear the textfield for another number to be enter. This would be a process that would just go in a loop. So its just save the number, clear the number, repeat infinitely.
import UIKit
class ViewController: UIViewController {
#IBOutlet var enterText: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func submitText(_ sender: Any) {
}}
Lets define and array.
var arrayOfInt = [Int]()
Suppose you have button action method and textField Object (could be IB outlet) to which user enters number.
#IBAction func submitText(_ sender: Any) {
if let text = textField.text {
if let number = Int(text){
arrayOfInt.append(number)
}else {
print("Please enter number")
}
}
}

button.isHidden to disappear again after being button view(background) is tapped on again

Still new to swift, but was having some fun with storyboard and with hiding and showing buttons. I created a storyboard with an image view that fills the whole view, and a button that lies on top of it called the overView. I also created a button called the searchButton that is on top of the overView. I so far am pretty stoked I got something this easy to work but need some more help. When the user clicks on the screen,(overView button), the searchButton appears. Great! But I want it (searchButton) to disappear once tapped on again. If someone could help me, that would be great. Thanks, here's my code
#IBOutlet weak var mainImage: UIImageView!
#IBOutlet weak var searchButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
searchButton.isHidden = true
// Do any additional setup after loading the view, typically from a nib.
}
#IBOutlet weak var overView: UIButton!
#IBAction func overView(_ sender: Any) {
searchButton.isHidden = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
Thanks
Try this:
#IBAction func overView(_ sender: Any) {
searchButton.isHidden = !searchButton.isHidden
}
And if your searchButton is cover overView then try this:
#IBAction func onTapSearchButton(_ sender: Any) {
searchButton.isHidden = true
}

Resources