Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8975513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:54:07+00:00 2026-06-15T18:54:07+00:00

Can someone please educate me why the following does not work? The button never

  • 0

Can someone please educate me why the following does not work? The button never gets set to selected.

[self.boldButton setSelected:isBold];

If I replace the above with an if else statement it works fine. I can also change the setSelected values to 1 or 0, instead of YES or NO and it still works fine.

if (isBold)
{
    [self.boldButton setSelected:YES];
}
else
{
    [self.boldButton setSelected:NO];
}

So I have a working project, but I don’t understand why these two implementations don’t deliver the same results. Thanks.

FWIW – I test for bold with another method. Though if the test were flawed, I don’t see how the second approach could work, while the first still doesn’t.

- (BOOL)isBold
{
    CTFontRef fontRef = (CTFontRef)CFBridgingRetain(self);
    CTFontSymbolicTraits symbolicTraits = CTFontGetSymbolicTraits(fontRef);
    return (symbolicTraits & kCTFontTraitBold);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:54:09+00:00Added an answer on June 15, 2026 at 6:54 pm

    BOOL is defined like this in <objc/objc.h>:

    typedef signed char     BOOL;
    

    That means a BOOL can actually hold any value in the range -128 through 127 (inclusive).

    -[UIControl setSelected:] works roughly like this:

    #define kSelectedBitPosition 10
    #define kSelectedBit (1 << kSelectedBitPosition)
    
    - (void)setSelected:(BOOL)selected {
        if (((self->_controlFlags >> kSelectedBitPosition) & 1) == selected) {
            return;
        } else {
            self->_controlFlags = (self->_controlFlags & ~kSelectedBit)
                | ((selected & 1) << kSelectedBitPosition);
            [self setNeedsDisplay];
        }
    }
    

    (I disassembled the simulator version of UIKit with Hopper to figure that out.)

    So, notice two things:

    • The if statement condition can only be true if selected == 0 or selected == 1. It will never be true if selected has any other value.

    • The assignment statement (that updates _controlFlags) only uses bit 0 (the 1’s bit) of selected. So, for example, if selected == -2, which is logically true in C and has every bit set except bit 0, the assignment statement will still not turn on the bit in _controlFlags.

    This means that you must pass 0 or 1 to -[UIControl setSelected:]. No other value will work reliably.

    The shortest way to convert all non-zero values to 1 in C is by applying the ! operator twice:

    [self.boldButton setSelected:!!isBold];
    

    However, it would probably be better to fix your -isBold method to return a “safe” BOOL instead:

    - (BOOL)isBold {
        CTFontRef fontRef = (CTFontRef)CFBridgingRetain(self);
        CTFontSymbolicTraits symbolicTraits = CTFontGetSymbolicTraits(fontRef);
        return !!(symbolicTraits & kCTFontTraitBold);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please explain what the & does in the following: class TEST {
Can someone please explain why this Emacs regexp find_class \(\w+|([^)]+)\) \(\w+|([^)]+)\) does not match
Can someone please explain to me why the hell? When I try to set
Can someone please explain what the following javascript statement is doing? var default_hide =
Can someone please derive a concrete example from the following: http://www.urdalen.com/blog/?p=210 ..that shows how
Can someone please explain why this doesn't work? MyClass myClass1 = new MyClass(); object
Can someone please explain the difference between the following queries. The joins in the
Can someone please tell me why, even though I've set my Window to SizeToContent
Can someone please explain to me why the padTo method of ArrayBuffer doesn't work
Can someone please explain to me how does Java garbage collector realize those memory

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.