Ios 如何在不使用segue的情况下在视图控制器之间传递数据

Ios 如何在不使用segue的情况下在视图控制器之间传递数据,ios,objective-c,storyboard,Ios,Objective C,Storyboard,我正在使用故事板开发一个iPad应用程序。在我的应用程序中,我使用segue模态表示从第一视图控制器连接了一个模态视图控制器。单击模态视图中出现的一个按钮可以关闭模态视图。如何在不使用segue的情况下将一个字典从模态视图控制器传递到第一视图控制器。您可以通过按住ctrl键并将其从第一个控制器拖动到第二个控制器(不要忘记指定此序列和标识符) 接下来在按钮的iAction(通过Interface Builder或addTarget:self action:forControlEvents:设置)中

我正在使用故事板开发一个iPad应用程序。在我的应用程序中,我使用segue模态表示从第一视图控制器连接了一个模态视图控制器。单击模态视图中出现的一个按钮可以关闭模态视图。如何在不使用segue的情况下将一个字典从模态视图控制器传递到第一视图控制器。

您可以通过按住ctrl键并将其从第一个控制器拖动到第二个控制器(不要忘记指定此序列和标识符)

接下来在按钮的iAction(通过Interface Builder或addTarget:self action:forControlEvents:设置)中,您可以调用[self-PerformsgueWithIdentifier:@“YourSegueIdentifier”发送方:按钮]


您可以像往常一样,在-(void)prepareforsgue:(UIStoryboardSegue*)segue sender:(id)sender中使用委托将数据传递给第二个控制器。 经历这一切


它有简单而完美的答案。

您可以使用通知传递值。

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

}
// 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
[[NSNotificationCenter defaultCenter] removeObserver:self]
您可以从观察到的入站通知中检索词典。在发布通知之前添加观察员。

// 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
[[NSNotificationCenter defaultCenter] removeObserver:self]
//用于获取数据

-(void)anyAction:(NSNotification *)anote
{
NSDictionary *dict = [anote userInfo];
AnyClass *objectIWantToTransfer = [dict objectForKey:@"objectName"];
}
请注意,您应该在dealloc方法中删除作为观察者的对象。

// 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
[[NSNotificationCenter defaultCenter] removeObserver:self]

使用协议是向前一个控制器或任何其他控制器发送信息的最佳过程

在您演示的viewcontrooler中,以.h格式写入此内容

@class yourclassName;
typedef void(^yourclassName回调)(NSDictionary*)

@协议类名称委托 @必需的 -(void)你的classnamedireceivedata:(NSDictionary*)dataDict

@结束

在一些你需要传递信息的按钮动作中,写下

[self yourclassNamedidReceiveData:yourDictHere]; // you can use any as per your requirement
注意:yourclassNamedidReceiveData方法在一个类中声明,现在它在另一个类中实现,您将从该类调用该类

在您以前的控制器中(您试图在其中呈现一个类(假设classA和classB),第一个类是classA,呈现的类是classB)

- (void) yourclassNamedidReceiveData:(NSDictionary *)dataDict
{

}
注意*不要忘记将委托设置为classB EX:ClassBobObject.delegate=self

希望这将有助于

使用协议这是将信息传递回上一个控制器的最佳方式..我尝试了您上面提到的方法,但出现了3个错误。综合委托=_委托;合成callbackBlock=\u callbackBlock;错误:-属性实现必须在接口“MyclassName”中。然后在调用函数的位置出现一个错误..[self YourClassName DidReceiveData:yourDictHere];错误-在“MyclassName”的接口处不可见声明选择器“MyclassNamedidReceiveData”。请帮助我如何解决此错误。我不需要segue。如果我要使用segue,我的第一个视图将重新加载,我不需要。