2

I have a button named by

UIButton *button1;

how can i save 'button1' in string? or am i able to save it or not?

Steve
  • 1,022
  • 2
  • 9
  • 30
  • 1
    If you explain why you want to do this we'll be better able to help you. – Abizern Aug 20 '13 at 07:17
  • Check this http://stackoverflow.com/questions/15879357/how-to-get-instance-variable-name , http://stackoverflow.com/questions/6615826/get-property-name-as-a-string – Kalpesh Aug 20 '13 at 07:24
  • Actually i have dozens of button in my project, which is saved by the name of a character, and i need to play the sound on these buttons, so here i need the name of every button, through which i will able to play sound regarding its name, hope you got ma point. – Steve Aug 20 '13 at 07:32
  • what about the `tag` value and collection array of `UIButton`? – holex Aug 20 '13 at 07:44

5 Answers5

0

You can save its' address in memory only: NSString *but1=[NSString stringWithFormat:@"%@",&button1];, but if you need to get unique indicator of your buttons, you can use its' tags: button1.tag Or you can create NSMutableDictionary and add buttons for keys, which equals their names.

Kepler
  • 705
  • 1
  • 5
  • 19
0

You can create a macro like this:

#define getVariableName(var) [NSString stringWithFormat:@"%s", #var]

And use it:

NSLOG(@"My variable name is %@", variableName(self.button1));

You'll see

My variable name is button1
Injectios
  • 2,777
  • 1
  • 30
  • 50
0

No you can't do this, as UIButton is an object and you are declaring UIButton as button1. This will remain static as it holds the reference in memory.

NSString is something that you can chage any time, but for Object and varible declaration you can't change it.

Sawant
  • 395
  • 6
  • 22
-1

you get button title in string but not saved button outlet in string you save button outlet in id using this you save button title

  NSString *btn = Mybutton.titleLabel.text;

for save button outlet

   Iboutlet Uibutton *myButton;
    id *myBtn=myButton;
Waseem Shah
  • 2,219
  • 23
  • 23
-2

Check this

import

#import "objc/runtime.h"


-(IBAction)btnItemListClicked:(id)sender

{
UIButton *btn=sender;
NSString *name = nil;

uint32_t ivarCount;
Ivar *ivars = class_copyIvarList([self class], &ivarCount);

if(ivars)
{
    for(uint32_t i=0; i<ivarCount; i++)
    {
        Ivar ivar = ivars[i];

        id pointer = object_getIvar(self, ivar);
        if(pointer == sender)
        {
            name = [NSString stringWithUTF8String:ivar_getName(ivar)];
            break;
        }
    }

    free(ivars);
}
NSLog(@"%@", name);
}

Output is:

Printing description of name:

btnconferenceCall

Check this sample demo

ButtonDemo

NANNAV
  • 4,875
  • 4
  • 32
  • 50
Kalpesh
  • 5,336
  • 26
  • 41
  • [sne addTarget:self action:@selector(btnItemListClicked:) forControlEvents:UIControlEventTouchDown]; – Steve Aug 20 '13 at 08:02
  • whatever you want , just write this code in your button action method . – Kalpesh Aug 20 '13 at 08:03
  • its returning null value – Steve Aug 20 '13 at 08:04
  • i have tested it, actually there is a problem in this case which is: its showing the name of that button which is made through xib file, its not showing the name of that button which is programmatically made. – Steve Aug 20 '13 at 08:29
  • 1
    i got the problem, actually its not that, what i have mentioned before, it works.. thanx :) – Steve Aug 20 '13 at 08:36