I'm trying to grasp what is happening and I'm really confused by this. Basically, I have a UITableViewCell
, and inside that cell I have a UIBUtton
. When the table loads up and calls cellForRowAt
, I set the UIButton
image and add a target to it.
Before the app enters the background, I tap the UIButton
which calls my function. In that function, I check that the image of the UIButton
is equal to the image I set, which it is.
However, once the app enters the background and then re-enters the foreground, when I tap the UIButton
and check whether the button's image is the same image that is set (and also visible) to me, it is not?
I dont understand why the UIButton
image is not retained when the app enters the foreground after being in the background.
Here is some sample code:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! UITableViewCell
let button = cell.viewWithTag(1) as! UIButton
button(self, action: #selector(ViewController.checkImage), for: UIControlEvents.touchUpInside)
button(#imageLiteral(resourceName: "button_stained"), for: UIControlState.normal)
}
fun checkImage(_ sender: UIButton)
{
if sender.currentImage == #imageLiteral(resourceName: "button_stained")
{
print("TEST")
}
}
TEST
is only printed out initially when my view controller loads. If my app enters the background, then back to the foreground, TEST
does not get printed out.
I have tried to also use sender.currentImage
as well as sender.image(for: .normal)
to no avail.
Can someone explain to me why this is happening?
Here is a sample project:
Thanks.