我正在使用storyboard开发一个iPad应用程序。在我的应用程序中,我使用segue modal presentation从第一个视图控制器连接了一个模态视图控制器。该模态视图通过在模态视图中出现的一个按钮进行关闭。如何在不使用segue的情况下从模态视图控制器传递一个字典到第一个视图控制器。
我正在使用storyboard开发一个iPad应用程序。在我的应用程序中,我使用segue modal presentation从第一个视图控制器连接了一个模态视图控制器。该模态视图通过在模态视图中出现的一个按钮进行关闭。如何在不使用segue的情况下从模态视图控制器传递一个字典到第一个视图控制器。
// Send Data
NSDictionary *aDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
anObject, @"objectName",
anotherObject, @"objectId",
nil] autorelease];
[[NSNotificationCenter defaultCenter] postNotificationName:@"AnythingAtAll" object:nil userInfo:aDictionary];
您可以从您观察到的入站通知中检索字典。请在发布通知之前提前添加观察者。
//this might be in your init method or a viewDidLoad method of your FirstViewController
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(anyAction:) name:@"AnythingAtAll" object:nil];
enter code here
// 获取数据
-(void)anyAction:(NSNotification *)anote
{
NSDictionary *dict = [anote userInfo];
AnyClass *objectIWantToTransfer = [dict objectForKey:@"objectName"];
}
[[NSNotificationCenter defaultCenter] removeObserver:self]
- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion {
SecondViewController *secVC = (SecondViewController*)toViewController;
secVC.property = self.property
}
使用协议是向先前控制器或任何其他控制器发送信息的最佳过程
在您的视图控制器中,将其写入.h文件中
@class yourclassName;
typedef void (^yourclassName回调) (NSDictionary*);
@protocol yourclassName代理 @required - (void)yourclassNamedidReceiveData:(NSDictionary *)dataDict;
@end
in .m file
@synthesize delegate=_delegate;
@synthesize callbackBlock=_callbackBlock;
[self yourclassNamedidReceiveData:yourDictHere]; // you can use any as per your requirement
- (void) yourclassNamedidReceiveData:(NSDictionary *)dataDict
{
}
注意* 不要忘记将委托设置为classB,例如:classBObject.delegate=self;
希望这能帮到你
你可以通过从第一个控制器向第二个控制器进行ctrl+拖动,创建一个不绑定到按钮的Segue(不要忘记给这个segue一个标识符)。
接下来,在按钮的IBAction中(通过Interface Builder设置或通过addTarget:self action:forControlEvents:设置),你可以调用[self performSegueWithIdentifier:@"YourSegueIdentifier" sender:button];
你可以像往常一样,在- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender中将数据传递给第二个控制器。