调用 popover 并将变量发送回之前的 uiview

Posted

技术标签:

【中文标题】调用 popover 并将变量发送回之前的 uiview【英文标题】:call popover and send back variable to previous uiview 【发布时间】:2017-01-11 21:43:00 【问题描述】:

我有一个使用情节提要创建的 UITableViewController。 比我有一个锚点,它也叫一个用故事板创建的 popOver。弹出框是另一个 UItableViewController,当我单击一行时,我应该回调第一个控制器并传递一个对象。 我在 popover 对象中试过这个:

(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

if ([[segue identifier] isEqualToString:@"Fortune"])

NSIndexPath *indexPath = (NSIndexPath *)sender;

ASFortuneTeller * aController = [segue destinationViewController];
[aController setWYPT:[allWYPTs objectAtIndex:indexPath.row]];

基本上我需要将一个 NSMutableDictionary 包传递给第一个 UITableViewController。 但是我注意到,通过这种方式我创建了一个新的 ASFortuneTeller 对象,这不是我想要的……我只想回调第一个控制器并传递一个对象。

我该怎么做?

【问题讨论】:

【参考方案1】:

一个快速的解决方案(当它总是涉及相同的两个类时)可能是:

在第一个视图控制器的.h文件中,定义一个方法(或者只是一个属性):

-(void)selectedWYPT:(NSMutableDictionary*)wypt;

在第二个视图控制器的 .h 文件中创建一个属性

@property FirstUIViewController *firstView;

在第一个视图控制器中,您将通过 segue 打开第二个视图控制器,因此您可以使用:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
    if ([segue.identifier isEqualToString:@"segueIdentifier"]) 
        SecondUIViewController *destination = segue.destinationViewController;
        destination.firstView = self;
    

在第二个视图中选择该行时,您可以使用

if (self.firstView)    
    [self.firstView setWYPT:[allWYPTs objectAtIndex:indexPath.row]];

将数据传回第一个视图。


如前所述,当总是涉及相同的两个类时,这将是一个快速的解决方案。

另一种方法是使用协议。当第一个视图控制器不总是 FirstUIVierController 时,你可以使用这样的东西:

SecondUIViewController.h

@class SecondUIViewController;
@protocol SecondUIViewControllerDelegate <NSObject>

-(void)secondUIViewController:(SecondUIViewController*)controller didSelectWYPT:(NSMutableDictionary*)wypt;

@end

@interface SecondUIViewController : UIViewController

@property id<SecondUIViewControllerDelegate> delegate;

@end

SecondUIViewController.m

选择行的位置:

if (self.delegate && [self.delegate respondsToSelector:@selector(secondUIViewController:didSelectWYPT:)])
    [self.delegate secondUIViewController:self didSelectWYPT:[allWYPTs objectAtIndex:indexPath.row]];

AnyOtherUIViewController.h

#import "SecondUIViewController.h"

@interface AnyOtherUIViewController : UIViewController <SecondUIViewControllerDelegate>

...
...

AnyOtherViewController.m

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
    if ([segue.identifier isEqualToString:@"segueIdentifier"]) 
        SecondUIViewController *destination = segue.destinationViewController;
        destination.delegate = self;
    


-(void)secondUIViewController:(SecondUIViewController*)controller didSelectWYPT:(NSMutableDictionary*)wypt 
    //do something with the data

【讨论】:

很好解释和实施的解决方案。 :) 感谢解释...但是属性返回错误:.h文件中的未知类型名称...是#import "nameOftheClass.h"实现... @yorh 要关闭弹出框,在将值传递回第一个视图控制器后,您可以使用:[self.navigationController popViewControllerAnimated:YES];。关于未知类型名称,请确保不要在两个 .h 文件中相互导入! FirstViewController.h 应该被导入到第二个的 .h 文件中。 SecondViewController.h 应该导入到第一个的 .m 文件中..

以上是关于调用 popover 并将变量发送回之前的 uiview的主要内容,如果未能解决你的问题,请参考以下文章