I have written some code which toggles 2 buttons depending on which one is selected. If the UK one is selected it becomes ticked and the BR one becomes unticked, and vice versa. However, this only seems to be the case for the UK button. If I select the BR button than the UK button unticks, the BR button briefly ticks but then it unticks again.
I have linked up my buttons correctly (I have triple checked), and as the BR button briefly ticks it is definitely linked up. The code I am using is below:
.h
#property (weak) IBOutlet UIButton *btUK;
#property (weak) IBOutlet UIButton *btBR;
.m
- (IBAction)changePortal:(id)sender
{
UIButton *button = (UIButton *)sender;
if (button.tag == kUKButton)
{
self.btUK.imageView.image = [UIImage imageNamed:#"tick_box.png"];
self.btBR.imageView.image = [UIImage imageNamed:#"tick_box_empty.png"];
[Singleton sharedSingleton].bUseUKPortal = YES;
}
else if (button.tag == kBRButton)
{
self.btBR.imageView.image = [UIImage imageNamed:#"tick_box.png"];
self.btUK.imageView.image = [UIImage imageNamed:#"tick_box_empty.png"];
[Singleton sharedSingleton].bUseUKPortal = NO;
}
}
I have set break points within the code and have confirmed that both the buttons go in to their relevant sections when clicked. I can also confirm that no other code is using the btUK and btBR variables as I have just written it all.
Both buttons have changePortal set as their action, and the function is only called once per click.
I have also tried cleaning the code but this did not fix my issue.
If anyone can shed any light as to why this is happening then I would be very grateful.
The correct way to set the image of a UIButton is to call setImage:forState:.
So try to alter your code to something like this:
// Follow this pattern for every button image change
[self.btUK setImage:[UIImage imageNamed:#"tick_box.png"] forState:UIControlStateNormal];
Now regarding the imageView property the documentation states that:
The button’s image view. (read-only)
#property(nonatomic, readonly, retain) UIImageView *imageView
Discussion
Although this property is read-only, its own properties are
read/write. Use these properties to configure the appearance and
behavior of the button’s view. For example:
UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
button.imageView.exclusiveTouch = YES;
The imageView property returns a value even if the button has not been
displayed yet. The value of the property is nil for system buttons.
Related
I am new to Objective-C so i am mostly using Storyboard, Someone Please tell me how to create a checkbox using Xcode in iOS.
UISwitch is the standard control used in iOS for indicating an on/off, selected/unselected state. Why not use that?
UISwitch is the standard control used in IOS applications for making binary choices but if you want to use checkbox you can create a UIButton
#property (weak, nonatomic) IBOutlet UIButton *CheckBox;
- (IBAction)CheckBoxClick:(id)sender
and change its background image on click event of UIButton
- (IBAction)CheckBoxClick:(id)sender {
if(!checked){
[_CheckBox setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
checked = YES;
}
else if(checked){
[_CheckBox setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
checked = NO;
}
}
also there are more detailed answers to this question at
How to create a simple checkbox in iOS?
and if you dont want to use images you can check the following
Checkbox in iOS application
CheckBox is not available in object library. You can use third party library for that purpose or you can create it by your self.
There is the working code for checkbox.
create a class level variable and property of button in #inteface
#interface testViewController (){
BOOL checkBoxSelected;
}
#property (weak, nonatomic) IBOutlet UIButton *checkBox;
#end
in viewdidload set images for the button states.
[_checkBox setBackgroundImage:[UIImage imageNamed:#"checkBoxUnChecked.png"]
forState:UIControlStateNormal];
[_checkBox setBackgroundImage:[UIImage imageNamed:#"checkBoxChecked.png"]
forState:UIControlStateSelected];
and after create a button action in that button Action.
checkBoxSelected = !checkBoxSelected; /* Toggle */
[_checkBox setSelected:checkBoxSelected];
Hope it helps
1) Create Prototype cell in UITableView.
2) Add one button inside the cell and set button style to Custom.
3) Set the image for checkbox.
ios language doesn't use checkbox controller.
but you are use checkbox that you are inport two image select & unselect
Step 1> Create button and set image.
Step 2> Create button touch object method and put if condition for check & uncheck.
for example:
- (IBAction)btnLogin:(id)sender {
UIButton *btn = (UIButton *)sender;
if (btn.tag == 0) {
btn.tag = 1;
//set image checked.
}
else{
btn.tag = 0;
//set image unchecked.
}
}
You shouldn't need to subclass the UIButton class. By design, Objective-C favors composition over inheritance.
UIButton is a subclass of UIControl, which has a selected property. You can use this property to toggle the on/off behaviour of a checkbox, just the same way a UISwitch does.
You can attach an action to the button's touched up inside event, and perform the toggling in there, something like this:
// when you setup your button, set an image for the selected and normal states
[myCheckBoxButton setImage:nonCheckedImage forState:UIControlStateSelected];
[myCheckBoxButton setImage:nonCheckedImage forState:UIControlStateNormal];
- (void)myCheckboxToggle:(id)sender
{
myCheckboxButton.selected = !myCheckboxButton.selected; // toggle the selected property, just a simple BOOL
}
I have a simple UITableViewController with a NavigationController as the root view controller. I have added an "Options" UIBarButtonItem to the navigation bar and a "Start" UIBarButtonItem to the toolbar, shown below:
. The issue that I am experiencing is that the "Options" button will become highlighted when pressed, like a regular UIButton would, but the "Start" button on the toolbar does not. This is very inconvenient, as it makes it very hard for the user to know if they actually pressed the button or not. This behavior is shown below:
Options Button Not Pressed:
Options Button Pressed:
Start Button Not Pressed:
Start Button Pressed:
I can't figure out how to fix this behavior. I did verify that the "Start" button actually works, so the highlighting issue is not because the button is not working. Also, interestingly enough, the "Start" button does become highlighted when it is long pressed.
You simply misconfigured the UIbarButtonItem/UIButton in Interface Builder's Attributes Inspector.
There is no point to waste time investigating such a trivial issue, no matter how puzzling it may seem. Just delete that Start button and drop a new one into the toolbar again.
There is no reason for it to behave differently that the options button above.
You should change button type from UIButtonTypeCustom to UIButtonTypeSystem.
Make sure your button gets initialised with the .Default state in IB.
Then make sure the .Highlighted state Text Color is different than the .Default state Text Color.
Enable: Shows Touch On Highlight for tests.
There isn't a built-in way, but I can think of a few custom approaches:
Bind the button to a target method that toggles whatever the button
is meant to toggle, and then changes the button's image property
accordingly. You can use 2 images one for the selected state one
for the default.
Create your own subclass of UIBarButtonItem that looks something like
this:
#interface CustomBarButtonItem : UIBarButtonItem {
BOOL _state;
UIImage * selectedImage;
UIImage * defaultImage;
}
- (BOOL)toggleState;
#property (nonatomic, retain) UIImage * selectedImage;
#property (nonatomic, retain) UIImage * defaultImage;
#end
#implementation CustomBarButtonItem
- (BOOL)toggleState {
if (_state) {
// Switch to Off state
self.image = defaultImage;
}
else {
// Switch to On state
self.image = selectedImage;
}
return _state = !_state;
}
#end
is the toolbar set to translucent? I suspect your nav bar is and tool bar is not.
I have a question and I didn't find an answer on it yet. I would like to know how to set the background image of a button in your code. I would like to use a value to change the background.
For example: if (value == one) { set background of button code}
I have two backgrounds but when you give a code you can use buttonimage1.png, buttonimage2.png.
Thnx
First you should add IBOutlet for your button, example:
#property(nonatomic, weak) IBOutlet UIButton *bttn;
next, using this outlet you can change background:
if (value == one) {
[self.bttn setBackgroundImage:[UIImage imageNamed:#"buttonimage1.png"]
forState:UIControlStateNormal];
} else {
[self.bttn setBackgroundImage:[UIImage imageNamed:#"buttonimage2.png"]
forState:UIControlStateNormal];
}
that's all
If you have a button that was created in Interface Builder and already points to another viewController how do you target that button in code to give it a custom background for normal and selected state in code?
UIImage *registerButtonNormal = [[UIImage imageNamed:#"yellowRegisterButton"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
[self.registerButton setBackgroundImage:registerButtonNormal forState:UIControlStateNormal];
UIImage *registerButtonSelected = [[UIImage imageNamed:#"yellowRegisterButtonSelected"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
[self.registerButton setBackgroundImage:registerButtonSelected forState:UIControlStateSelected];
This is actually really easy.
You just have to create a #property and connect the button you want to style by control dragging from the button to the newly created property.
The property should look something like this:
#property (nonatomic, strong) IBOutlet UIButton *registerButton;
Then in the viewDidLoad, you put the above code that targets your button:
- (void)viewDidLoad
{
[super viewDidLoad];
// code for styling button goes here.
}
You can see the code for styling the button in the question.
How can I add a clear button (cross inside a circle) for UITextView like UITextField has?
Based on the answer from GhostRider a more accurate and up to date implementation:
int kClearButtonWidth = 15;
int kClearButtonHeight = kClearButtonWidth;
//add the clear button
self.clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.clearButton setImage:[UIImage imageNamed:#"UITextFieldClearButton.png"] forState:UIControlStateNormal];
[self.clearButton setImage:[UIImage imageNamed:#"UITextFieldClearButtonPressed.png"] forState:UIControlStateHighlighted];
self.clearButton.frame = CGRectMake(0, 0, kClearButtonWidth, kClearButtonHeight);
self.clearButton.center = CGPointMake(self.textView.frame.size.width - kClearButtonWidth , kClearButtonHeight);
[self.clearButton addTarget:self action:#selector(clearTextView:) forControlEvents:UIControlEventTouchUpInside];
[self.textView addSubview:self.clearButton];
And the method
- (void)clearTextView:(id)sender{
self.textView.text = #"";
}
You can use this images for the two states of the button:
just make a uibutton and put it on uitextview and set its action for clear text view;
uitextview.frame = (0,0,320,416);
uibutton.frame = (310,0,10,10);
[uibutton setimage:#"cross.png" forcontrolstate:uicontrolstatenoraml];
[uibutton addTarget:self action:#selector(clearButtonSelected:) forControlEvents:UIControlEventTouchUpInside];
-(void)clearButtonSelected{
uitextview=#"";
}
hope you want to clear the text view text when you click on cross button above is help
if not understand then i can send you proper program for that
From product perspective, if you're going to have a clear button, you probably want to use a UITextField instead of a UITextView and UITextField supports a clear button natively - set the clearButtonMode property as such:
UITextField *textfield = ...;
textfield.clearButtonMode = UITextFieldViewModeAlways;
See screenshot:
You could use UITextFieldViewModeWhileEditing to only present the clear button while the user is actively updating the content.
There's nothing built in like there is for the UITextField. You'd have to add the view yourself (probably a UIButton) and place it correctly and also somehow get the text to wrap around it correctly. (And I don't think the latter is really possible.)
Maybe instead you should display a toolbar above the keyboard (or an inputAccessoryView if you're targeting 3.2 and later) that provides a clear button.
For me changing the .frame or the .contentInset properties did not work.
For me the best result came from:
1) adding a UIView to the controller, give it round corners and a border to mimic a UITextView.
self.viewTextBackground.layer.borderColor = [UIColor colorWithRed:171/255.0 green:171/255.0 blue:171/255.0 alpha:1.0].CGColor;
self.viewTextBackground.layer.borderWidth = 1.0f;
self.viewTextBackground.layer.cornerRadius = 9.0f;
2) place UITextView on top of this UIView. Place it so that the borders of the underlying UIView stay visible.
3) give the UITextView round corners:
self.textNote.layer.cornerRadius = 9.0f;
3) Make its width fe. 30pixels less compared to the underlying UIView. You now have space for a clear-button.
4) simply add a UIButton to your controller and place it in the top-right corner of the underlying UIView.
5) change the buttons properties: set its type to 'custom' and set its image to the image of a grey cross.
6) bind an action to the button te clear the UITextView
You can add a clear button like the one in the attached screenshot with minimal coding. Just follow these steps:
Select the storyboard and drag a UIButton into your UITextView
Set the buttons constraints
Assign a title or a background image
Create the button's IBOutlet reference and the action (see onClearPressed(_:) below) for "Touch Up Inside" in the ViewController
Implement the textViewDidChange(_:) UITextViewDelegate delegate method, and make sure to set the button's isEnabled property based on the textfield content, e.g.:
func textViewDidChange(_ textView: UITextView) {
clearButton.isEnabled = !textView.text.isEmpty
}
Implement the onClearPressed(_:) action:
#IBAction func onClearPressed(_ sender: Any) {
textView.text = ""
clearButton.isEnabled = false
}
That's it.