【问题标题】:it possible to Pass Data with popViewControllerAnimated?可以使用 popViewController Animated 传递数据吗?
【发布时间】:2012-11-22 09:47:51
【问题描述】:

我遇到了一个有趣的问题,我有主 ViewController,让我们用导航控制器称他为 MainVC,我正在执行从他到我的第二个 ViewController 的 performSegueWithIdentifier,让我们称他为 SecVC。因此,当我尝试执行 popViewControllerAnimated 时,我想将一些数据从 SecVC 传递到 MainVc .. 我知道我可以使用 appDelegate Param 或单例类来做到这一点,但我的问题是:我可以用更优雅的解决方案来做到这一点吗?就像我使用 prepareForSegue 并使用本地参数..

谢谢...

【问题讨论】:

  • 据我所知,这是不可能的。您必须使用 golabla 变量来存储数据或将其存储在用户默认值中。
  • 我会 .. 我想试试没有:(
  • 这是在 Swift 中将代理与 popViewControllerAnimated 一起使用的真正方法!:stackoverflow.com/questions/39692791/…

标签: iphone objective-c ios xcode


【解决方案1】:

最好的方法是使用委托。

//SecVCDelegate.h

#import <Foundation/Foundation.h>

@protocol SecVSDelegate <NSObject>

@optional
- (void)secVCDidDismisWithData:(NSObject*)data;
@end

//SecVC.h

#import <UIKit/UIKit.h>
#import "SecVSDelegate.h"

@interface SecVC : UIViewController

/** Returns the delegate */
@property (nonatomic, assign)   id<SecVSDelegate> delegate;

@end

//SecVC.M

...

- (void) dealloc
{
...
delegate = nil
...
}

当你 popViewControllerAnimated,在它之后(或之前)你这样做

if(_delegate && [_delegate respondsToSelector:@selector(secVCDidDismisWithData:)])
{
[_delegate secVCDidDismisWithData:myDataObject];
}

并且在 MainVC 中你必须确定你实现了委托函数 //MainVC.m

- (void)secVCDidDismisWithData
{
//do whatever you want with the data
}

为避免任何警告,您必须告知 MainVC 类实现了这样的委托:

//MainVC.h

#import "SecVCDelegate.h"
...
@interface MainVC : UIViewController <SecVCDelegate>
...
secVCInstance.delegate = self;
[self.navigationController pushViewController:secVCInstance]; 
...

【讨论】:

  • 我照你说的做了。但是为什么这不起作用?我应该在哪里做错事?
  • @alicanbature - 确保在 pushViewController 之前的 MainVC 中设置了 SecVC.delegate = self;,此答案中未提及
  • 是的,你是对的@thefoyer,我刚刚编辑了我的答案以添加它
【解决方案2】:

虽然我同意最好的选择是使用 Delegate, 但如果有人在寻找不同的东西,他可以使用 NSNotificationCenter 作为替代。

在 MainVC 的 viewDidLoad 中:

- (void)viewDidLoad
{

    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(recvData:)
                                             name:@"SecVCPopped"
                                           object:nil];
}

并在 MainVC.m 中添加方法 recvData

- (void) recvData:(NSNotification *) notification
{

    NSDictionary* userInfo = notification.userInfo;
    int messageTotal = [[userInfo objectForKey:@"total"] intValue];
    NSLog (@"Successfully received data from notification! %i", messageTotal);
}

现在在 SecVC 中,在弹出之前,添加这一行

NSMutableDictionary* userInfo = [NSMutableDictionary dictionary];
[userInfo setObject:[NSNumber numberWithInt:messageTotal] forKey:@"total"];

NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:@"SecVCPopped" object:self userInfo:userInfo];

【讨论】:

    【解决方案3】:

    我会通过以下方式之一来做,但我不确定它是否足够优雅......

    1. 在SecVC中,在调用[self.navigationController popViewControllerAnimated:...];之前添加@property MainVC *mainVC;使用[self.mainVC setSomeValue:...];

    2. 使用 [self.navigationController viewControllers];找出MainVC *mainVC,并在弹出ViewController的代码行之前调用[mainVC setSomeValue:...];。

    这是你想要的吗?

    【讨论】:

    • 这对我来说不是很清楚.. 请你解释一下
    • @DivineChild,我认为 Saliom 建议的委托方法比我的回答更好。请看他的回答。
    【解决方案4】:

    我只是在被关闭的视图中设置了一个协议(Swift 中的示例):

     protocol ExampleTableViewControllerDismissDelegate {
        func didDismiss(withData: Any)
    }
    
    var delegate: SearchableTableViewControllerDismissDelegate?
    

    当你像这样关闭/弹出你的视图时,你可以调用它

    self.navigationController?.popViewController(animated: true)
    delegate?.didDismiss(withData: Any)
    

    然后在被解除的视图(层次结构中的父级)中,我们可以符合委托,并在视图被解除后基本上获得带有数据的回调。

    //MARK: ExampleTableViewControllerDismissDelegate
    
    func didDismiss(withData: Any) {
        //do some funky stuff
    }
    

    并且不要忘记使用订阅父视图中的委托

    viewController.delegate = self
    

    【讨论】:

      【解决方案5】:

      还有另一种在视图之间传递数据的方法,包括 popViewControllerAnimated 并且它使用全局 var 实例,因此如果您在详细视图中修改该 Var 并在之后执行 popViewControllerAnimated,可以在viewWillAppear方法中调用新数据。

      第一步是在 main.h 中声明全局变量

      NSMutableArray * layerList;
      

      现在你必须在细节视图中调用它。

      SecondView.m

      extern NSString *layerList;
      

      SecondView.h

      -(void)back{
          layerList = @"Value to send";
          [self.navigationController popViewControllerAnimated:YES];
      }
      

      现在您可以在检测到弹出动作后使用主视图中的信息。

      FirstView.m

      extern NSString *layerList;
      

      FirstView.h

      -(void)viewWillAppear:(BOOL)animated{
         NSLog(@"This is what I received: %@",layerList);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-10
        • 2015-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多