how to check title of uibutton - objective-c

NSString *title=btn.titleLabel.text;
NSLog(#"Title=%#",title);
if(title == #"SelectCategory")
{
//alert
}
else
{
//somecode
}
I want to check title of UIButton. But my code always executing else statement.
What is the error in this code?

Never compare two strings using '==', use isEqualToString
if ([title isEqualToString:#"SelectCategory"]){
//alert
}else{
//somecode
}

Try this line:
If([btn.titleLabel.text isEqualToString:#"Your text"])
{
//do this
}
else
{
//do this
}

NSString *title=[btn currentTitle];
if([title isEqualToString:#"SelectCategory"])
{
NSLog(#"Equal");
}

Use
if ([title isEqualToString:#"SelectCategory"]) {}
instead of == operator.

Use bun.title is Equalto:#"" it will work for you.
Welcome

Never use == to compare strings, with == you're checking if the pointer of a string is the same of another string, and it's not what you want.

Try this
UIButton *YourButton=btn;
if([[YourButton titleForState:UIControlStateNormal] isEqualToString:#"SelectCategory"])
{
// normal
}
else if([[YourButton titleForState:UIControlStateHighlighted] isEqualToString:#"SelectCategory"])
{
//highlighted
}
else if([[YourButton titleForState:UIControlStateSelected] isEqualToString:#"SelectCategory"])
{
//selected
}
else
{
//somecode
}

Two strings or two objects cannot be compared using ==. To compare two objects you should use isEqual.
In this case :
if([stringToBeCompared isEqualToString:#"comparestring"])
{
//statement
}

Related

Reduction number of IF in my case

I have too much IF in my method like this:
if (myObject?.name !=null)
first.text = myObject.name.bigThing
if (myObject?.age !=null)
second.text = myObject.age.bigThing
if (myObject?.surname !=null)
third.text = myObject.surname.bigThing
and 20 more ...
How can I shorten the code?
age/surname/name is type my own class Big with id: Int and bigThing: String
One way could be:
myObject?.age?.let { second.text = it.bigThing }
If you're putting the value inside a TextView:
first.text = myObject?.age?.bigThing
One option would be
fun updateText(x: WhateverTheTypeOfFirstSecondEtcIs, y: Big?) {
if (y != null) { x.text = y.bigThing }
}
updateText(first, myObject?.name)
updateText(second, myObject?.age)
updateText(third, myObject?.surname)
Change each of them like this if you like it:
myObject?.name?.run { first.text = this.bigThing }
I don't know the exact syntax in kotlin, however I would expect to be able to create a method something like this:
String extractBigThing(Big big){
if(big != null) return big. GetBigThing() ;
return null;
}
And call it something like this :
first.text = extractBigThing(myObject.name);
The basic idea is to extract reused functionality into reusable code, these can then be unit tested to increase code robustness.
I hope this helps.
myObject?.name?.bigThing?.let { first.text = it } etc for each line
or
myObject?.apply {
name?.bigThing?.let { first.text = it }
age?.bigThing?.let { second.text = it }
}
etc

Return Statement in Objective C for Binary Search Tree Search Function

I want to write a search function for a Binary Search Tree. How can I write it so that I can return from the base case if(root.data == node.data){return node;} without getting compiler warnings about the method returning nothing. In this case, root is an ivar that's part of the BST class that these methods are a part of.
-(Node*)search:(Node*)node{
if(root == nil){
return node;
}
if(root.data == node.data){
return node;
}
if (node.data < root.data){
root = root.left;
[self search:root];
}
if (node.data > root.data) {
root = root.right;
[self search:root];
}
}
You get the error because you don't return anything from your search: method under certain conditions. You must return something. You also don't make any use of the return value when you recursively call search:.
You also have a problem with updating root. You don't want to do that.
You probably want something more like this:
- (Node *)search:(Node *)node {
if (root == nil) {
return node;
}
if (root.data == node.data) {
return node;
} else if (node.data < root.data) {
return [self search:node.left];
} else // node.data > root.data
return [self search:node.right];
}
}
if none of your first two if tests are true, your method doesn't return anything.
Try;
if (node.data < root.data){
root = root.left;
return [self search:root];
}
if (node.data > root.data) {
root = root.right;
return [self search:root];
}
return ? // you need a return value here

If-else not executing block when bool is true?

This could just as easily be something stupid I missed, but take a look at this code: (which has been stripped down to just debug functionality, although the actual method name was left unchanged)
-(BOOL)shouldHideStatusBarItem:(BOOL)showItem{
if (showItem == YES) {
NSBeep();
NSLog(showItem ? #"YES(inloop)" : #"NO(inloop)" );
}
else if (showItem == NO){
NSBeep();
NSLog(showItem ? #"YES(inloop)" : #"NO(inloop)" );
}
NSLog(showItem ? #"YES" : #"NO" );
return showItem;
}
When I pass in YES it logs:
YES
When I pass in NO it logs:
NO(inloop)
NO
Obviously when I pass in YES it SHOULD log:
YES(inloop)
YES
Does anybody have any ideas?
Change your code to just check the truth of the showItem variable. The BOOL type is not actually restricted to the values YES and NO.
if (showItem)
{
...
}
else
{
...
}
a variation on lakesh's answer
-(BOOL)shouldHideStatusBarItem:(BOOL)showItem{
if (showItem) {
} else {
}
NSBeep();
NSLog(showItem ? #"YES" : #"NO" );
return showItem;
}

Conditional Statement checking length of string

I'm trying to create a conditional statement that returns to me "nothing entered" if the string length is less than 1. However, I'm getting an error message in xcode that says: Expected identifier. I think my syntax may be wrong but I can't figure it out.
- (IBAction)batman:(id)sender {
if ([self.nameTextField.text.length] < 1)
{ //returnType method argument
[NSString stringWithFormat:#"nothing entered."];
}
else {
self.secondLabel.text = [NSString stringWithFormat:#"Batman %#", self.nameTextField.text ];
}
}
Just get rid of your brackets [] and it should work:
- (IBAction)batman:(id)sender {
if (self.nameTextField.text.length < 1) {
self.secondLabel.text = #"nothing entered.";
} else {
self.secondLabel.text = [NSString stringWithFormat:#"Batman %#", self.nameTextField.text ];
}
}

isEqualToString always returns true

for (Annotation *ann in annotaionArray) // annotationArray contains annotations added to map
{
NSString *fetchedtitle = ann.title;
if([fetchedtitle isEqualToString:oldTitle]); // oldTitle = textfield.text
{
ann.title = appDelegate.pinTitle;
break;
}
}
But the comparison is always true. What could be the error please?
fetched const char from sqlite is casted to stringWithUTF8String.
Everything has been done to cast perfectly to string but still why is the error in comparison?
Remove the trailing semi-colon!
if ([fetchedtitle isEqualToString:oldTitle])
{
ann.title = appDelegate.pinTitle;
break;
}
With the semi-colon, your code is the same as:
if ([fetchedtitle isEqualToString:oldTitle])
;
{
ann.title = appDelegate.pinTitle;
break;
}
Since you have a semi colon at the end of your IF statement your code is :
if( [fetchedtitle isEqualToString:oldTitle])
{
// Do nothing
}
// this will always run
ann.title = appDelegate.pinTitle;
break;
replace the following line
if([fetchedtitle isEqualToString:oldTitle]);
with
if([fetchedtitle isEqualToString:oldTitle])
Just remove semicolon(;) from the following part of your code...
if([fetchedtitle isEqualToString:oldTitle]);