【问题标题】:How to change UiImageView in xib file programmatically如何以编程方式更改 xib 文件中的 UiImageView
【发布时间】:2015-12-21 14:17:45
【问题描述】:

我试图在 xib 文件中更改 UIImageView,但它似乎不起作用。我有带有 UIImageView 的 xib 文件。我将我的UIImageView 链接到我的 xib 文件。在我的主要ViewController 中,我调用了一个函数,该函数将更改 xib 文件中的UIImageView。下面是我更改 xib 文件中图像的函数

UIView *view = [[[NSBundle bundleForClass:[self class]]loadNibNamed:@"View" owner:self options:nil] firstObject];
[self addSubview:view];
view.frame = self.bounds;
_imageTest.image = [UIImage imageNamed:@"0.png"];

我是不是做错了什么。我在堆栈溢出中搜索,但似乎我找不到我要找的东西。

【问题讨论】:

  • 应该是[NSBundle mainBundle]
  • 我更改为 [NSBundle mainBundle] 但仍然无法正常工作..有什么想法吗?
  • 可能是imageTest 的出口未在 xib 的文件所有者中设置?
  • 不,我已经设置为 xib 的文件所有者。当我尝试使用此代码时,我的 _testImage 似乎为零。 UIImage *image = [UIImage imageNamed:@"0.png"]; [_testImage setImage:image];
  • 你为什么使用[[NSBundle bundleForClass:[self class]]

标签: ios objective-c iphone xcode uiimageview


【解决方案1】:

我不确定你是想用其他UI<control 更改UIImageView 的图像还是控件本身。

要更改UIImage,您只需要在viewController 类中设置一个插座,然后将图像设置为它。比如:

[self.yourImageView setImage:[UIImage imageNamed:@"0.png"]]; //and make sure the 0.png file exist in the application bundle.

如果您对更改控件本身感兴趣,那么最好不要同时创建 UIImage。在运行时创建 UIImage 和其他控件,并将控件添加到视图。

【讨论】:

  • 我的 UIImageView 在 xib 文件中,因为我想重用 xib。这就是为什么我想使用函数来更改我的 xib 文件中的 UIImageView
  • 不是改变而是设置。我想从 xib 文件中设置我的 UIImageView。
【解决方案2】:

尝试像这样加载您的 xib 视图:

UIView *view = [[[NSBundle mainBundle] loadNibNamed:@"yourView" owner:self options:nil] objectAtIndex:0];

并在设置图像的位置放置断点,确保您的 _imageTest 不为零。

【讨论】:

  • 我按照您的要求做了,但我的 UIImageView 似乎为零。为什么会这样?
【解决方案3】:

没关系,伙计们。我已经可以让它工作了。这似乎是我的 UIImageView 问题。在我删除并添加回来之后,一切都已经正常工作了。谢谢大家的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    相关资源
    最近更新 更多