for (id btn in [searchMenu subviews]) {
if([btn isKindOfClass:[UIButton class]]){
if ([btn tag]>=1 && [btn tag]<=3) {
if ([btn tag]==flag) {
[btn setBackgroundImage:[UIImage imageNamed:#"all_news_bg.png"] forState:UIControlStateNormal];
}else {
[btn setBackgroundImage:nil forState:UIControlStateNormal];
}
}
}
}
I have five button on searchMenu subview... when button i am clicking the btn tag 3 i need to hidden the btn tag 11.... How to do?
In the search menu subview five button tags are 0,1,2,3,11
I need to hidden the button tag 11 when i click button tag 3.
#Thanks in advance.
You must be receiving your UIButtons pressed event in an one method,
Let's assume that's buttonClicked:
-(void) buttonClicked:(id) sender
{
UIButton* myButton = (UIButton*) sender;
if(myButton.tag == 3)
{
UIButton* buttonWithTaged11 = [myButton.superview viewWithTag:11];
if(buttonWithTaged11)
buttonWithTaged11.hidden = YES;
}
}
Implement this code in the interface action method of the button with tag 3.
for( UIView *view in self.view.subviews ) {
if( [view isKindOfClass:[UIButton class]] ) {
if( view.tag == 11 )
[view removeFromSuperview];// You can hide or remove
}
}
Related
I have UICollectionView with UIButton as a radio Button in UICollectionViewCell.xib.
I need one radioButton selected at time, if next was pressed, previous should be deselected. I will appreciate help.
//Loading radio button when view loads
-(void)setRadioButton{
if (!self.product.isSelectedCell) {
[self.radioBtn setBackgroundImage:[UIImage imageNamed:#"radio-button"] forState:UIControlStateNormal];
}
else
{
[self.radioBtn setBackgroundImage:[UIImage imageNamed:#"radio-button-select"] forState:UIControlStateNormal];
}
}
//Action for radio button
- (IBAction)radioBtnAction:(UIButton*)sender {
if([self.radioBtn isSelected]==YES)
{
[self.radioBtn setBackgroundImage:[UIImage imageNamed:#"radio-button-select"] forState:UIControlStateNormal];
}
else{
//always calls else
[self.radioBtn setBackgroundImage:[UIImage imageNamed:#"radio-button"] forState:UIControlStateNormal];
}
}
Use this code in cellForRowAtIndexPath
UIButton *radioButton = (UIButton *) [cell.contentView viewWithTag:kRadioButtonTag]; // Radio button
[radioButton setImage:[UIImage imageNamed:#"radio_unselected"] forState:UIControlStateNormal];
[radioButton setImage:[UIImage imageNamed:#"RadioSelected"] forState:UIControlStateSelected];
[radioButton addTarget:self action:#selector(radioButtonPressed:) forControlEvents:UIControlEventTouchDown];
if(indexPath == selectedIndexPath)
[radioButton setImage:[UIImage imageNamed:#"RadioSelected"] forState:UIControlStateSelected];
else
[radioButton setImage:[UIImage imageNamed:#"radio_unselected"] forState:UIControlStateNormal];
Radio button taped from table cell and get index path of that button
Take on class Lebel NSIndexPath variable and store selected index path.
- (void)radioButtonPressed:(UIButton *)sender {
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:tableView];
NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:buttonPosition];
if (indexPath != nil) {
selectedIndexPath = indexPath
[tableView reloadData];
}
}
Hope this will help
I have a UITableViewCell with UIButton in each cell. When the button is pressed I set its state to highlighted like in this answer:
[self performSelector:#selector(doHighlight:) withObject:[cell.subviews objectAtIndex:2] afterDelay:0.0];
Then I do this:
- (void)doHighlight:(UIButton *)sender {
if (sender.highlighted) {
[sender setHighlighted:NO];
} else {
[sender setHighlighted:YES];
}
}
But the button not only is just not highlighted at all, not speaking about the fact that I should be able to UNhighlight it.
Any ideas on what is wrong?
I ended up using UIButton's selected property. It does not require any delay and it works brilliantly with this type of things:
if (!sender.selected) {
[sender setSelected:YES];
[cell addSubview:hiddenButton];
[self performSelector:#selector(doHighlight:) withObject:sender];
} else {
[sender setSelected:NO];
[self performSelector:#selector(doUnHighlight:) withObject:sender];
}
Here is a possible solution you are looking for. I have modified my answer
UIButton *button=[cell.subviews objectAtIndex:2];
//I am adding these five lines to ensure the different
//states of the button to achieve your highlighted state goal.
UIImage *highlightImage = [UIImage imageNamed:#"highlight.png"];//Also used when button is selected
UIImage *normalImage = [UIImage imageNamed:#"normal.png"];
[button setBackgroundImage:normalImage forState:(UIControlStateHighlighted)];
[button setBackgroundImage:highlightImage forState:(UIControlStateSelected)];
[button setBackgroundImage:normalImage forState:UIControlStateNormal];
[self performSelector:#selector(doHighlight:) withObject:[cell.subviews objectAtIndex:2]];
-(IBAction) doHighlight:(id)sender
{
if ([sender isKindOfClass:[UIButton class]])
{
UIButton *btn=(UIButton*)sender;
if (btn.isSelected) {
[btn setSelected:NO];
}
else
{
[btn setSelected:YES];
}
}
}
I have a view that has labels being dynamically added by the user. If the use would like to edit any of the labels, they press a button and all the labels get highlighted with a delete button and move button. (Editing is another bridge I’ll cross later).
My issue is: What is the best way to turn the buttons on and off? I have a method that turns the buttons on… but I am at a loss as to how I turn them off when done editing. Do I need to tag my buttons and then just ‘hide them’? Or do I just remove them all totally? How do I parse all the buttons that are turned on then, turn them off. Do I need to put them in an array as well? The labels are tagged with unique numbers so I know which label is which.
Any thoughts? Guidance? If I am doing it all wrong please tell me.
Here is a couple methods I have:
- (void) showEditableText {
// Parse the array of labels
if(textArray.count > 0){
for(UILabel *label in textArray){
//Add Delete Button
UIImage * delButtonImage = [UIImage imageNamed:#"GUI_Delete.png"];
UIButton * delThisButton = [[UIButton alloc] initWithFrame:CGRectMake(label.frame.origin.x - delButtonImage.size.width, label.frame.origin.y - delButtonImage.size.height, delButtonImage.size.width, delButtonImage.size.height)];
[delThisButton setBackgroundImage:delButtonImage forState:UIControlStateNormal];
[delThisButton addTarget:self action:#selector(deleteThisLabel:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:delThisButton];
//Add a move button
UIImage * moveButtonImage = [UIImage imageNamed:#"GUI_Move.png"];
UIButton * moveThisButton = [[UIButton alloc] initWithFrame:CGRectMake((label.frame.origin.x + label.frame.size.width + moveButtonImage.size.width), label.frame.origin.y - moveButtonImage.size.height, moveButtonImage.size.width, moveButtonImage.size.height)];
[moveThisButton setBackgroundImage:moveButtonImage forState:UIControlStateNormal];
[moveThisButton addTarget:self action:#selector(moveThisLabel:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:moveThisButton];
//Make the text highlighed
label.highlighted = YES;
label.backgroundColor = [UIColor colorWithRed:203/255.0f green:230/255.0f blue:239/255.0f alpha:1];
label.highlightedTextColor = [UIColor redColor];
}
}
}
- (void) doneEditingText {
if(textArray.count > 0){
for(UILabel *label in textArray){
//THIS IS WHERE I AM STUCK? WHAT DO I DO?
label.highlighted = NO;
label.backgroundColor = [UIColor clearColor];
}
}
}
//inside your first method set the same tag to your all buttons
-(void) showEditableText {
........
.......
.......
delThisButton.tag = 10;
moveThisButton.tag = 10;
}
//inside your second method delete all the subviews using this tag as shown below..
-(void) doneEditingText {
if(textArray.count > 0){
for(UILabel *label in textArray){
..............................
//THIS IS WHERE I AM STUCK? WHAT DO I DO?
for (UIView *subview in [self.view subviews]) {
if (subview.tag == 10) {
[subview removeFromSuperview];
}
}
...............................
label.highlighted = NO;
label.backgroundColor = [UIColor clearColor];
}
}
}
Move all your buttons code to viewDidLoad, hide them (button.hidden = YES). When you start/finish editing your text, unhide and hide your buttons. You need to have an ivar to contain the buttons too. So add them in your .h file.
Try This
UIView * seletetButton = nil;
for (UIView * btn in self.view.subviews){
if ([btn isKindOfClass:[UIButton class]])
{
if (seletetButton.tag != btn.tag)
{
[btn removeFromSuperview];
}
}
}
After a lot of trouble I was able to add a RETURN button to a numerical key pad.
I want to delete it when introduciing other data into other uitextfields because currently that button appears everytime I use the keyboard, it's part of the keyboard.
Im adding the event handler on KeyBoardDidShow, otherwise it doesnt work,
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
Then in KeyboarWillShow
- (void)keyboardWillShow:(NSNotification *)note {
// create custom button
NSLog(#"El note es: %#", note);
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton setImage:[UIImage imageNamed:#"done.jpg"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"done_pressed.jpg"] forState:UIControlStateHighlighted];
[doneButton addTarget:self action:#selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
UIWindow *keyboardWindow = nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
if (![[testWindow class] isEqual:[UIWindow class]]) {
keyboardWindow = testWindow;
break;
}
}
if (!keyboardWindow) return;
// Locate UIKeyboard.
UIView *foundKeyboard = nil;
for (UIView *possibleKeyboard in [keyboardWindow subviews]) {
// iOS 4 sticks the UIKeyboard inside a UIPeripheralHostView.
if ([[possibleKeyboard description] hasPrefix:#"<UIPeripheralHostView"]) {
possibleKeyboard = [[possibleKeyboard subviews] objectAtIndex:0];
}
if ([[possibleKeyboard description] hasPrefix:#"<UIKeyboard"]) {
foundKeyboard = possibleKeyboard;
break;
}
}
if (foundKeyboard) {
// Add the button to foundKeyboard.
[foundKeyboard addSubview:doneButton];
}
}
The thing is that this creates the button always! Obviously! I would like to create the button only in certains uitextfields, not in all, but how to do that?
The method - (BOOL)textFieldShouldBeginEditing: doesnt execute the first time, only the rest, why? Dont know, but if I was able to know which uitextfield is calling the keyboard my hair would grow again and I'd be slimmer, anyone can help?
textFieldShouldBeginEditing: only fires consistently if each and every one of your text fields have delegates set properly (check your XIB).
And then you can set a tag value in your text fields. You can display the Return button for fields with tag 1 and no Return button for fields with tag 0.
You can try to count the number of subviews inside the keyboard to know how many keys it has. That is a bad hack, but you already have a bad hack to find the keyboard :-)
Also another suggestion:
if (foundKeyboard) {
UIView *doneButton = [foundKeyboard viewWithTag:BUTTON_TAG];
if(numPad){
if(buttonView==nil){
//create doneButton
[foundKeyboard addSubview:doneButton];
}
}else{
[doneButton removeFromSuperview];
}
}
I have this code to control two check boxes (buttons customized):
- (IBAction) setCheckBox: (id) sender{
UIImage *selected = [UIImage imageNamed:#"checkbox_checked.png"];
UIImage *notSelected = [UIImage imageNamed:#"checkbox_unchecked.png"];
if ([sender isSelected])
{
[sender setImage:notSelected forState:UIControlStateNormal];
[sender setSelected:NO];
if ([sender tag] == 10) boolOne = FALSE;
if ([sender tag] == 11) boolTwo = FALSE;
}
else
{
[sender setImage:selected forState:UIControlStateSelected];
[sender setSelected:YES];
if ([sender tag] == 10) boolOne = TRUE;
if ([sender tag] == 11) boolTwo = TRUE;
}
}
You can see that this code control two checkboxes, when I press one it become checked and when I press another time it become unchecked.
Now I want change this code in this way:
In default checkbox with tag 10 is checked and when I press it, I haven't effects, but when I press checkbox with tag 11 it should be checked and checkbox 10 should be unchecked.
The checkbox should toggle the selection and also the bool value
if([sender isSelected])
return;
if([sender tag] == 11){
[sender setSelected:YES];
[checkbox10 setSelected:NO];
}
if ([sender tag] == 10) {
[sender setSelected:YES];
[checkbox11 setSelected:NO];
}
you can get checkbox by tag value like I assume checkbox are instance of uibutton so
UIButton *checkBox10 = (UIButton*)[self.view viewWithTag:10];
I hope you understand. Also set the images according to code.
If you want the behavior of radio buttons, you should probably use radio buttons instead of check boxes.
Assuming that you store your check boxes as instance variables named checkbox10 and checkbox11, this should solve your problem:
- (IBAction) setCheckBox: (id) sender{
UIImage *selected = [UIImage imageNamed:#"checkbox_checked.png"];
UIImage *notSelected = [UIImage imageNamed:#"checkbox_unchecked.png"];
if (![sender isSelected])
{
[sender setImage:notSelected forState:UIControlStateNormal];
[sender setSelected:YES];
if ([sender tag] == 10)
{
boolOne = YES;
boolTwo = NO;
[checkbox11 setImage:notSelected forState:UIControlStateNormal];
[checkbox11 setSelected:NO];
}
if ([sender tag] == 11)
{
boolTwo = YES;
boolOne = NO;
[checkbox10 setImage:notSelected forState:UIControlStateNormal];
[checkbox10 setSelected:NO];
}
}
}