When using a SearchBarDisplay, how to change name of the cancel button - iphone

When setting a SearchBarDisplay, using IB or programmatically it gets a Cancel button right to the search window. The text on the button says "Cancel". How do I change text to another language.
I know how to change text for a Done button at the Navbar using only SearchWindow but that does not seem to work for this button which comes as default.
And a similar question is the text "No result" coming up in the table if no match.
How change to another language?

You can change title of you cancel but of searchBar by this :
for (UIButton *v in srchBar.subviews)
{
NSLog(#"%#",v);
if ([v isKindOfClass:[UIButton class]])
{
[v setTitle:#"Hello" forState:UIControlStateNormal];
}
}
now set this title as per your language.
i tried this and its working in my demo app.
let me know if it wont work for you.

Related

What's the preferred way to set accessibility label on UIActionSheets

Looking at adding KIF to our app for integration testing. I'd like to be able to select a button from a UIActionSheet but it doesn't seem like there's an efficient way to set the accessibility labels on each button in the action sheet.
I'm looking at using the accessibleElementCount, iterating through the buttons that way. Is that the preferred way to do this or is there a more standard way of setting up accessibility for UIActionSheets?
From the UIKit User Interface Catalog:
Action sheets are accessible by default.
Accessibility for action sheets primarily concerns button titles. If VoiceOver is activated, it speaks the word “alert” when an action sheet is shown, then speaks its title if set (although iOS human interface guidelines recommend against titling action sheets). As the user taps a button in the action sheet, VoiceOver speaks its title and the word “button.”
From your question it's not clear if you want to change the accessibility label to something other than the button title + button, but it appears that that's not possible in a straight-forward way.
you have must implements the UIActionSheetDelegate in your class.
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
for (UIView *_yourView in actionSheet.subviews) {
if ([_yourView isKindOfClass:[UILabel class]]) {
[((UILabel *)_yourView) setFont:[UIFont boldSystemFontOfSize:14.0f]];
}
}
}
If you want to have some custom control over the UIActionSheet (from a testing perspective using KIF) , the simplest way for me to do it , was replacing it with UIAlertController , if you have in mind that subclassing is not possible in both Classes , and if you test multi language app like me - you will find yourself in a trouble . You can set properties to the UIAlertAction's , here is some code , so you can have the idea :
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:[LI18n localizedString:#"YES"]
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Handle your yes please button action here
}];
yesButton.accessibilityLabel = #"YES_BUTTON";

set UIButton's image in interface builder for differen state

