简体   繁体   中英

How to create an Instance Variable in Objective-C

I have this example, and I would like to make my_Picture an instance variable in order to use removeFromView. Any Ideas? I got all kinds of warnings and errors trying different approaches. Thank you in advance

- (void) viewDidLoad
{
   UIImageView *my_Picture = [[UIImageView alloc] initWithImage: myImageRef];
   [self.view addSubview:my_Picture];
   [my_Picture release];

   [super viewDidLoad];
}

To make it an instance variable you would store the value in your class instead of as a temporary variable. You will also release it when your class is destroyed instead of after adding it as a subview.

Eg

// header file (.h)
@interface MyController : UIViewController
{
  UIImageView* myPicture;
}
@end

// source file (.m)
- (void) viewDidLoad
{
   myPicture = [[UIImageView alloc] initWithImage: myImageRef];
   [self.view addSubview:myPicture];

   [super viewDidLoad];
}

- (void) dealloc
{
   [myPicture release];
   [super dealloc];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM