iphone - 跨 View Controller 传输图像

标签 iphone objective-c ios xcode imageview

在我的 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/

相关文章:

iPhone 内存管理 didReceiveMemoryWarning

iphone - iPhone 应用程序可以在启动时启动吗?

iphone - ios如何访问和修改不同类的变量?

iphone - avaudioplayer 完成播放后请求回电或通知

ios - iOS应用本地化

objective-c - 在分配实例之前尝试访问静态属性时发生崩溃

java - Android 和 ios 中的单词库

iphone - Xcode 中的 Cordova 错误 - 没有这样的文件或目录

ios - Xcode NSURLConnection NSMutableData appdendData 有时只能工作

ios - Apple Push Notifications - 将数据放入应用程序