跨视图控制器传输图像
在我的iPhone应用程序上,我有两个viewController(firstViewController,secondViewController),在firstViewController上,用户可以从相机胶卷中选择一张照片,然后将其显示在imageView中,但是我还需要在secondViewController中显示它,但不知道如何到。
您能否也请深入解释您的答案,因为我对 Objective-C 相当陌生,
这是我的代码:
firstViewController.hfirstViewController.m
#import <UIKit/UIkit.h>
@interface firstViewController : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
UIImageView *theImageView;
}
@property (nonatomic, retain) IBOutlet UIImageView *theImageView;
-(IBAction)selectExistingPicture;
@end
SecondViewController
#import "firstViewController.h"
#import "secondViewController.h"
@implementation firstViewController
@synthesize theImageView
-(IBAction) selectExistingPicture
{
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage : (UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
theImageView.image = image;
[picker dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *) picker
{
[picker dismissModalViewControllerAnimated:YES];
}
-(IBAction)switchSecondViewController {
SecondViewController *viewcontroller = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self presentModalViewController:viewcontroller animated:YES];
[viewcontroller release];
}
// Default Apple code
@end
中没有太多内容,因此我不会费心发布该代码。
On my iPhone app, I have two viewControllers (firstViewController, secondViewController), on firstViewController the user can select a photo from the camera roll and it then displays it in an imageView, however I need to also display it in secondViewController but have no idea how to.
Can you also please explain your answers in-depth as I am fairly new to objective-C
Here's my code:
firstViewController.h
#import <UIKit/UIkit.h>
@interface firstViewController : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
UIImageView *theImageView;
}
@property (nonatomic, retain) IBOutlet UIImageView *theImageView;
-(IBAction)selectExistingPicture;
@end
firstViewController.m
#import "firstViewController.h"
#import "secondViewController.h"
@implementation firstViewController
@synthesize theImageView
-(IBAction) selectExistingPicture
{
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage : (UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
theImageView.image = image;
[picker dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *) picker
{
[picker dismissModalViewControllerAnimated:YES];
}
-(IBAction)switchSecondViewController {
SecondViewController *viewcontroller = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self presentModalViewController:viewcontroller animated:YES];
[viewcontroller release];
}
// Default Apple code
@end
There's not much in secondViewController so I won't bother posting that code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在第二个视图控制器中有一个类型的方法
在创建视图控制器后,在下面的方法中调用带有图像的方法,如图所示
Have a method in your second view controller of the type
In the method below after you create a viewcontroller call the method with the image as shown
您需要在第二个视图中声明一个属性,以便您可以从第一个视图中设置图像,如下所示:
secondaryImageView 是 secondaryView 中的属性,您必须设置它
You need to declare a property in second view so you can set the image from your first view some thing like this:
secondImageView is the property in secondView and you have to set it