从弹出窗口中的表中将图像加载到图像视图中 [英] load images in an image view from a table in a popover

查看:68
本文介绍了从弹出窗口中的表中将图像加载到图像视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有图像视图的视图控制器。

I have a view controller with an image view in it.

我有一个带表格视图的弹出框,该弹出框固定在此视图的一个条形按钮上

I have a popover with a table view in it which is anchored to a bar button in this view controller.

我希望能够通过使用弹出框中的表将图像加载到图像视图中。

I would like to be able to load images into the image view by using the table in the popover.

popover和主view controller都有单独的view controller类。

Both the popover and the main view controller have separate view controller classes.

我从一个segue中启动了popover。

I have launched the popover from a segue.

我该怎么办?

推荐答案

我假设您的选择将您从imageViewController带到您的

I am assuming that your segue takes you from your imageViewController to your popped-over tableViewController.

然后,您可以将imageViewController设置为tableViewController的委托,以便您可以以分离的方式从tableViewController调用其上的方法。

Then you can set your imageViewController as delegate to the tableViewController, so that you can call methods on it from the tableViewController in a decoupled manner.

MyTableViewController.h

在tableViewController头文件中声明一个期望的协议它是要遵循的委托。将其放在您的@interface部分上方:

In your tableViewController header file declare a protocol which it will expect it's delegate to follow. Place it above your @interface section:

    @protocol MyTableViewControllerDelegate <NSObject>
    - (void) dismissPopoverAndLoadImage:(NSString*)imageName;
    @end

也声明一个属性来保存对其委托的引用:

Also declare a property to hold a reference to it's delegate:

  @property (nonatomic, weak) id <MyTableViewControllerDelegate> delegate;

协议声明您的tableView希望能够对其委托进行调用的方法签名。它允许它发送回一些数据,并使其自身被关闭。委托(在本例中为您的imageViewController)将必须实现此方法。

The protocol declares the method signature that your tableView will expect to be able to call on its delegate. It allows it to send back some data, and get itself dismissed. The delegate (in this case, your imageViewController) will have to implement this method.

MyTableViewController.m

当选择一个表格单元格时在委托上调用该方法:

The method is called on the delegate when a table cell is selected:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
       UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
       NSString* imageName = cell.textLabel.text;
       [self.delegate dismissPopoverAndLoadImage:imageName];
    }

MyImageViewController.h

包括MyTableViewController.h并将委托协议添加到 @interface

include MyTableViewController.h and add the delegate protocol to the @interface.

#include "TableViewController.h

@interface MyImageViewController: UIViewController <MyTableViewControllerDelegate>

声明一个属性来保存对UIPopOverController的引用,以便向其发送关闭消息:

Declare a property to hold a reference to your UIPopOverController so that you can send it a dismiss message:

@property (nonatomic, weak) UIPopoverController* seguePopoverController;

(这些步骤可以

MyImageViewController.m

您将在 MyImageViewController prepareForSegue 方法中设置委托属性,当segue为您还将设置对popoverControlle的引用

You will set the delegate property in MyImageViewController's prepareForSegue method, which gets called when the segue is invoked.You will also set the reference to the popoverController here.

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"popoverTable"]) {
        self.seguePopoverController = [(UIStoryboardPopoverSegue*)segue popoverController];
        [segue.destinationViewController setDelegate:self];
        }
    }
} 

最后,您实现了tableViewController的委托方法:

Lastly, you implement the tableViewController's delegate method:

- (void) dismissPopoverAndLoadImage:(NSString*)imageName
{
    self.imageView.image = [UIImage imageNamed:imageName];
    [self.seguePopoverController dismissPopoverAnimated:YES];
}






update

除了popOverController本身是一个稍微不寻常的实体(没有视图的控制器,直接从NSObject继承)之外,大多数都是标准的委托模式。您可以通过在 didSelectRowAtIndexPath 中使用一些间接和运行时检查来稍微简化一下:


update
Aside from the fact that the popOverController itself is a slightly unusual entity (a controller without a view, inheriting directly from NSObject), most of this is the standard delegation pattern. You could simplify it somewhat by using a bit of indirection and runtime checking in didSelectRowAtIndexPath:

 if ([[self delegate] respondsToSelector:@selector(dismissPopoverAndLoadImage:)])
       [[self delegate] performSelector:@selector(dismissPopoverAndLoadImage:)
                             withObject:imageName];

在这种情况下,您无需定义协议或< adhere> ; ,则无需 #import MyTableViewController 。但是,如果您未正确实现该方法,则编译器将对您没有帮助。从我以前的错误中可以看出,这可能是不明智的。

In this case you would not need to define the protocol or <adhere> to it, and you wouldn't need to #import MyTableViewController. However the compiler would give you no help if you did not implement the method correctly. Which, as you can see from my earlier mistake, is probably unwise.

这篇关于从弹出窗口中的表中将图像加载到图像视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