Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since you have custom button, the best solution is to change image of the button depending on the state of a checkbox you have (you mentioned that the idea is to create something like checkbox button).</p> <p>There is a method in UIButton class named "setImage:forState:". You can change the image inside the button using mentioned method. See more info <a href="http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIButton_Class/UIButton/UIButton.html" rel="nofollow">here</a></p> <p>Example how I see the code (you can see it another way):</p> <pre><code>// bool property @property (assign) BOOL isChecked; .... // custom button UIButton *theCheckboxButton = ; [theCheckboxButton setImage:[UIImage imageNamed:@"Unchecked"] forState:UIControlStateNormal]; [theCheckboxButton setImage:[UIImage imageNamed:@"Checked"] forState:UIControlStateSelected]; [theCheckboxButton addTarget:self action:@selector(checkBoxButtonAction:) forControlEvents: UIControlEventTouchUpInside]; ... // handler - (void)checkBoxButtonAction:(UIButton *)aCheckBox { self.isChecked = !self.isChecked; [aCheckBox setImage:[UIImage imageNamed:(self.isChecked ? @"Checked" : @"Unchecked")] forState:UIControlStateNormal]; [theCheckboxButton setImage:[UIImage imageNamed:(self.isChecked ? @"Unchecked" : @"Checked")] forState:UIControlStateSelected]; } </code></pre> <p><strong>EDIT:</strong></p> <pre><code>// images from not external resources @property (strong) UIImage *checkedImage; @property (strong) UIImage *uncheckedImage; // bool property @property (assign) BOOL isChecked; ... // custom button UIButton *theCheckboxButton = ; [theCheckboxButton setImage:self.uncheckedImage forState:UIControlStateNormal]; [theCheckboxButton setImage:self.checkedImage forState:UIControlStateSelected]; [theCheckboxButton addTarget:self action:@selector(checkBoxButtonAction:) forControlEvents: UIControlEventTouchUpInside]; ... // handler - (void)checkBoxButtonAction:(UIButton *)aCheckBox { self.isChecked = !self.isChecked; [aCheckBox setImage:self.isChecked ? self.checkedImage : self.uncheckedImage] forState:UIControlStateNormal]; [theCheckboxButton setImage:(self.isChecked ? self.uncheckedImage : self.checkedImage) forState:UIControlStateSelected]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload