xcode 是否可以使用 popViewControllerAnimated 传递数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13509832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
it possible to Pass Data with popViewControllerAnimated?
提问by Sosily
I came across an interesting problem, i have main ViewController let's call him MainVC with navigationController and i am doing performSegueWithIdentifier from him to Mine second ViewController let's call him SecVC. so when i am trying to do the popViewControllerAnimated i want to pass some data from the SecVC to the MainVc.. i know i can do it with appDelegate Param or with singletons class but my question is : can i do it with more Elegant solution? like i use prepareForSegue and use local parmeters..
我遇到了一个有趣的问题,我有主 ViewController,让我们用导航控制器称他为 MainVC,我正在执行从他到我的第二个 ViewController 的 performSegueWithIdentifier,让我们称他为 SecVC。因此,当我尝试执行 popViewControllerAnimated 时,我想将一些数据从 SecVC 传递到 MainVc。就像我使用 prepareForSegue 并使用本地参数一样..
Thank you...
谢谢...
回答by Saliom
The best way to do it is by using a delegate.
最好的方法是使用委托。
//SecVCDelegate.h
//SecVCDelegate.h
#import <Foundation/Foundation.h>
@protocol SecVSDelegate <NSObject>
@optional
- (void)secVCDidDismisWithData:(NSObject*)data;
@end
//SecVC.h
//SecVC.h
#import <UIKit/UIKit.h>
#import "SecVSDelegate.h"
@interface SecVC : UIViewController
/** Returns the delegate */
@property (nonatomic, assign) id<SecVSDelegate> delegate;
@end
//SecVC.M
//SecVC.M
...
- (void) dealloc
{
...
delegate = nil
...
}
When ever you popViewControllerAnimated, right after it (or before it) you do this
当你 popViewControllerAnimated 时,就在它之后(或之前)你这样做
if(_delegate && [_delegate respondsToSelector:@selector(secVCDidDismisWithData:)])
{
[_delegate secVCDidDismisWithData:myDataObject];
}
And in the MainVC you must be certain that you implement the delegate function //MainVC.m
在 MainVC 中,您必须确定您实现了委托函数 //MainVC.m
- (void)secVCDidDismisWithData
{
//do whatever you want with the data
}
To avoid any warnings you must tell that the MainVC class implements the delegate like this:
为了避免任何警告,您必须告诉 MainVC 类实现了这样的委托:
//MainVC.h
//主VC.h
#import "SecVCDelegate.h"
...
@interface MainVC : UIViewController <SecVCDelegate>
...
secVCInstance.delegate = self;
[self.navigationController pushViewController:secVCInstance];
...
回答by Atif Alvi
While I agree that the best option is to use Delegate, but still if any one is looking for something different, he can use NSNotificationCenteras an alternative.
虽然我同意最好的选择是使用Delegate,但如果有人正在寻找不同的东西,他可以使用NSNotificationCenter作为替代。
In viewDidLoadof MainVC:
在MainVC 的viewDidLoad中:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(recvData:)
name:@"SecVCPopped"
object:nil];
}
And add method recvDatain MainVC.m
相加法recvData在MainVC.m
- (void) recvData:(NSNotification *) notification
{
NSDictionary* userInfo = notification.userInfo;
int messageTotal = [[userInfo objectForKey:@"total"] intValue];
NSLog (@"Successfully received data from notification! %i", messageTotal);
}
Now in SecVC, before popping, add this line
现在在 SecVC 中,在弹出之前,添加这一行
NSMutableDictionary* userInfo = [NSMutableDictionary dictionary];
[userInfo setObject:[NSNumber numberWithInt:messageTotal] forKey:@"total"];
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:@"SecVCPopped" object:self userInfo:userInfo];
回答by John
I would do it in one of the following ways, but I'm not sure if it's elegant enough...
我会通过以下方式之一来做,但我不确定它是否足够优雅......
In SecVC, add an
@property MainVC *mainVC;
Use[self.mainVC setSomeValue:...];
before calling[self.navigationController popViewControllerAnimated:...];
Use [self.navigationController viewControllers]; to find out the
MainVC *mainVC
, and call[mainVC setSomeValue:...];
before the line of code that pop theViewController
.
在SecVC中,调用前加一个
@property MainVC *mainVC;
Use[self.mainVC setSomeValue:...];
[self.navigationController popViewControllerAnimated:...];
使用 [self.navigationController viewControllers]; 找出
MainVC *mainVC
, 并[mainVC setSomeValue:...];
在弹出ViewController
.
Is this what you want?
这是你想要的吗?
回答by Harry Bloom
I simply set up a protocol in the view being dismissed (example in Swift):
我只是在被驳回的视图中设置了一个协议(Swift 中的示例):
protocol ExampleTableViewControllerDismissDelegate {
func didDismiss(withData: Any)
}
var delegate: SearchableTableViewControllerDismissDelegate?
You can then call this when you dismiss/pop your view like this
然后,当您像这样关闭/弹出您的视图时,您可以调用它
self.navigationController?.popViewController(animated: true)
delegate?.didDismiss(withData: Any)
Then in the view being dismissed to (the parent in the hierarchy), we can conform to the delegate and essentially get a callback with the data after the view has been dismissed.
然后在视图被解散到(层次结构中的父级)中,我们可以符合委托并在视图被解散后本质上获得带有数据的回调。
//MARK: ExampleTableViewControllerDismissDelegate
func didDismiss(withData: Any) {
//do some funky stuff
}
And don't forget to subscribe to the delegate in the parent view by using
并且不要忘记通过使用订阅父视图中的委托
viewController.delegate = self
回答by lojals
There is another way to pass data between views including popViewControllerAnimatedand it's with a global var instance, so If you modify that Var in your detail view and after do the popViewControllerAnimated, you can call the new data in the viewWillAppear method.
还有另一种在视图之间传递数据的方法,包括popViewControllerAnimated并且它使用全局 var 实例,因此如果您在详细视图中修改该 Var 并在执行popViewControllerAnimated 之后,您可以在 viewWillAppear 方法中调用新数据。
The first step is declare the Global var in main.h
第一步是在main.h中声明全局变量
NSMutableArray * layerList;
And now you have to call it in detail view.
现在您必须在详细信息视图中调用它。
SecondView.m
第二视图.m
extern NSString *layerList;
SecondView.h
第二视图.h
-(void)back{
layerList = @"Value to send";
[self.navigationController popViewControllerAnimated:YES];
}
Now you can use the information in the Master View after detect the pop action.
现在您可以在检测到弹出操作后使用主视图中的信息。
FirstView.m
FirstView.m
extern NSString *layerList;
FirstView.h
第一视图.h
-(void)viewWillAppear:(BOOL)animated{
NSLog(@"This is what I received: %@",layerList);
}