有可能传递数据与popViewControllerAnimated?
我遇到了一个有趣的问题,我有主ViewController让我们调用他与导航控制器的MainVC,我正在执行从他的performSegueWithIdentifier我的第二个ViewController让我们叫他SecVC。 所以当我想要做popViewControllerAnimated我想从SecVC传递一些数据到MainVc ..我知道我可以做到这一点与appdelegate参数或与singletons类,但我的问题是:我可以做更雅致的解决scheme吗? 像我使用prepareForSegue和使用本地参数..
谢谢…
最好的办法是使用委托。
//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]; ...
虽然我同意最好的select是使用委托 ,但如果有人正在寻找不同的东西,他可以使用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中,在popup之前,添加这一行
NSMutableDictionary* userInfo = [NSMutableDictionary dictionary]; [userInfo setObject:[NSNumber numberWithInt:messageTotal] forKey:@"total"]; NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; [nc postNotificationName:@"SecVCPopped" object:self userInfo:userInfo];
我会用下面的方法之一,但我不确定它是否够优雅…
-
在SecVC中,添加
@property MainVC *mainVC;
使用[self.mainVC setSomeValue:...];
在调用[self.navigationController popViewControllerAnimated:...];
-
使用[self.navigationController viewControllers]; 找出
MainVC *mainVC
,并调用[mainVC setSomeValue:...];
在popupViewController
的代码行之前。
这是你想要的吗?
我只是在视图中设置了一个协议(例如Swift):
protocol ExampleTableViewControllerDismissDelegate { func didDismiss(withData: Any) } var delegate: SearchableTableViewControllerDismissDelegate?
你可以打电话给你,当你解雇/popup你的观点是这样的
self.navigationController?.popViewController(animated: true) delegate?.didDismiss(withData: Any)
然后,在视图被解散到(层次结构中的父级)的情况下,我们可以遵从委托,并且在视图被解除之后基本上与数据进行callback。
//MARK: ExampleTableViewControllerDismissDelegate func didDismiss(withData: Any) { //do some funky stuff }
并且不要忘记通过使用在父视图中订阅委托
viewController.delegate = self
还有另外一种方法可以在视图之间传递数据,包括popViewControllerAnimated和全局的var实例,所以如果你在你的详细视图中修改了Var,并且在执行了popViewControllerAnimated之后 ,你可以在viewWillAppear方法中调用新的数据。
第一步是在main.h中声明Global var
NSMutableArray * layerList;
现在,你必须详细的调用它。
SecondView.m
extern NSString *layerList;
SecondView.h
-(void)back{ layerList = @"Value to send"; [self.navigationController popViewControllerAnimated:YES]; }
现在,您可以在检测到popup操作后使用Master View中的信息。
FirstView.m
extern NSString *layerList;
FirstView.h
-(void)viewWillAppear:(BOOL)animated{ NSLog(@"This is what I received: %@",layerList); }