我有这个例子,我想让 my_Picture 成为一个实例变量,以便使用 removeFromView。有任何想法吗?我在尝试不同的方法时收到了各种警告和错误。先感谢您

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

   [super viewDidLoad];
}

最佳答案

要使其成为实例变量,您可以将值存储在类中而不是作为临时变量。您还将在您的类被销毁时释放它,而不是在将其添加为 subview 之后。

例如。

// 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];
}

关于objective-c - 如何在 Objective-C 中创建实例变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/622874/

10-15 08:14
查看更多