简体   繁体   English

UIImageView不显示图片

[英]UIImageView doesn't show picture

I have an UIImageView and I would like to paste a picture in it, but which picture, it depends from the navigationItem.title . 我有一个UIImageView ,我想在其中粘贴图片,但是哪张图片取决于navigationItem.title

So here is the code: 所以这是代码:

- (void)viewDidLoad
{
    if(self.navigationItem.title == @"Кружка")
    {
        giftImage.image = [UIImage imageNamed:@"cup2.png"];
    }
    [super viewDidLoad];
}

No errors, just doesn't work. 没有错误,就是行不通。 What's wrong? 怎么了?

I changed my code to this: 我将代码更改为此:

- (void)viewDidLoad
{

    [super viewDidLoad];
    if(self.navigationItem.title == @"Кружка")
    {
        giftImage.image = [UIImage imageNamed:@"cup1"];

    }


    [self.view addSubview:giftImage];
    // Do any additional setup after loading the view from its nib.
}
if(self.navigationItem.title == @"Кружка")

Don't compare strings like this. 不要比较这样的字符串。 This is checking for pointer equality, not string equality. 这是在检查指针是否相等,而不是字符串是否相等。 Use 采用

if([self.navigationItem.title isEqualToString: @"Кружка"])

Your image is not getting set because the two pointers are not equal. 您的图片未设置,因为两个指针不相等。

Debug using breakpoints and check whether the If condition is returning true or false... If it returns False, then try again by replacing self.navigationItem.title with self.title 使用断点进行调试,并检查If条件返回的是true还是false ...如果返回False,则通过将self.navigationItem.title替换为self.title来重试。

And if it returns true then : 如果返回true,则:

1. Recheck cup2.png spellings (Note that image extension is mandatory)
2. Image names are case sensitive. so recheck letter case in spellings
3. This image must present in xcode resources.

I think the convention is to call [super viewDidLoad] in the beginning of viewDidLoad. 我认为惯例是在viewDidLoad的开头调用[super viewDidLoad]。

There are several checkpoints 有几个检查站

  1. Debug if it is coming inside the if block. 调试是否在if块中。
  2. Check if the giftImage is added as a subview. 检查giftImage是否添加为子视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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