0

我是目标 c 的新手,我正在尝试设置图像,UIImageview同时推动viewcontroller 我检查了其他答案,我按照他们所说的方式做了同样的事情,但它对我不起作用下面是我的代码,我将在其中解释其他内容

- (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;

}

下面是我的detailviewcontroller

@interface DetailViewController : UIViewController{

    UIImageView *image;


}

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

它连接到 Xib,我已经合成了它

- (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>>

}

当我将它们发送NSString并设置在我的标签上但不适用于图像时,它工作得很好,它是如此之小,但现在我不知道该怎么做,请帮忙

4

3 回答 3

2

您不能这样做,因为在您尝试设置其图像视图的图像时,svController 的视图尚未加载(因此图像视图将为空)。

您需要将图像本身传递给您在 svController 中创建的属性,并在 svController 的 viewDidLoad 方法中设置图像。

于 2013-08-05T05:25:40.297 回答
2

听到是简单的方法,我正在展示您可以在详细视图控制器中设置图像的示例

在您的主视图控制器中

-(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
    }
}

并在您的详细视图控制器中

// .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会这样做:

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

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

就是这样....

于 2013-08-05T05:54:06.223 回答
1

设置你的UI object intializationafer pushing view controller

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

理由是viewDidLoadsvController不会现在calledloaded现在还imageview不会reference


编辑Better会这样做:

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

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

就是这样....

于 2013-08-05T05:25:26.790 回答