0
- (void)loadView 
{
    [super loadView];
    arrayOfImages = [[NSMutableArray alloc]initWithObjects:@"11.jpg",@"22.jpg",@"33.jpg", nil];       
    UIImageView *awesomeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [awesomeView setImage :[UIImage imageNamed:[arrayOfImages objectAtIndex:0]]];
    awesomeView.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:awesomeView]; 
    NSLog(@"%@",[arrayOfImages objectAtIndex:0]);
}

When I put the NSMutableArray in -(void)viewDidLoad, UIImageView displays nothing and NSLog shows NULL. Why is that?

ps. NSMutableArray worked perfectly in -(void)loadView. I've declared NSMutableArray *arrayOfImage in @interface .h file

Jenny
  • 351
  • 3
  • 19
  • Is your image view nil? – BergQuester Aug 18 '14 at 03:50
  • How to check if my image view is nil :p? – Jenny Aug 18 '14 at 03:55
  • Either `NSLog()` it or use `po` in the debugger when you are stopped at a breakpoint you set in the method. – BergQuester Aug 18 '14 at 03:56
  • NSLog(@"%@",awesomeView); shows that my imageView is not nil :) – Jenny Aug 18 '14 at 03:58
  • 4
    1) Never call `super loadView` in the `loadView` method - see the docs for `UIViewController loadView`. 2) You need to assign `self.view` to a view you create in `loadView`. 3) As written, the code you have in `loadView` should actually be in `viewDidLoad`. You don't seem to need to implement `loadView`. – rmaddy Aug 18 '14 at 04:22
  • @maddy All true, but nothing would explain the "NULL" output. – Eiko Aug 18 '14 at 06:46

2 Answers2

0

The only way that the given could would output "NULL" in this situation is that arrayOfImages is NULL by itself.

This is only possible if arrayOfImages is declared as a weak variable.

But as @maddy pointed out, your code is all wrong: Don't call super, but assign self.view. Or use viedDidLoad instead (probably what you want here).

Eiko
  • 25,601
  • 15
  • 56
  • 71
-1
-(void)loadView 
{
   [super loadView];
    arrayOfImages = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"11.jpg"],[UIImage imageNamed:@"22.jpg"],[UIImage imageNamed:@"33.jpg"], nil];       
    UIImageView *awesomeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
[awesomeView setImage :[UIImage imageNamed:[arrayOfImages objectAtIndex:0]]];
awesomeView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:awesomeView]; 
NSLog(@"%@",[arrayOfImages objectAtIndex:0]);

This will work but you will only get to see 1 picture. ie the picture in the 0 index of the array.

If you want to show all then you got to apply a loop.

Saheb Roy
  • 5,899
  • 3
  • 23
  • 35
  • What is different in the code you post and the code the OP posted? – trojanfoe Aug 18 '14 at 06:53
  • before giving a downvote please check the answer properly. OP posted the code along with the names of the Image . So the Array only contains strings NOT images. [UIImage imageNamed:@"myImage.extension"]; this was my change. – Saheb Roy Aug 20 '14 at 04:51