如何从A类(控制器1的自定义类)更改B类(控制器2的自定义类)中的imageView?

时间:2014-03-06 20:35:59

标签: ios objective-c uiviewcontroller

我真的在苦苦挣扎,也许是因为我是Objective-C的新手,能够执行以下操作。

  1. 控制器MAViewControllerMenu(控制器1)拥有一个允许从库中选择图片的按钮
  2. 控制器MAViewControllerPictureDisplay(控制器2)拥有一个imageView(bigImageView)
  3. 一旦我从控制器1中的选择器中选择了一张图片,我想移动到下一个窗口(控制器2),然后让imageView保持我选择的图片。
  4. 问题: 1.它不会显示下一个控制器 2.它不会改变imageView来保存我选择的图片

    观察: 1.当我在控制器1上使用imageView而不是2时,它就像我想要的那样保持图像 2.调试器显示调用控制器2类中更改imageView的函数。但是,即使我试图简单地更改标签,也没有任何变化。

    代码: MAViewControllerMenu.h

     @interface MAViewControllerMenu : UIViewController     <UIImagePickerControllerDelegate,UINavigationControllerDelegate>
    {
        UIImagePickerController *picker1;
        UIImagePickerController *picker2;
        UIImage *image;
        IBOutlet UIImageView *imageView;//THIS CHANGES,BUT IS IN CONTROLLER 1
    }
    - (IBAction)TakePhoto;
    - (IBAction)ChooseExisting;
    - (IBAction)movetonext;
    

    MAViewControllerPictureDisplay.h

    #import "MAViewControllerMenu.h"
    
    
    @interface MAViewControllerPictureDisplay : UIViewController <UIImagePickerControllerDelegate,UINavigationControllerDelegate,menuControllerDelegate>
    {
    
    
    }
    
    @property (weak, nonatomic) IBOutlet UIImageView *bigImageView;//THIS ONE WOULDNT HOLD THE CHOSEN IMAGE
    @property (weak, nonatomic) IBOutlet UILabel *theLabel;
    @property(nonatomic, strong) UIImage *image;
    
    @end
    

    实施

    - (void)viewDidLoad
    {
    
        [super viewDidLoad];
    // Do any additional setup after loading the view.
        //load next page
        self.bigImageView.image = self.image;
    
    
    }
    

    在MAViewControllerMenu.m

    中对图片选择做出反应的方法
    #import "MAViewControllerMenu.h"
    #import "MAViewControllerPictureDisplay.h"
    
    @interface MAViewControllerMenu ()
    
    @end
    
    @implementation MAViewControllerMenu
    
    MAViewControllerPictureDisplay* imageControllerView;
    
    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        image = [info objectForKey:UIImagePickerControllerOriginalImage];
        [imageView setImage:image]; //WORKS ON CURRENT CONTROLLER
        //imageControllerView.theLabel.text = @"hhdhdhd"; //DIDNT WORK
        [imageControllerView.bigImageView setImage:image]; //UPDATE IMAGE AT CONTROLLER 2, DIDNT WORK
        [self dismissViewControllerAnimated:YES completion:NULL];
        [self movetonext];
    }
    
    
    - (IBAction)movetonext{
        [self presentViewController:imageControllerView animated:YES completion:nil];//MOVE TO NEXT WINDOW- DOESNT WORK WHEN INVOKED AFTER SELECTING A PICTURE. DOES WORK WHEN INVOKING,SAY, AFTER A BUTTON WAS CLICKED
    }
    
    
    - (void)viewDidLoad
    {
    
        [super viewDidLoad];
    // Do any additional setup after loading the view.
        //load next page
        imageControllerView = [[self storyboard] instantiateViewControllerWithIdentifier:@"chosenImageController"];
    
    
    }
    

2 个答案:

答案 0 :(得分:0)

图片未显示在第二个视图控制器中,因为此时您正在设置图像的图像视图尚不存在。

[imageControllerView.bigImageView setImage:image];

声明UIImage property并使用它传递图像。然后在imageControllerView的{​​{1}}中显示viewDidLoad。例如:

UIImageView

答案 1 :(得分:0)

不要那样做。您应该将另一个视图控制器的视图视为私有。 Reecon显示了执行此操作的正确方法(设置属性以保存信息,然后在viewDidLoad或vieWillAppear:animated中显示该信息)