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

发布于 2024-12-20 19:00:05 字数 3045 浏览 3 评论 0 原文

您好,我的代表没有将图像和一些字符串传递给我的第二个导航控制器。任何人都可以看到出了什么问题吗?我创建了 ImageAndTextDelegate.h 、 PhotoEditViewController.h/.m 和 PreviewFrameViewController.h/.m 。基本上在选择一些图像并在照片编辑中输入一些文本之后。我想使用我的委托方法将数据发送到预览帧。

//ImageAndTextDelegate.h
@protocol ImageAndTextDelegate

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

@end

//PhotoEditViewController.h #导入“ImageAndTextDelegate.h”

@interface PhotoEditViewController : UIViewController

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

@property (nonatomic,retain) UITextField *mainText;
@property(非原子,保留)UITextField *subText;
@property(非原子,保留)UIImageView *imageView;
@property(非原子,保留)UIButton *previewPictureButton;
@property(非原子,分配)IBOutlet id代表;

-(IBAction)预览图片:(id)发件人;

@结尾
//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 
#import“ImageAndTextDelegate.h”

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

@property(非原子,保留)UIImageView *myImage;
@property (非原子,保留) UILabel *main;
@property (非原子,保留) UILabel *sub;

@结尾
//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;
}

Hi my delegate isn't passing an image and some strings to my second navigation controller. Can anyone see whats wrong? I've created ImageAndTextDelegate.h , PhotoEditViewController.h/.m and PreviewFrameViewController.h/.m . Basically after selecting some images and typing some text in photoedit. i wanna send the data through to previewframe using my delegate methods.

//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>{
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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

踏月而来 2024-12-27 19:00:06

因此,您的 PhotoEditViewController 正在创建并呈现一个 PreviewFrameViewController。

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

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

做您似乎想要的事情的最直接方法是这样(在您的 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];

}

我希望这是有道理的?

So your PhotoEditViewController is creating and presenting a PreviewFrameViewController.

Then in its viewDidLoad, your PreviewFrameViewController is creating its own PhotoEditViewController and setting that instance's delegate to itself.

So you've got two PhotoEditViewControllers now, and the first one still has no delegate. (Which leads to the question of why you needed the delegate pattern here to begin with?)

The most direct way to do what you seem to want is this (in your 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];

}

I hope that makes sense?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文