【问题标题】:iOS Updating Property of an ObjectiOS 更新对象的属性
【发布时间】:2013-07-31 10:35:54
【问题描述】:

我是 iOS 开发新手,希望得到一些指导。

我有一个 ViewController,它有一个我想定期替换的属性:

@property (strong, nonatomic) IBOutlet UIImageView *image;

现在在我更新该属性的另一个类中,我首先分配并初始化 ViewController,然后相应地更新该属性。但是,当我用另一个图像替换该图像时,它只使用第一个图像。但是,如果我用 alloc init 实例化另一个 ViewController 一切正常。

所以我的问题是,通过创建我的 ViewController 的新实例来更新正确/最佳实践方式 - 内存效率明智是最佳实践吗?

谢谢。

【问题讨论】:

  • 检查viewcontroller是否存在,好像(viewController) viewController = nil;然后分配它。

标签: objective-c object


【解决方案1】:

听起来您肯定想更改图像,而不是替换整个视图控制器。

您能在更改图像的位置显示代码吗?另外,请指定您是要更改图像视图还是其中的图像。 (图像视图是在屏幕上显示实际图像的内容。我建议您将属性命名为imageView,以避免混淆。)

这里有两个例子。我假设有一个变量“theViewController”包含您的视图控制器的引用。

替换图像视图:

// Create a new image view with a new image inside it
theViewController.image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img1"]];

更换图片:

// Put a new image into the existing image view
theViewController.image.image = [UIImage imageNamed:@"img1"];

最后一个示例演示了为什么属性名称imageView 最好改为image:现在,看起来您正在更改“图像上的图像”。但实际上,您正在更改 imageView 上的图像。

【讨论】:

    猜你喜欢
    • 2013-01-18
    • 1970-01-01
    • 2020-07-16
    • 2012-03-16
    • 2016-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多