可以使用popViewControllerAnimated传递数据吗? [英] it possible to Pass Data with popViewControllerAnimated?

查看:336
本文介绍了可以使用popViewControllerAnimated传递数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个有趣的问题,我有一个主要的ViewController让我用navController调用他的MainVC,我正在从他执行performSegueWithIdentifier到我的第二个ViewController让我们称他为SecVC。所以,当我试图做popViewControllerAnimated我想从SecVC一些数据传递给MainVc ..我知道我可以用的appDelegate帕拉姆或单身类做,但我的问题是:我可以用更优雅的解决方案做呢?就像我使用prepareForSegue并使用本地参数..

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..

谢谢......

推荐答案

最好的方法是使用委托。

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
...
}

什么时候popViewControllerAnimated,就在它之后(或之前)你这样做

When ever you popViewControllerAnimated, right after it (or before it) you do this

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

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

And in the MainVC you must be certain that you implement the delegate function //MainVC.m

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

为了避免任何警告,您必须告诉MainVC类如下所示实现委托:

To avoid any warnings you must tell that the MainVC class implements the delegate like this:

// MainVC.h

//MainVC.h

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

这篇关于可以使用popViewControllerAnimated传递数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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