I am facing very strange issue.
In interface builder I am trying to set images for "normal" state config and for "selected" state config. but by clicking the button my image is not getting changed.
even I write the code in viewDidLoad method.
[btnCheckBoxMale setImage:[UIImage imageNamed:#"blankcheckbox.png"] forState:UIControlStateNormal];
[btnCheckBoxMale setImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateSelected];
but my images are not getting changed on cliking on it
can anyboday say what is the problem?
Set the image to different states in interface builder instead of setting it in code. Please see the screenshot for how to set it. Try this & if you are facing any problem then ask me.
It will work if you set manually in button action:
btnCheckBoxMale.selected = YES;
Because by default UIButton has only two states - Normal and Highlighted. And other states like selected and disabled you have to control manually.
just set selected when you call the method on button click event.. for example..
-(IBAction) btnCheckBoxMale_Clicked:(id)sender{
[btnCheckBoxMale setSelected:YES];
}
-(IBAction) btnCheckBoxMale_Clicked:(id)sender{
UIButton * btnCheckBoxMale = (UIButton *)sender;
[btnErase setSelected:! btnCheckBoxMale];
}

Change UIButton image when button pressed

I'm trying to change the image button just in the moment that the button is pressed. When released, i want go back to the original.
I read some posts here, but it's not working.
Is it possible do it in the GUI without programming?
I tried this:
-(IBAction) toggleUIButtonImage:(id)sender{
if ([sender isSelected]) {
[sender setImage:unselectedImage forState:UIControlStateNormal];
[sender setSelected:NO];
}else {
[sender setImage:selectedImage forState:UIControlStateSelected];
[sender setSelected:YES];
}
}
But what kind of sender events should i associated the function toggleUIButtonImage ?
Thanks
You can do it in interface builder by giving different settings to each button state config:
Or you can also change the 'settings' of the button in code by using 'UIButton' setImage:forState method.
[myButton setImage:highlightedImage forState:UIControlStateHighlighted];
Usually, you should do it by 'configuring' the button as described above and not by changing the sub views of the button manually as a response to user interaction. The reason it doesn't work for you is the inherent hidden behaviour of UIButton. You manually make the change of a subview of the button as a response to user interaction, but your change is changed back to what was defined in the settings of your button (according to the button's state). So it seems like your code doesn't do anything at all.
in the ViewDidLoad Method
set the property of
[[button setImage:[UIImage ImageNamed:#"unselectedImage.png"] forState: UIControlStateNormal];
[[button setImage:[UIImage ImageNamed:#"selectedImage.png"] forState: UIControlStateHighlighted ];
This can be done with Interface Builder, no code necessary.
Select your button in IB, go to the right sidebar, change the Type to Custom, make sure State Config is Default, and type in the name of your "regular image" reference in the Background Image text field.
Then change State Config to Highlighted, and type in the name of your "pressed image" reference in the Background Image text field.
In IB, you can set the image for the Highlighted (not Selected) state of the button. That should do it.
Look in the Button section of the Utilities pane, and select Highlighted from the State Config drop-down.

How to set UIButton title to null or an empty string

TL;DR - My question really is: How to I set a UIButton's title to an empty string, or null after it has already been assigned a previous title.
I have a UIButton that accepts a string generated based on the text from an NSString variable as it's title using the following:
NSString *MyStringVariable = #"HELLO";
...
[myButton setTitle:MyStringVariable forState:UIControlStateNormal];
For the sake of this question, let's just assume I have "HELLO" set on my NSString variable. This then makes my UIButton have the title "HELLO", obviously...
I then have another button which removes this title from the UIbutton, like so:
[myButton setTitle:#"" forState:UIControlStateNormal];
Which does hide the UIButton's title (i.e. it now just looks like an empty UIButton), but upon further inspection, the title does infact still exist. If I add the following to my UIViewController:
- (void)viewWillAppear:(BOOL)animated{
NSLog(#"Title: %#", myButton.titleLabel.text);
}
and navigate to and fro from this view, the title continues to get logged in the console, even though I've set the text to #"".
I have tried the following methods to make the title truly go away:
[myButton setTitle:#"" forState:UIControlStateNormal];
[myButton setTitle:NULL forState:UIControlStateNormal];
[myButton setTitle:nil forState:UIControlStateNormal];
myButton.titleLabel.text = #"";
The weird thing is, if I set the title to #" " (with a space):
[myButton setTitle:#" " forState:UIControlStateNormal];
the title does update correctly, and the space is then logged to the console, rather than the old title.
My question really is: How to I set a UIButton's title to an empty string, or null after it has already been assigned a previous title.
I too faced the same issue. I was setting the title of the button to #"" for all the states, and at a later point when I check the titlelabel.text it gives me the previous title which is not #"". So I printed out all the possible values of a button as suggested by PengOne and found that the properties titleForState and currentTitle give the updated/correct values.
So instead of checking the titleLabel.text, try checking the currentTitle or titleForState.
I still haven't figured out why the titleLabel is not updated properly.
I believe this has to do with how the UIButton updates its titleLabel property when setTitle:forState: is called.
For example, if you try to set the title of a button by directly accessing the titleLabel.text property, it does not work (check out the myriad questions on SO about this).
If you truly want to understand what's going on, then you should look at both the titleLabel.text and the current state of the button. One way to do this is to log titleLabel.text, currentTitle, and titleForState: in viewWillAppear, viewDidAppear, viewWillDisappear, and viewDidDisappear. The first two (titleLabel.text, currentTitle,) should coincide, and the third should be what you expect (titleForState:).
This sounds like a bug in the SDK, create a bugreport.
If you really need a button without a title, you'll probably have to create one yourself. A UIView with it's layer set to have a cornerRadius (and hence be rounded), which responds to user interaction should do it. Also, check the three20 toolkit, they may have a custom button or two in there you could use to save some time.
I have seen the same issue but in a different context. I have been using NSLocalizableString to change the text in a button.
It appears that setting the UIButton.title changes its UIButton.currentTitle but not its UIButton.titleLabel.text.
Though it may not make much of a difference for your application, the fact that titleLabel.txt and currentTitle are different turns to be more of a "be aware" than an implementation challenge.

iphone sdk- UIAlertView button with image

can you please tell me if it is possible to use an uiimage view as uialertview button "text"?
i was unable to find the answer googling..
thanks a lot
Yes, it is possible, but I wouldn't recommend it. You'd have to run through the UIAlertView's subviews until you find one that is of the right class, I would assume UIButton, and add your UIImageView on top like this:
for (UIView *v in [myAlertView subviews]) {
if ([v isKindOfClass:[UIButton class]]) {
//IF I AM THE RIGHT BUTTON
[v addSubview:myUIImageView];
}
}
To determine the correct button, you might initially give it some odd text like "foobar5" and test each button to see if that is its text, and if so, remove the text and add the UIImageView.
You can use unicode characters, which are rendered as icons.
I described it here:
Adding Images to UIActionSheet buttons as in UIDocumentInteractionController