How would I compare UIButtons by their titles
On an UIButton I have set the ti开发者_Go百科tle like this
[myButton setTitle:@"Play" forState:UIControlStateNormal];
on another button I have set same title
[myButton1 setTitle:@"Play" forState:UIControlStateNormal];
by setting button tag I can easily compare. But how can I compare buttons by their title name?
Get the title value first..
NSString* str1 = [myButton titleForState:UIControlStateNormal];
NSString* str2 = [myButton1 titleForState:UIControlStateNormal];
Now use the NSString function
- (NSComparisonResult)compare:(NSString *)aString;
- (BOOL)isEqualToString:(NSString *)aString
if([str1 isEqualToString:str2])
{
NSLog(@"Equal");
}
else
{
NSLog(@"UnEqual");
}
[Button1.currentTitle isEqual:Button2.currentTitle]
Was it for iPhone 3.smth or 4.smth? The syntax changes between versions.
You can use UIButton's currentTitle
property.
if([firstButton.currentTitle isEqualToString:secondButton.currentTitle])
{
//Do something
}
精彩评论