委托方法不将数据传递给第二个视图控制器

时间:2011-12-11 14:54:35

标签: objective-c model-view-controller delegates protocols viewcontroller

您好我的代表没有将图像和一些字符串传递给我的第二个导航控制器。谁能看到什么错了?我创建了ImageAndTextDelegate.h,PhotoEditViewController.h / .m和PreviewFrameViewController.h / .m。基本上在选择一些图像并在photoedit中键入一些文本之后。我想使用我的委托方法将数据发送到预览框架。

//ImageAndTextDelegate.h
@protocol ImageAndTextDelegate

-(void)passImage:(UIImage*)image;
-(void)passMainText:(NSString*)maintext withSubText:(NSString*)subtext;

@end
  

// PhotoEditViewController.h       #import" ImageAndTextDelegate.h"

@interface PhotoEditViewController : UIViewController<UINavigationControllerDelegate,
     

UIImagePickerControllerDelegate&GT; {           IBOutlet UITextField * mainText;           IBOutlet UITextField * subText;           IBOutlet UIImageView * imageView;           IBOutlet UIButton * previewPictureButton;       }

@property (nonatomic,retain) UITextField *mainText;
@property (nonatomic,retain) UITextField *subText;
@property (nonatomic,retain) UIImageView *imageView;
@property (nonatomic,retain) UIButton *previewPictureButton;
@property (nonatomic,assign) IBOutlet id<ImageAndTextDelegate> delegate;

-(IBAction)previewPicture:(id)sender;

@end
//PhotoEditViewController.m
         #import "PhotoEditViewController.h"
         #import "PreviewFrameViewController.h"

         @implementation PhotoEditViewController
         @synthesize delegate;
         -(IBAction)previewPicture:(id)sender{
             PreviewFrameViewController* prvc = [[PreviewFrameViewController alloc]init];
             [delegate passImage:imageView.image];
             [delegate passMainText:mainText.text withSubText:subText.text];
             [self.navigationController pushViewController:prvc animated:YES];
         }
         - (void)viewDidLoad
         {
             [super viewDidLoad];
             // Do any additional setup after loading the view from its nib. 
         }
//PreviewFrameViewController.h
#import <UIKit/UIKit.h>
#import "ImageAndTextDelegate.h"

@interface PreviewFrameViewController : UIViewController<ImageAndTextDelegate>{
    IBOutlet UIImageView *myImage;
    IBOutlet UILabel *main;
    IBOutlet UILabel *sub;
}

@property (nonatomic,retain) UIImageView *myImage;
@property (nonatomic,retain) UILabel *main;
@property (nonatomic,retain) UILabel *sub;

@end
//PreviewFrameViewController.m
#import "PreviewFrameViewController.h"
#import "PhotoEditViewController.h"

@implementation PreviewFrameViewController

@synthesize myImage,main,sub;

-(void)passImage:(UIImage *)image{
    myImage.image = image;
}

-(void)passMainText:(NSString *)maintext withSubText:(NSString *)subtext{
    main.text = maintext;
    sub.text = subtext;
}
-(IBAction)goBack:(id)sender{
    [self dismissModalViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    PhotoEditViewController* pvc = [[PhotoEditViewController alloc]init];
    pvc.delegate = self;
}

1 个答案:

答案 0 :(得分:0)

所以你的PhotoEditViewController正在创建和呈现一个PreviewFrameViewController。

然后在其viewDidLoad中,您的PreviewFrameViewController正在创建自己的 PhotoEditViewController并将该实例的委托设置为自己。

所以你现在有了两个 PhotoEditViewControllers,而第一个仍然没有委托。 (这导致了为什么你需要在这里开始委托模式的问题?)

做你想要的最直接的方法就是这个(在你的PhotoEditViewController中):

-(IBAction)previewPicture:(id)sender{

    PreviewFrameViewController* prvc = [[PreviewFrameViewController alloc]init];
    [prvc passImage:imageView.image];
    [prvc passMainText:mainText.text withSubText:subText.text];
    [self.navigationController pushViewController:prvc animated:YES];

    // You could have set self.delegate here, but I'm not sure why you need that now
    // but if for some reason you do: self.delegate = prvc;

    // and if you aren't using ARC: [prvc release];

}

我希望这有道理吗?