在SecondViewController中单击按钮时,在ViewController中显示图像 [英] Show image in ViewController when button is clicked in SecondViewController

查看:71
本文介绍了在SecondViewController中单击按钮时,在ViewController中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过在另一个 UIViewController中点击 UIButton 来显示 UIIimage ?我想添加相同的 UIButton 命令将图像添加到SecondViewController。原谅我的糟糕问题。



myProtocol.h

  #import< Foundation / Foundation.h> 

@protocol myProtocol< NSObject>

- (UIImage *)transferImage;

@end

ViewController.h

  #importSecondClass.h

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

@property(非原子,保留)UIImageView * imageView;

- (IBAction)sendImage:(id)sender;

@end

ViewController.m

  #importViewController.h
#importSecondViewController.h
#importmyProtocol。 h

@interface ViewController()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];
_imageView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:@VoodooVibe@2x.png]];
[查看addSubview:_imageView];
NSLog(@我在VC.m);
}

- (UIImage *)transferImage {

NSLog(@我在transferImage中);
return _imageView.image;
}

- (IBAction)sendImage:(id)sender {

SecondViewController * secClass = [[SecondViewController alloc] init];
secClass.delegate = self;
[secClass callTransfer];
NSLog(@我是发件人);
[self.navigationController pushViewController:secClass animated:YES];
}

- (无效)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];
//处理可以重新创建的任何资源。
}

@end

SecondViewController.h

  #import< UIKit / UIKit.h> 
#importmyProtocol.h
#importViewController.h

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

@property(非原子,保留)UIImageView * myImage;
@property(nonatomic,assign)id delegate;

- (void)callTransfer;

@end

SecondViewController.m

  #importSecondViewController.h
#importViewController.h
#importmyProtocol。 h

@interface SecondViewController()

@end

@implementation SecondViewController

@synthesize delegate,myImage;

- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self){
//自定义初始化
}
返回self;
}

- (void)viewDidLoad {

[super viewDidLoad];
//加载视图后进行任何其他设置。
[secondView addSubview:myImage];
}

- (void)callTransfer {

myImage.image = [delegate performSelector:@selector(transferImage)];
myImage.image = [UIImage imageNamed:@VoodooVibe@2x.png];
NSLog(@%@,myImage.image);
NSLog(@我在呼叫转移);
}

- (无效)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];
//处理可以重新创建的任何资源。
}

@end


解决方案

通常应该使用代理,如果你的 UIViewController 中包含一个类,它将执行某些操作并在特定进程完成时通过委托方法通知您。但在这种情况下,您将设置 UIImage 两次。一次通过委托,再通过以编程方式设置 UIImage



你不应该做任何事情,比如调用初始化第二个 UIImage 的方法> UIViewController 来自外部。只需调用 viewDidLoad 中的所有内容,您就不必关心它,因为 UIViewController 会自行处理它。 / p>

您只需在 SecondViewController 中插入 UIImageView 并将其连接到您的头文件。然后你可以在m里面访问它。文件。我遇到的问题是我第一次使用 jpg 而不是 png ,但更改后缀后一切正常。



ViewController.m

   - (IBAction)sendImage:(id)sender {

SecondViewController * secClass = [[SecondViewController alloc] init];
[secClass setImageName:@pic.png];
[self.navigationController pushViewController:secClass
animated:YES];

}

SecondViewController.h

  @interface SecondViewController:UIViewController 

@property(strong,nonatomic)NSString * imageName;
@property(弱,非原子)IBOutlet UIImageView * myImage;

@end

SecondViewController.m (只有这两行)

   - (void)viewWillAppear:(BOOL)animated {

[super viewWillAppear:animated];
[_myImage setImage:[UIImage imageNamed:_imageName]];
}


How to show an UIIimage by clicking on an UIButton inside another UIViewController? I would like to add to the same UIButton the command to add an image to the SecondViewController. Excused my poor question.

myProtocol.h

#import <Foundation/Foundation.h>

@protocol myProtocol <NSObject>

-(UIImage *)transferImage;

@end

ViewController.h

#import "SecondClass.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:@"VoodooVibe@2x.png"]];
    [view addSubview:_imageView];
    NSLog(@"I am in VC.m");
}

-(UIImage *)transferImage{

    NSLog(@"I am in transferImage");
    return _imageView.image;
}

- (IBAction)sendImage:(id)sender {

    SecondViewController *secClass = [[SecondViewController alloc]init];
    secClass.delegate=self;    
    [secClass callTransfer];
    NSLog(@"I am in sender");
    [self.navigationController pushViewController:secClass animated:YES];
}

- (void)didReceiveMemoryWarning{

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@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,assign) id delegate;

-(void)callTransfer;

@end

SecondViewController.m

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

@interface SecondViewController ()

@end

@implementation SecondViewController

@synthesize delegate,myImage;

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

- (void)viewDidLoad{

    [super viewDidLoad];
// Do any additional setup after loading the view.
    [secondView addSubview:myImage]; 
}

-(void)callTransfer{

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

- (void)didReceiveMemoryWarning{

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

解决方案

Delegates should normally be used, if you you have a class included inside your UIViewController which will do something and notify you with the delegate method when the specific process has finished. But in this case you would set an UIImage two times. Once by your delegate and a second time by setting an UIImage programmatically.

You shouldn't do anything like calling a method for initializing the UIImage of the second UIViewController from outside. Just call everything inside viewDidLoad and you don't have to care about it, because the UIViewController handles it itself.

You just have to insert an UIImageView inside your SecondViewController and connect it to your header file. then you can access it inside the m. file. I had the problem that I first used a jpg instead of a png, but after changing the suffix everything worked fine.

ViewController.m

- (IBAction)sendImage:(id)sender {

    SecondViewController *secClass = [[SecondViewController alloc] init];
    [secClass setImageName:@"pic.png"];
    [self.navigationController pushViewController:secClass
                                         animated:YES];

}

SecondViewController.h

@interface SecondViewController : UIViewController

@property (strong, nonatomic)NSString *imageName;
@property (weak, nonatomic) IBOutlet UIImageView *myImage;

@end

SecondViewController.m (There are just these two lines)

- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    [_myImage setImage:[UIImage imageNamed:_imageName]];
}

这篇关于在SecondViewController中单击按钮时,在ViewController中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