委托方法不将数据传递给第二个视图控制器
您好,我的代表没有将图像和一些字符串传递给我的第二个导航控制器。任何人都可以看到出了什么问题吗?我创建了 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,您的 PhotoEditViewController 正在创建并呈现一个 PreviewFrameViewController。
然后在其 viewDidLoad 中,您的 PreviewFrameViewController 创建自己的 PhotoEditViewController 并将该实例的委托设置为其自身。
所以你现在有两个 PhotoEditViewController,而第一个仍然没有委托。 (这导致了一个问题:为什么您需要这里的委托模式?)
做您似乎想要的事情的最直接方法是这样(在您的 PhotoEditViewController 中):
我希望这是有道理的?
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):
I hope that makes sense?