0

我是目标 C 的初学者。我想将图像从ViewControllerto传递SecondClass给以显示SecondClass已经在ViewController. 但是传输后的传输imageview显示值,按下按钮后null我无法到达。secondClass我的代码如下,

我的协议.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 "SecondClass.h"

#import "myProtocol.h"

(void)viewDidLoad

{

    [super viewDidLoad];

     imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"photo1.png"]];

     [view addSubview:imageView];

    NSLog(@"I am in VC.m");

}

-(UIImage *)transferImage{

    NSLog(@"I am in transferImage");

    return imageView.image;

}

- (IBAction)sendImage:(id)sender {

    SecondClass *secClass = [[SecondClass alloc]init]; 

    secClass.delegate=self;

    [secClass callTransfer];

    NSLog(@"I am in sender");

    [self.navigationController pushViewController:secClass animated:YES];

}

@end

第二类.h

#import "myProtocol.h"

#import "ViewController.h"

@interface SecondClass : UIViewController<myProtocol,UINavigationControllerDelegate>

{
    UIView *secondView;

    IBOutlet UIImageView *myImage;

    id <myProtocol> myDelegate;

}

@property (nonatomic,retain) UIImageView *myImage;

@property(nonatomic,assign) id delegate;

-(void)callTransfer;

@end

第二类.m

#import "SecondClass.h"

#import "ViewController.h"

#import "myProtocol.h"

@implementation SecondClass

@synthesize delegate,myImage;

- (void)viewDidLoad

{
    [super viewDidLoad];

  [secondView addSubview:myImage];

}

-(void)callTransfer

{
    myImage.image=[delegate performSelector:@selector(transferImage)];

    myImage.image=[UIImage imageNamed:@"photo1.png"];

    NSLog(@"%@",myImage.image);

    NSLog(@"I am in call transfer");

}

@end
4

3 回答 3

1

使用此代码并检查它

UIImage *img = [delegate performSelector:@selector(transferImage)]; 
NSLog(@"%@",img );
NSLog(@"I am in call transfer");
于 2012-08-13T09:09:56.633 回答
0

您在这里不需要委托,您将数据传递到新视图控制器的简单任务大大复杂化了。

SecondClass *secClass = [[SecondClass alloc] init]; 

secClass.myImage.image = imageView.image;

[self.navigationController pushViewController:secClass animated:YES];

更新

你提到这是为了练习 - 但它仍然是根本错误的。对于委托人来说,更正常的情况是发送图像,child -> parent而不是从parent -> child父代拥有子代,并且可以在没有设置详细协议的情况下调用方法。

#import "ChildVC.h"

@interface ParentVC : UIViewController <ChildDelegate>

@end

@implementation ParentVC

- (void)pushViewController;
{
    ChildVC *childVC = [[ChildVC alloc] init];
    childVC.delegate = self;
    [self.navigationController pushViewController:childVC animated:YES];
}

- (void)childViewController:(ChildVC *)childViewController didFinishWithImage:(UIImage *)image;
{
    // do something with the image passed back
}

@end

然后孩子看起来像

@class ChildVC;

@protocol ChildDelegate <NSObject>

- (void)childViewController:(ChildVC *)childViewController didFinishWithImage:(UIImage *)image;

@end

@interface ChildVC : UIViewController

@property (nonatomic, weak) id<ChildDelegate> delegate;
@property (nonatomic, strong) UIImage *image;

@end

@implementation ChildVC

- (void)whenSomethingInterestingHappens;
{
    if ([self.delegate respondsToSelector:@selector(childViewController:didFinishWithImage:)]) {
        [self.delegate childViewController:self didFinishWithImage:self.image];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

@end
于 2012-08-13T09:27:56.747 回答
0
how to pass two view controller image for delegate method :



//step 1: create for delegate method :


@protocol DrawViewDelegate
-(void)didReceiveDrawImage:(UIImage *)DrawImage;
 @end

step2: delegate pass for your first view:

@interface EditorViewController :
 UIViewController< DrawViewDelegate >

// step3: 你必须是视图控制器通过视图时间

like : crop.delegat=self;

// step:4 传递第二个视图控制器协议

@protocol DrawViewDelegate;    

// step:5 使用方法set image pass other view:

[_delegate didReceiveDrawImage:savedimage];



//step: 6 get second view image to first view



-(void)didReceiveDrawImage:(UIImage *)DrawImage
于 2018-04-04T10:23:29.940 回答