Using UIButton currentTitle to create an action

Discussion in 'iOS Programming' started by ledzepster, Jun 2, 2010.

  1. ledzepster macrumors newbie

    Joined:
    Jun 2, 2010
    #1
    Hi,

    I've spent hours looking for an answer to what I know is a newbee question but to no avail. I know the solution has to be straightforward but....

    I want to use the title from a button to perform a task. What I have is the following:

    -(IBAction)doSomethingWithButton: (id)sender {

    NSString *whatToDo = [(UIButton *)sender currentTitle];
    if (whatToDo == @"+")
    {//do some action
    }
    [whatToDo release];
    }

    The problem is that the logical operator doesn't work. It seems that whatToDo is type NSString and is incompatible with @"+".

    Any help appreciated!

    thanks
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    You cannot compare two NSStrings with ==. In fact, in general, you cannot do that with two objects: you are comparing the addresses of the pointers. Not the content of the two objects.

    Is isEqualToString:
     
  3. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #3
    Also, you should not be releasing whatToDo because you don't create or retain it.
     
  4. ledzepster thread starter macrumors newbie

    Joined:
    Jun 2, 2010
    #4
    thanks!

    thanks to both of you.obviously a fundamental flaw in my thinking - I'm from a cobol / .net background. Cheers
     

Share This Page