Why UIButton requires two clicks to change its image












0















I am new to iOS app development using Swift 4. I used the code below to change the image of button2 by running it in the iOS simulator:



 @IBAction func button2(_ sender: Any) {
button2.setImage(UIImage(named: "wrong_answer"), for: .normal)
}


However, button2 was highlighted when I first click on it without changing its image. Then after the second click, the image has been changed in button2.



My question is why the image was not changed in button2 after the first click?



What can I do to change the image after the first click instead of twice? Is this a bug in the iOS simulator of Xcode or it is normal?










share|improve this question

























  • i think it's simulator bug.your code is correct(please change sender from Any to UIButton)

    – andesta.erfan
    Nov 17 '18 at 15:04













  • That may because you have set image to normal state and the button must be in highlighted state. But do check that code on device.

    – rptwsthi
    Nov 17 '18 at 15:10











  • @J.Doe i said i think. if you know the correct answer please post it because it's as you say basic

    – andesta.erfan
    Nov 17 '18 at 16:05













  • Notice that you call the button "sender" in the method. You need to update sender, not button2. This would be clearer if you updated sender to UIButton (which is what it is) instead of using Any. See my answer below.

    – Mozahler
    Nov 17 '18 at 16:13











  • @J.Doe so please ask and try to answer and do not interrupt the community

    – andesta.erfan
    Nov 17 '18 at 16:36
















0















I am new to iOS app development using Swift 4. I used the code below to change the image of button2 by running it in the iOS simulator:



 @IBAction func button2(_ sender: Any) {
button2.setImage(UIImage(named: "wrong_answer"), for: .normal)
}


However, button2 was highlighted when I first click on it without changing its image. Then after the second click, the image has been changed in button2.



My question is why the image was not changed in button2 after the first click?



What can I do to change the image after the first click instead of twice? Is this a bug in the iOS simulator of Xcode or it is normal?










share|improve this question

























  • i think it's simulator bug.your code is correct(please change sender from Any to UIButton)

    – andesta.erfan
    Nov 17 '18 at 15:04













  • That may because you have set image to normal state and the button must be in highlighted state. But do check that code on device.

    – rptwsthi
    Nov 17 '18 at 15:10











  • @J.Doe i said i think. if you know the correct answer please post it because it's as you say basic

    – andesta.erfan
    Nov 17 '18 at 16:05













  • Notice that you call the button "sender" in the method. You need to update sender, not button2. This would be clearer if you updated sender to UIButton (which is what it is) instead of using Any. See my answer below.

    – Mozahler
    Nov 17 '18 at 16:13











  • @J.Doe so please ask and try to answer and do not interrupt the community

    – andesta.erfan
    Nov 17 '18 at 16:36














0












0








0








I am new to iOS app development using Swift 4. I used the code below to change the image of button2 by running it in the iOS simulator:



 @IBAction func button2(_ sender: Any) {
button2.setImage(UIImage(named: "wrong_answer"), for: .normal)
}


However, button2 was highlighted when I first click on it without changing its image. Then after the second click, the image has been changed in button2.



My question is why the image was not changed in button2 after the first click?



What can I do to change the image after the first click instead of twice? Is this a bug in the iOS simulator of Xcode or it is normal?










share|improve this question
















I am new to iOS app development using Swift 4. I used the code below to change the image of button2 by running it in the iOS simulator:



 @IBAction func button2(_ sender: Any) {
button2.setImage(UIImage(named: "wrong_answer"), for: .normal)
}


However, button2 was highlighted when I first click on it without changing its image. Then after the second click, the image has been changed in button2.



My question is why the image was not changed in button2 after the first click?



What can I do to change the image after the first click instead of twice? Is this a bug in the iOS simulator of Xcode or it is normal?







ios swift uibutton






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 16:25









rmaddy

240k27313377




240k27313377










asked Nov 17 '18 at 14:29









Hosy HosyHosy Hosy

44




