0

I made a simple code to clarify my problem. When I run my app on my iPhone it works properly, but when I press home button - either on simulator or my real iPhone - and press the app icon again, the app runs, but the app's button "TestButtonAction don't work. Same problem when I press power/sleep button and turn it on again. same problem either when I duple click home button and terminate my app by sliding it's window away, and rerun it again the button don't work or print "Right Image" in the Console Here is my code, Thanks.

//
//  ViewController.swift
//  TestingHomeButton
//
//  Created by Samuel Oncy on 4/25/18.
//  Copyright © 2018 Samuel Oncy. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var TestButtonOutlet: UIButton!

    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 TestButtonAction(_ sender: UIButton) {
        if(TestButtonOutlet.imageView?.image == UIImage(named:"NatureView")){
                    print("Right Image")
        }
    }
}
Samuel
  • 65
  • 8

2 Answers2

2

Simply Make UIImage global Variable, If you want to compare in If.

Other ways can also be to solve same problem - Tag is right one.

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var TestButtonOutlet: UIButton!
    var image = UIImage (named: "NatureView")

    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 TestButtonAction(_ sender: UIButton) {

       // print(TestButtonOutlet.imageView?.image == UIImage(named:"NatureView"))
        if (TestButtonOutlet.currentImage == image) {
            print(TestButtonOutlet.currentImage)
        }
    }
}
Chatar Veer Suthar
  • 15,541
  • 26
  • 90
  • 154
  • Great Job.. Well Done bro. :) – Samuel Apr 25 '18 at 13:08
  • sorry there is related issue.. when I duple click home button and terminate the app window by sliding it away, and rerun the app again the same issue appears!!! – Samuel Apr 25 '18 at 13:17
  • I will look into it, in couple of hours, than back to you. – Chatar Veer Suthar Apr 25 '18 at 13:45
  • Thanks os much bro., please let me know if I should create a new thread for the issue of "rerun terminated app"? – Samuel Apr 25 '18 at 13:46
  • I guide you to tell me, what is your requirement, so I can tell your better way to identify, than matching images. – Chatar Veer Suthar Apr 25 '18 at 13:54
  • i made 2 image buttons with default images. and a button read image's names and do a lot of thing such as.. if the image1 is Red and the other one is yellow do something. and by pressing these two images their their own images changes so if I press the main button, it compares these images and do different functions. either if I press anyone of the 2 imageButtons and their colors changes they're also do something after touch inside event happens.. I know it's so complicated but to be simple for the user, thanks :) – Samuel Apr 25 '18 at 14:08
  • I'll consider this question answers and create another one with the other issue .. thanks so much for your help you solved 90% of my problem – Samuel Apr 25 '18 at 14:13
  • When you killing the APP, its removed from XCode Debugger, so Its no more able to show you LOGs, So, you can do is put a LABEL and change its text, instead of showing LOGs. – Chatar Veer Suthar Apr 25 '18 at 14:25
0

To check the current image of button, use the currentImage property as follows:-

@IBAction func TestButtonAction(_ sender: UIButton) {
    if sender.currentImage.isEqual(UIImage(named: "NatureView")) {
        print("Right Image")
    }
}
pkc456
  • 8,350
  • 38
  • 53
  • 109
  • Thanks for your quick reply, I tried this code now it's running but when i press the home button and rerun my app again the button don't work and don't print anything – Samuel Apr 25 '18 at 11:42
  • On returning to the app, print the current image of your button and check what it is printing in console. – pkc456 Apr 25 '18 at 11:44
  • print nothing but when i remove the condition and run my app. it print true , and when i press the home button and rerun my app again it prints false – Samuel Apr 25 '18 at 11:49
  • Optional(, {120, 160}) – Samuel Apr 25 '18 at 11:51
  • current image prints the previous line but after using home button, don't print anything – Samuel Apr 25 '18 at 11:52
  • Hit & Trial:- There may be some issue with your `IBOutlet`. So try to update the `TestButtonOutlet` with `sender, as I did in my [updated answer](https://stackoverflow.com/a/50021286/988169) – pkc456 Apr 25 '18 at 11:52
  • it fixes your uploaded code to this one: if (sender as AnyObject).currentImage.isEqual(UIImage(named: "NatureView")) { print("Right Image") } – Samuel Apr 25 '18 at 12:14
  • when i press the UIbutton for the first time it gives me a long error this is the end of error code: libc++abi.dylib: terminating with uncaught exception of type NSException (lldb) – Samuel Apr 25 '18 at 12:15
  • this is the project link if you'd like to check it, Thanks so much – Samuel Apr 25 '18 at 12:47