I am creating a button like this inside a method:
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 100, 50);
[btn setTitle:@"Hello, world!" forState:UIControlStateNormal];
[self.view addSubview:btn];
myButton = btn;
// I am saving the btn reference to this ivar declared on .h as UIButton.
At another point of the code I try to use myButton and it is always nil.
I have tried to retain btn after assigning it to myButton on the original method but myButton is always nil.
self.view is always there.
btn is never released.
Why myButton is nil?
I know I can create the button using alloc, but I am just trying to understand this.
thanks.
here you get an autorelease object of UIButton.
if want to access this button at other places so for this purpose,make it a property and use
or
creating by alloc and release it in dealloc .
Edit
in .h
and in .m
and release it in dealloc.