44













  • i think it's simulator bug.your code is correct(please change sender from Any to UIButton)

    – andesta.erfan
    Nov 17 '18 at 15:04













  • That may because you have set image to normal state and the button must be in highlighted state. But do check that code on device.

    – rptwsthi
    Nov 17 '18 at 15:10











  • @J.Doe i said i think. if you know the correct answer please post it because it's as you say basic

    – andesta.erfan
    Nov 17 '18 at 16:05













  • Notice that you call the button "sender" in the method. You need to update sender, not button2. This would be clearer if you updated sender to UIButton (which is what it is) instead of using Any. See my answer below.

    – Mozahler
    Nov 17 '18 at 16:13











  • @J.Doe so please ask and try to answer and do not interrupt the community

    – andesta.erfan
    Nov 17 '18 at 16:36



















  • i think it's simulator bug.your code is correct(please change sender from Any to UIButton)

    – andesta.erfan
    Nov 17 '18 at 15:04













  • That may because you have set image to normal state and the button must be in highlighted state. But do check that code on device.

    – rptwsthi
    Nov 17 '18 at 15:10











  • @J.Doe i said i think. if you know the correct answer please post it because it's as you say basic

    – andesta.erfan
    Nov 17 '18 at 16:05













  • Notice that you call the button "sender" in the method. You need to update sender, not button2. This would be clearer if you updated sender to UIButton (which is what it is) instead of using Any. See my answer below.

    – Mozahler
    Nov 17 '18 at 16:13











  • @J.Doe so please ask and try to answer and do not interrupt the community

    – andesta.erfan
    Nov 17 '18 at 16:36

















i think it's simulator bug.your code is correct(please change sender from Any to UIButton)

– andesta.erfan
Nov 17 '18 at 15:04







i think it's simulator bug.your code is correct(please change sender from Any to UIButton)

– andesta.erfan
Nov 17 '18 at 15:04















That may because you have set image to normal state and the button must be in highlighted state. But do check that code on device.

– rptwsthi
Nov 17 '18 at 15:10





That may because you have set image to normal state and the button must be in highlighted state. But do check that code on device.

– rptwsthi
Nov 17 '18 at 15:10













@J.Doe i said i think. if you know the correct answer please post it because it's as you say basic

– andesta.erfan
Nov 17 '18 at 16:05







@J.Doe i said i think. if you know the correct answer please post it because it's as you say basic

– andesta.erfan
Nov 17 '18 at 16:05















Notice that you call the button "sender" in the method. You need to update sender, not button2. This would be clearer if you updated sender to UIButton (which is what it is) instead of using Any. See my answer below.

– Mozahler
Nov 17 '18 at 16:13





Notice that you call the button "sender" in the method. You need to update sender, not button2. This would be clearer if you updated sender to UIButton (which is what it is) instead of using Any. See my answer below.

– Mozahler
Nov 17 '18 at 16:13













@J.Doe so please ask and try to answer and do not interrupt the community

– andesta.erfan
Nov 17 '18 at 16:36





@J.Doe so please ask and try to answer and do not interrupt the community

– andesta.erfan
Nov 17 '18 at 16:36












3 Answers
3






active

oldest

votes


















1














You probably have an issue related to UIButton states that is causing this problem.
I don't think it is a simulator bug.
By the way, a good practice you should follow is to name the outlet different than the @IBAction. Let's say:



@IBAction func buttonTapped(_ sender: Any) {
button.setImage(UIImage(named: "image"), for: .normal)
}


Try this:



override func viewDidLoad() {
super.viewDidLoad()
button.setImage(UIImage(named: "image"), for: .selected)
}

@IBAction func buttonTapped(_ sender: Any) {
button.isSelected = !button.isSelected
}


And then the image will be updated automatically when you tap on the button. You can change it to button.isSelected = true if you want to keep the image after the first tap.






share|improve this answer


























  • @HosyHosy it worked for you or you still need help?

    – Matias Jurfest
    Nov 18 '18 at 10:13



















0














Rename your method/action so it differs from the button/property.



Change Any to UIButton since you know its class.



@IBAction func buttonTapped(_ buttonTapped: UIButton) {
buttonTapped. button.isSelected = !button.isSelected
}


Make sure that you are receiving the button callbacks by declaring your view controller a UIButtonDelegate and set the button's delegate property to self.






share|improve this answer


























  • Thank Mozahler but it didn't work

    – Hosy Hosy
    Nov 17 '18 at 20:45











  • Did you declare your view controller a UIButtonDelegate and set the button's delegate property to self?

    – Mozahler
    Nov 17 '18 at 22:56













  • Excellent. Glad you figured it out!

    – Mozahler
    Nov 18 '18 at 13:35



















0














