【问题标题】:set image from one view to another xcode 4.4将图像从一个视图设置到另一个 xcode 4.4
【发布时间】:2012-08-24 10:48:06
【问题描述】:

我有 2 个视图,它们是 UITabBarController 的一部分。对于每个视图,我声明了一个不同的类。

PictureViewController与方法:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    [imagePicker dismissModalViewControllerAnimated:YES];
    [imageField setImage:image];
}

另一个视图:AdjustViewController 和另一个 UIImage:

@property (weak, nonatomic) IBOutlet UIImageView *viewImage;

我想在上面的方法-didFinishPickingImage中将AdjustViewController中viewImage的值设置为选中的图片。

我该怎么做?

【问题讨论】:

  • PictureViewController 是否引用了 AdjustViewController 实例?
  • 没有。它们是选项卡控件中的 2 个选项卡,我为每个选项卡创建了一个不同的文件
  • 我回答了你的问题,但它非常具体到你如何使用UITabBarController,所以如果你编辑你的问题以包含你正在使用的信息会很酷UITabBarController
  • 谢谢我编辑了它,并会尝试你的解决方案

标签: iphone objective-c ios xcode uiimageview


【解决方案1】:

由于这两个都在 tabBarController 中,您可以使用 tabBarController 获取对另一个视图控制器的引用并从那里访问其属性。

像这样:

NSArray *theViewControllers = [self.tabBarController viewControllers];

//On this line, you will need to use the Index of the AdjustViewController (0 is on the left and then they go in order from left to right.)
AdjustViewController *adjViewController = (AdjustViewController *)[theViewControllers objectAtIndex:0];

adjViewController.viewImage.image = image;

假设您使用正确的索引,这会将图像分配给AdjustViewControllerviewImage 属性,该属性位于UITabBarController 上。

或者,如果您想将内容压缩为尽可能少的行:

((AdjustViewController *)[[self.tabBarController viewControllers] objectAtIndex:0]).viewImage.image = image;

【讨论】:

  • 在上述方法中,如您所愿。
  • 感谢您的回答,但是,当用户单击底部栏中的调整按钮时,图像未设置。知道为什么吗? AdjustViewController 中有任何可以初始化它的函数吗?
  • 你有没有使用上面的代码并将索引更改为AdjustViewController的正确索引? AdjustViewController 是否对 viewWillAppear 上的图像做任何事情? AdjustViewController 是否已经加载?
  • 是的。调整是数字 1(如果我留下数字 0 - 我得到一个变量没有出现在那里的异常)。 AdjustViewController.m 中没有名为 viewWillAppear 的方法。我怎么知道它是否已经加载?
  • 除非您已经点击了该选项卡,否则它很可能不会加载。您可以在AdjustViewController 中设置 viewDidLoad 方法,以使用相同的方法从PictureViewController 中提取图像,但使用PictureViewController 的索引。
【解决方案2】:

编辑:要在 AppDelegate 中设置您的图像,您必须在 AppDelegate 中为 UIImage *image 创建属性并像这样分配图像:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{ 
   MyAppDelegateClass *appDelegate = (MyAppDelegateClass  *)[[UIApplication sharedApplicaton] delegate];
  appDelegate.image=image; //your picked image here

  [imageField setImage:image];
  [imagePicker dismissModalViewControllerAnimated:YES];

}

传递数据的快速而肮脏的方法是向应用委托添加属性,然后使用以下方法从视图控制器调用应用委托:

MyAppDelegateClass *appDelegate= (MyAppDelegateClass  *)[[UIApplication sharedApplicaton] delegate];
viewImage.image=appDelegate.image;

检索变化数据的最佳位置是在 viewWillAppear 控制器方法中。这样,每次用户切换到该选项卡时,数据都会更新。


您可能需要考虑 NSNotificationCenter(参考);您在应用程序通知中心注册一个视图控制器,并在做出选择时发送通知。当收到通知时,另一个视图控制器会相应地更新自己

更多信息请参考this链接

【讨论】:

  • 您的意思是:1. 向 AppDelegate.h 文件中添加属性图像 2. 将这两行添加到 viewWillApear 控制器方法中,如何在 didFinishPicking 方法中将图像值分配给 AppDelegate?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多