Pergunta

I would like get the integer from the name of a UIButton.

The UIButton's name is "Button11", and I would like my integer therefore like to be 11.

Can anybody please tell me how to do that?

I "already" have the following code:

-(IBAction)processButtonUpInside:(id)sender
{
     UIButton *nButton (UIButton*)sender;
     integer_t nInt = ... //I am not perfectly sure if I should use "integer_t" 
}

Thank you very much!

Foi útil?

Solução

It is better to use tag instead of name for your need.

[aButton setTag:11];


-(IBAction)processButtonUpInside:(id)sender
{
     UIButton *nButton = (UIButton*)sender;
     int nInt =  nButton.tag;
}

But, if you still need the integer in name, then set the button name like this:@"13button"

Now you can get the integer like this.

[nButton.titleLabel intValue];//13

EDIT:

Button name is @"Button13".

   NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
NSString *newString = [[nButton.titleLabel.text componentsSeparatedByCharactersInSet:[numbers invertedSet]] componentsJoinedByString:@""];
//newString = 13
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top