it's simulator bug. it worked on a real device
@IBAction func button2(_ sender: UIButton) {
button2.setImage(UIImage(named: "wrong_answer"), for: .normal)
}






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53352159%2fwhy-uibutton-requires-two-clicks-to-change-its-image%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You probably have an issue related to UIButton states that is causing this problem.
    I don't think it is a simulator bug.
    By the way, a good practice you should follow is to name the outlet different than the @IBAction. Let's say:



    @IBAction func buttonTapped(_ sender: Any) {
    button.setImage(UIImage(named: "image"), for: .normal)
    }


    Try this:



    override func viewDidLoad() {
    super.viewDidLoad()
    button.setImage(UIImage(named: "image"), for: .selected)
    }

    @IBAction func buttonTapped(_ sender: Any) {
    button.isSelected = !button.isSelected
    }


    And then the image will be updated automatically when you tap on the button. You can change it to button.isSelected = true if you want to keep the image after the first tap.






    share|improve this answer


























    • @HosyHosy it worked for you or you still need help?

      – Matias Jurfest
      Nov 18 '18 at 10:13
















    1














    You probably have an issue related to UIButton states that is causing this problem.
    I don't think it is a simulator bug.
    By the way, a good practice you should follow is to name the outlet different than the @IBAction. Let's say:



    @IBAction func buttonTapped(_ sender: Any) {
    button.setImage(UIImage(named: "image"), for: .normal)
    }


    Try this:



    override func viewDidLoad() {
    super.viewDidLoad()
    button.setImage(UIImage(named: "image"), for: .selected)
    }

    @IBAction func buttonTapped(_ sender: Any) {
    button.isSelected = !button.isSelected
    }


    And then the image will be updated automatically when you tap on the button. You can change it to button.isSelected = true if you want to keep the image after the first tap.






    share|improve this answer


























    • @HosyHosy it worked for you or you still need help?

      – Matias Jurfest
      Nov 18 '18 at 10:13














    1












    1








    1







    You probably have an issue related to UIButton states that is causing this problem.
    I don't think it is a simulator bug.
    By the way, a good practice you should follow is to name the outlet different than the @IBAction. Let's say:



    @IBAction func buttonTapped(_ sender: Any) {
    button.setImage(UIImage(named: "image"), for: .normal)
    }


    Try this:



    override func viewDidLoad() {
    super.viewDidLoad()
    button.setImage(UIImage(named: "image"), for: .selected)
    }

    @IBAction func buttonTapped(_ sender: Any) {
    button.isSelected = !button.isSelected
    }


    And then the image will be updated automatically when you tap on the button. You can change it to button.isSelected = true if you want to keep the image after the first tap.






    share|improve this answer















    You probably have an issue related to UIButton states that is causing this problem.
    I don't think it is a simulator bug.
    By the way, a good practice you should follow is to name the outlet different than the @IBAction. Let's say:



    @IBAction func buttonTapped(_ sender: Any) {
    button.setImage(UIImage(named: "image"), for: .normal)
    }


    Try this:



    override func viewDidLoad() {
    super.viewDidLoad()
    button.setImage(UIImage(named: "image"), for: .selected)
    }

    @IBAction func buttonTapped(_ sender: Any) {
    button.isSelected = !button.isSelected
    }


    And then the image will be updated automatically when you tap on the button. You can change it to button.isSelected = true if you want to keep the image after the first tap.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 17 '18 at 15:54

























    answered Nov 17 '18 at 15:20









    Matias JurfestMatias Jurfest

    386412




    386412













    • @HosyHosy it worked for you or you still need help?

      – Matias Jurfest
      Nov 18 '18 at 10:13



















    • @HosyHosy it worked for you or you still need help?

      – Matias Jurfest
      Nov 18 '18 at 10:13

















    @HosyHosy it worked for you or you still need help?

    – Matias Jurfest
    Nov 18 '18 at 10:13





    @HosyHosy it worked for you or you still need help?

    – Matias Jurfest
    Nov 18 '18 at 10:13













    0














    Rename your method/action so it differs from the button/property.



    Change Any to UIButton since you know its class.



    @IBAction func buttonTapped(_ buttonTapped: UIButton) {
    buttonTapped. button.isSelected = !button.isSelected
    }


    Make sure that you are receiving the button callbacks by declaring your view controller a UIButtonDelegate and set the button's delegate property to self.






    share|improve this answer


























    • Thank Mozahler but it didn't work

      – Hosy Hosy
      Nov 17 '18 at 20:45











    • Did you declare your view controller a UIButtonDelegate and set the button's delegate property to self?

      – Mozahler
      Nov 17 '18 at 22:56













    • Excellent. Glad you figured it out!

      – Mozahler
      Nov 18 '18 at 13:35
















    0














    Rename your method/action so it differs from the button/property.



    Change Any to UIButton since you know its class.



    @IBAction func buttonTapped(_ buttonTapped: UIButton) {
    buttonTapped. button.isSelected = !button.isSelected
    }


    Make sure that you are receiving the button callbacks by declaring your view controller a UIButtonDelegate and set the button's delegate property to self.






    share|improve this answer


























    • Thank Mozahler but it didn't work

      – Hosy Hosy
      Nov 17 '18 at 20:45











    • Did you declare your view controller a UIButtonDelegate and set the button's delegate property to self?

      – Mozahler
      Nov 17 '18 at 22:56













    • Excellent. Glad you figured it out!

      – Mozahler
      Nov 18 '18 at 13:35














    0












    0








    0







    Rename your method/action so it differs from the button/property.



    Change Any to UIButton since you know its class.



    @IBAction func buttonTapped(_ buttonTapped: UIButton) {
    buttonTapped. button.isSelected = !button.isSelected
    }


    Make sure that you are receiving the button callbacks by declaring your view controller a UIButtonDelegate and set the button's delegate property to self.






    share|improve this answer















    Rename your method/action so it differs from the button/property.



    Change Any to UIButton since you know its class.



    @IBAction func buttonTapped(_ buttonTapped: UIButton) {
    buttonTapped. button.isSelected = !button.isSelected
    }


    Make sure that you are receiving the button callbacks by declaring your view controller a UIButtonDelegate and set the button's delegate property to self.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 17 '18 at 23:02

























    answered Nov 17 '18 at 16:08









    MozahlerMozahler

    2,09151625




    2,09151625













    • Thank Mozahler but it didn't work

      – Hosy Hosy
      Nov 17 '18 at 20:45











    • Did you declare your view controller a UIButtonDelegate and set the button's delegate property to self?

      – Mozahler
      Nov 17 '18 at 22:56













    • Excellent. Glad you figured it out!

      – Mozahler
      Nov 18 '18 at 13:35



















    • Thank Mozahler but it didn't work

      – Hosy Hosy
      Nov 17 '18 at 20:45











    • Did you declare your view controller a UIButtonDelegate and set the button's delegate property to self?

      – Mozahler
      Nov 17 '18 at 22:56













    • Excellent. Glad you figured it out!

      – Mozahler
      Nov 18 '18 at 13:35

















    Thank Mozahler but it didn't work

    – Hosy Hosy
    Nov 17 '18 at 20:45





    Thank Mozahler but it didn't work

    – Hosy Hosy
    Nov 17 '18 at 20:45













    Did you declare your view controller a UIButtonDelegate and set the button's delegate property to self?

    – Mozahler
    Nov 17 '18 at 22:56







    Did you declare your view controller a UIButtonDelegate and set the button's delegate property to self?

    – Mozahler
    Nov 17 '18 at 22:56















    Excellent. Glad you figured it out!

    – Mozahler
    Nov 18 '18 at 13:35





    Excellent. Glad you figured it out!

    – Mozahler
    Nov 18 '18 at 13:35











    0














    it's simulator bug. it worked on a real device
    @IBAction func button2(_ sender: UIButton) {
    button2.setImage(UIImage(named: "wrong_answer"), for: .normal)
    }






    share|improve this answer




























      0














      it's simulator bug. it worked on a real device
      @IBAction func button2(_ sender: UIButton) {
      button2.setImage(UIImage(named: "wrong_answer"), for: .normal)
      }






      share|improve this answer


























        0












        0








        0







        it's simulator bug. it worked on a real device
        @IBAction func button2(_ sender: UIButton) {
        button2.setImage(UIImage(named: "wrong_answer"), for: .normal)
        }






        share|improve this answer













        it's simulator bug. it worked on a real device
        @IBAction func button2(_ sender: UIButton) {
        button2.setImage(UIImage(named: "wrong_answer"), for: .normal)
        }







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 11:41









        Hosy HosyHosy Hosy

        44




        44






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53352159%2fwhy-uibutton-requires-two-clicks-to-change-its-image%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            鏡平學校

            ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

            Why https connections are so slow when debugging (stepping over) in Java?