Question

I am new on objective c and I am trying to set image of UIImageview while pushing viewcontroller I checked other answer and I did the same way they said it but its not working for me below is my code and i will explain other things in it

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    DetailViewController *svController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]];



    UIImage *image = [UIImage imageNamed:@"default.png"];
    UIImageView* imageview = [[UIImageView alloc] initWithImage:image];


    id obj = [[mainArray valueForKey:@"image"] objectAtIndex:[indexPath row]];
    if ([obj isKindOfClass:[NSString class]])
    {




          NSLog(@" image%@",image); //print this image<UIImage: 0xaa70700>

         NSLog(@" image%@",svController.image); //print this  image(null)
        [[svController image] setImage: image];

         NSLog(@" image%@",svController.image);   //print this  image(null)


    }


    else     {
         NSLog(@" image%@",svController.image);

                  [[svController image] setImage: [[mainArray valueForKey:@"image"] objectAtIndex:[indexPath row]]];

              NSLog(@" image%@",svController.image);


    }


    [self.navigationController pushViewController:svController animated:YES];

    [svController release];
    svController = nil;

}

below is my detailviewcontroller

@interface DetailViewController : UIViewController{

    UIImageView *image;


}

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

its connected to Xib and i have synthesized it

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.


    name.text = nameString;
    email.text = emailString;

     NSLog(@"%@",image); // print this <UIImageView: 0x9d73950; frame = (27 35; 204 181); autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x9db4050>>

}

its working perfectly when i send NSString and set them on my lable but not working for image it its so small thing but now i dont know what to do please help

Was it helpful?

Solution

hear is the simple way, i am showing the example that u can set image in detail view controller

in your master view controller

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
    if(indexPath.row == 0)//for 1st row
    {
       DetailViewController *detController = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
       detController.detailImage = [UIImage imageNamed:@"abc.png"];//setting the property for image
       [self.navigationController pushViewController:detController animated:YES];
       [detController release];//im doing without ARC
    }
    else if (indexPath.row == 1)
    {
       DetailViewController *detController = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
       detController.detailImage = [UIImage imageNamed:@"123.png"];
       [self.navigationController pushViewController:detController animated:YES];
       [detController release];//im doing without ARC
    }
}

and in your detail view controller

// .h file
@interface DetailViewController : UIViewController
{

}
@property (retain, nonatomic) IBOutlet UIImageView *ImageView;
@property (nonatomic, retain)UIImage * detailImage;//set a property for image that u are passing from master viewcontroller 
@end

//in .m file

@implementation DetailViewController
@synthesize detailImage;  //synthesise it

// and in viewDidload method 
- (void)viewDidLoad
{
   [super viewDidLoad];

  //set your passed image to image view
  self.ImageView.image = self.detailImage;   
}

Better would be just do like this:

[self.navigationController pushViewController:svController animated:YES];
if(yourImage) //got UIImage reference then only set it
   svController.yourImgView.image = yourImageHere

.................
<<Another code here>>
..............

Thats it....

OTHER TIPS

You can't do this, because svController's view has not been loaded at the time you try to set the image of its image view (so the image view will be null).

You need to pass the image itself to a property you create in svController, and set the image in svController's viewDidLoad method.

Set your UI object intialization afer pushing view controller.

[self.navigationController pushViewController:svController animated:YES];
//set image here

Reason is viewDidLoad of svController will not be called and loaded yet and imageview will not have reference yet.


EDIT : Better would be just do like this:

[self.navigationController pushViewController:svController animated:YES];
if(yourImage) //got UIImage reference then only set it
   svController.yourImgView.image = yourImageHere

.................
<<Another code here>>
..............

Thats it....

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top