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

时间:2013-10-14 17:41:32

标签: ios delegates uiimageview

经过多次尝试,我没有成功理解错误是什么,为什么图像没有出现......

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];
    [secondView addSubview:myImage];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [myImage setImage:[UIImage imageNamed:_imageName]];
}

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

1 个答案:

答案 0 :(得分:0)

首先,如果你不打算重用一个类,那么我不建议使用委托,因为还有其他方法可以在两个类之间传递数据。

您似乎正在尝试将UIImage从第一个ViewController传递到第二个ViewController。 所以让你的生活更轻松:

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

并且瞧,您的第二个View控制器,在transferImage变量中有UIImage指针......