I have a button named by
UIButton *button1;
how can i save 'button1' in string? or am i able to save it or not?
I have a button named by
UIButton *button1;
how can i save 'button1' in string? or am i able to save it or not?
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.
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
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.
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;
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