如何在FirstViewController中单击按钮时在SecondViewController中显示图像

时间:2013-10-15 21:16:55

标签: ios uiview uibutton

经过多次尝试后,我没有成功理解是错误以及为什么图像没有出现......这是我的代码。

myProtocol.h

#import <Foundation/Foundation.h>

@protocol myProtocol <NSObject>

-(UIImage *)transferImage;

@end

ViewController.h

#import "SecondViewController.h"

@interface ViewController : UIViewController<myProtocol, UINavigationControllerDelegate>
{
    UIView *view;
} 

@property (nonatomic,retain) UIImageView *imageView;

- (IBAction)sendImage:(id)sender; 

@end

ViewController.m

#import "ViewController.h" 
#import "SecondViewController.h" 
#import "myProtocol.h"

@interface ViewController () 
@end 

@implementation ViewController

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"VoodooVibeThumb@2x.png"]];
    [view addSubview:_imageView];
    NSLog(@"VoodooVibeThumb@2x.png");
}

-(UIImage *)transferImage
{
    NSLog(@"VoodooVibeThumb@2x.png"); 
    return _imageView.image;
} 

- (IBAction)sendImage:(id)sender 
{    
    SecondViewController *secClass = [[SecondViewController alloc] init];
    [secClass setImageName:@"VoodooVibeThumb@2x.png"];
    [self.navigationController pushViewController:secClass animated:YES];
}

- (void)didReceiveMemoryWarning 
{
    [super didReceiveMemoryWarning];
} 

@end 

SecondViewController.h

#import <UIKit/UIKit.h> 
#import "myProtocol.h" 
#import "ViewController.h"

@interface SecondViewController: UIViewController <myProtocol, UINavigationControllerDelegate>
{
    UIView *secondView;
    IBOutlet UIImageView *myImage;
    id <myProtocol> myDelegate;
}

@property (nonatomic,retain) UIImageView *myImage;
@property (nonatomic,retain) UIImage *transferImage;
@property(nonatomic,assign) id delegate;
@property (strong, nonatomic)NSString *imageName;

-(void)callTransfer;

@end

SecondViewController.m

#import "SecondViewController.h"
#import "ViewController.h"
#import "myProtocol.h" 

@interface SecondViewController ()
@end 

@implementation SecondViewController

@synthesize delegate, myImage, transferImage;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[myImage setImage:[UIImage imageNamed:_imageName]];
UIImageView * myImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:_imageName]];
[secondView addSubview:myImage];
}

-(void)callTransfer
{
    myImage.image=[delegate performSelector:@selector(transferImage)];
    myImage.image=[UIImage imageNamed:@"VoodooVibeThumb@2x.png"];
    NSLog(@"%@",myImage.image);
    NSLog(@"I am in call transfer");
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end

3 个答案:

答案 0 :(得分:1)

在将它添加到SecondViewController的viewDidLoad方法中之前,您似乎没有初始化myImage子视图。

尝试删除:

[secondView addSubview:myImage];
从viewDidLoad

,并将以下内容添加到viewWillAppear:

UIImageView * myImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:_imageName]];

就在之前:

[secondView addSubview:myImage];

可能有更好的方法可以做到这一点,但这可能会让你朝着正确的方向前进。

答案 1 :(得分:1)

使用此:

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    myImage = [[UIImageView alloc] initWithImage[UIImage imageNamed:_imageName]];
    [secondView addSubview:myImage];
}

我认为你的界面中有一个变量(在.h或.m中),名为myImage,所以当你创建myImage的本地实例(UIImageView * myImage)时,XCode会变得混乱。因为它被声明为最新的(没有引用,抱歉)它正在使用本地声明,但它知道有一个使用相同名称的实例变量无法访问。您希望通过从方法中删除UIImageView *来直接保存到实例变量

希望这是有道理的!

马特

答案 2 :(得分:0)

[secondView addSubview:myImage];

错误:'myImage'隐藏变量的本地声明。

enter image description here