繁体   English   中英

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

[英]How to show image in SecondViewController when button is clicked in FirstViewController

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

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

看来您没有在将myImage子视图添加到SecondViewController的viewDidLoad方法之前对其进行初始化。

尝试删除:

[secondView addSubview:myImage];

从viewDidLoad中添加以下内容到viewWillAppear中:

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

之前:

[secondView addSubview:myImage];

这样做可能会有更好的方法,但是这可能使您朝正确的方向前进。

用这个:

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

我假设您的界面中有一个名为myImage的变量(无论是.h还是.m),因此当您创建myImage的本地实例(UIImageView * myImage)时,XCode会感到困惑。 由于它是最新声明的(无引用,对不起),它使用的是本地声明,但是它知道存在一个使用相同名称的实例变量,该实例变量无法访问。 相反,您想通过从方法中删除UIImageView *直接保存到实例变量

希望这有道理!

马特

[secondView addSubview:myImage];

错误:“ myImage”隐藏阻力变量的本地声明。

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM