在我的 iPhone 应用程序上,我有两个 viewController(firstViewController、secondViewController),在 firstViewController 上,用户可以从相机胶卷中选择一张照片,然后将其显示在 imageView 中,但是我还需要在 secondViewController 中显示它,但是不知道怎么做。
由于我对 objective-C 还很陌生,能否请您详细解释一下您的答案
这是我的代码:
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
secondViewController 中没有太多内容,因此我不会费心发布该代码。
最佳答案
在你的第二个 View Controller 中有一个方法
-(void)setImage:(UIImage *)image
在下面的方法中,在创建 View Controller 后调用带有图像的方法,如图所示
-(IBAction)switchSecondViewController {
SecondViewController *viewcontroller = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self presentModalViewController:viewcontroller animated:YES];
[viewcontroller setImage:theImageView.image]
[viewcontroller release];
}
关于iphone - 跨 View Controller 传输图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6844762/