Objective-C keep track of my view in subviews array
up vote
0
down vote
favorite
I have a question about memory management.
For example I have an iPhone application that uses multiply programmatically created views.
for example programmatically generated buttons.
UIButton *myButton=[UIButton alloc] initWithFrame:...; //etc
then, normally we add this button to subviews array:
[self.view addSubview:myButton];
then we releasing button.
[myButton release]
When I need to remove this button how can I keep track on this button in subviews array?
I know I can do this using tag property but I think exists another way to keep connection with it.
iphone objective-c uiview subview addsubview
add a comment |
up vote
0
down vote
favorite
I have a question about memory management.
For example I have an iPhone application that uses multiply programmatically created views.
for example programmatically generated buttons.
UIButton *myButton=[UIButton alloc] initWithFrame:...; //etc
then, normally we add this button to subviews array:
[self.view addSubview:myButton];
then we releasing button.
[myButton release]
When I need to remove this button how can I keep track on this button in subviews array?
I know I can do this using tag property but I think exists another way to keep connection with it.
iphone objective-c uiview subview addsubview
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a question about memory management.
For example I have an iPhone application that uses multiply programmatically created views.
for example programmatically generated buttons.
UIButton *myButton=[UIButton alloc] initWithFrame:...; //etc
then, normally we add this button to subviews array:
[self.view addSubview:myButton];
then we releasing button.
[myButton release]
When I need to remove this button how can I keep track on this button in subviews array?
I know I can do this using tag property but I think exists another way to keep connection with it.
iphone objective-c uiview subview addsubview
I have a question about memory management.
For example I have an iPhone application that uses multiply programmatically created views.
for example programmatically generated buttons.
UIButton *myButton=[UIButton alloc] initWithFrame:...; //etc
then, normally we add this button to subviews array:
[self.view addSubview:myButton];
then we releasing button.
[myButton release]
When I need to remove this button how can I keep track on this button in subviews array?
I know I can do this using tag property but I think exists another way to keep connection with it.
iphone objective-c uiview subview addsubview
iphone objective-c uiview subview addsubview
edited Nov 11 at 6:17
Cœur
17k9102140
17k9102140
asked Nov 25 '11 at 13:53
Oleg
67041434
67041434
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
You can simply assign it to an instance variable:
UIButton *myButton = ...;
[self.view addSubView:myButton];
myInstanceVariable = myButton;
[myButton release];
You just need to be careful: as soon as you do something like [myInstanceVariable removeFromSuperview];
it might get deallocated immediately (if you haven't retained it) and it would then point to invalid memory.
Thank you for your help
– Oleg
Nov 25 '11 at 14:09
add a comment |
up vote
0
down vote
You can try to declare somewhere a retain property of UIButton*
type, that can be assigned with pointer value to your button instance:
@interface myclass
@property (retain, nonatomic) UIButton *savedButton;
@end
@implementation myclass
@synthesize savedButton;
- (void) someMethod...
{
...
UIButton *myButton=[UIButton alloc] initWithFrame:...;
[self.view addSubview:myButton];
self.savedButton = myButton;
[myButton release];
...
}
...
@end
Thank you for your help
– Oleg
Nov 25 '11 at 14:09
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You can simply assign it to an instance variable:
UIButton *myButton = ...;
[self.view addSubView:myButton];
myInstanceVariable = myButton;
[myButton release];
You just need to be careful: as soon as you do something like [myInstanceVariable removeFromSuperview];
it might get deallocated immediately (if you haven't retained it) and it would then point to invalid memory.
Thank you for your help
– Oleg
Nov 25 '11 at 14:09
add a comment |
up vote
0
down vote
accepted
You can simply assign it to an instance variable:
UIButton *myButton = ...;
[self.view addSubView:myButton];
myInstanceVariable = myButton;
[myButton release];
You just need to be careful: as soon as you do something like [myInstanceVariable removeFromSuperview];
it might get deallocated immediately (if you haven't retained it) and it would then point to invalid memory.
Thank you for your help
– Oleg
Nov 25 '11 at 14:09
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You can simply assign it to an instance variable:
UIButton *myButton = ...;
[self.view addSubView:myButton];
myInstanceVariable = myButton;
[myButton release];
You just need to be careful: as soon as you do something like [myInstanceVariable removeFromSuperview];
it might get deallocated immediately (if you haven't retained it) and it would then point to invalid memory.
You can simply assign it to an instance variable:
UIButton *myButton = ...;
[self.view addSubView:myButton];
myInstanceVariable = myButton;
[myButton release];
You just need to be careful: as soon as you do something like [myInstanceVariable removeFromSuperview];
it might get deallocated immediately (if you haven't retained it) and it would then point to invalid memory.
answered Nov 25 '11 at 13:57
DarkDust
75.8k12154195
75.8k12154195
Thank you for your help
– Oleg
Nov 25 '11 at 14:09
add a comment |
Thank you for your help
– Oleg
Nov 25 '11 at 14:09
Thank you for your help
– Oleg
Nov 25 '11 at 14:09
Thank you for your help
– Oleg
Nov 25 '11 at 14:09
add a comment |
up vote
0
down vote
You can try to declare somewhere a retain property of UIButton*
type, that can be assigned with pointer value to your button instance:
@interface myclass
@property (retain, nonatomic) UIButton *savedButton;
@end
@implementation myclass
@synthesize savedButton;
- (void) someMethod...
{
...
UIButton *myButton=[UIButton alloc] initWithFrame:...;
[self.view addSubview:myButton];
self.savedButton = myButton;
[myButton release];
...
}
...
@end
Thank you for your help
– Oleg
Nov 25 '11 at 14:09
add a comment |
up vote
0
down vote
You can try to declare somewhere a retain property of UIButton*
type, that can be assigned with pointer value to your button instance:
@interface myclass
@property (retain, nonatomic) UIButton *savedButton;
@end
@implementation myclass
@synthesize savedButton;
- (void) someMethod...
{
...
UIButton *myButton=[UIButton alloc] initWithFrame:...;
[self.view addSubview:myButton];
self.savedButton = myButton;
[myButton release];
...
}
...
@end
Thank you for your help
– Oleg
Nov 25 '11 at 14:09
add a comment |
up vote
0
down vote
up vote
0
down vote
You can try to declare somewhere a retain property of UIButton*
type, that can be assigned with pointer value to your button instance:
@interface myclass
@property (retain, nonatomic) UIButton *savedButton;
@end
@implementation myclass
@synthesize savedButton;
- (void) someMethod...
{
...
UIButton *myButton=[UIButton alloc] initWithFrame:...;
[self.view addSubview:myButton];
self.savedButton = myButton;
[myButton release];
...
}
...
@end
You can try to declare somewhere a retain property of UIButton*
type, that can be assigned with pointer value to your button instance:
@interface myclass
@property (retain, nonatomic) UIButton *savedButton;
@end
@implementation myclass
@synthesize savedButton;
- (void) someMethod...
{
...
UIButton *myButton=[UIButton alloc] initWithFrame:...;
[self.view addSubview:myButton];
self.savedButton = myButton;
[myButton release];
...
}
...
@end
answered Nov 25 '11 at 14:00
Denis
5,83012225
5,83012225
Thank you for your help
– Oleg
Nov 25 '11 at 14:09
add a comment |
Thank you for your help
– Oleg
Nov 25 '11 at 14:09
Thank you for your help
– Oleg
Nov 25 '11 at 14:09
Thank you for your help
– Oleg
Nov 25 '11 at 14:09
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8270116%2fobjective-c-keep-track-of-my-view-in-subviews-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown