通过presentModalViewController添加的视图将其解除与调用该调用的类进行通信的最佳方式是什么? [英] What is the best way for a view added via presentModalViewController to communicate its dismissal with the class which invoked the call?

查看:82
本文介绍了通过presentModalViewController添加的视图将其解除与调用该调用的类进行通信的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过presentModalViewController添加的视图与调用该调用的类进行通信的最佳方式是什么?

What is the best way for a view added via presentModalViewController to communicate its dismissal with the class which invoked the call?

在我的方法之一中,我使用了presentModalViewController :animated:弹出一个记分牌,在其中输入玩家的姓名,然后单击确定。一切都与计分板配合良好,但是当我使用dissmissModaleViewController时,我希望为该类调用presentModeViewController的方法显示警报。

In one of my methods, I use presentModalViewController:animated: to popup a scoreboard where a player enters their name, then clicks OK. Everything is working fine with the scoreboard, but when I use dissmissModaleViewController I'd like a way for the class which called presentModeViewController to display an alert.

我试图但以下是调用presentModalViewController的方法:

I tried to but the following in the method which called the presentModalViewController:

[self presentModalViewController:"myNib" animated:YES];
[alert show];

但是警报代码在presentModalViewController运行之后运行,因此我假设当presentModalViewController被调用时,它不会

But the alert code runs after the presentModalViewController runs, so I assume that when presentModalViewController is called, it doesn't stop the code executing outside of it.

无论如何,我需要在调用presentModalViewController的类中处理警报选项,所以最好的方法是

Anyway, I need the alert options to be handled within the class which calls the presentModalViewController, so what is the best way to go about it?

推荐答案

在我看来,最好的办法是实现一个协议,并以您的调用方类作为委托ScoreView,请尝试在您的scoreView头文件中实现:

In my opinion the best to do this is by implementing a protocol and suscribe your caller class as delegate of the scoreView, try implementing this in your scoreView header file:

@portocol ScoreViewDelegate <NSObject> 

- (void)userDidEnterData:(NSString *)data;

@end

@interface ScoreView:.... {
      // Any instance variable ...

      id <ScoreViewDelegate> delegate;  // delegate shouldn't be pointer
}

@property (assign) id <ScoreViewDelegate> delegate;

...
@end

在ScoreView类中实现中,您应该具有任何处理用户交互的方法,因此在完成插入数据后,该方法必须将消息发送给其委托:

Inside your ScoreView class implementation you should have any method that handles the user interactions so when they are done with inserting data the method must send the message to it's delegate:

@implementation ScoreView
@synthesize delegate;         // Don't forget to synthesize your delegate

- (IBAction)userIsDone:(id)sender {
       // Do some stuff

      [delegate userDidEnterData:self.aTextField.text];
}

...

@end

现在,此代码应将消息发送给调用方类,但应将其作为委托,因此您必须这样操作:

Now, this code should send the message to the caller class but it shoul be suscribed as delegate so you must do it like this:

@interface CallerClass:NSObject <SocreViewDelegate> {
...

@end

@implemetation CallerClass
...

     ScoreView *sView = ....;
     sView.delegate = self;

//Because it's now suscribed as delegate, it should implement the method
- (void)userDidEnterData:(NSString *)data {
       // Here you have the data
       // Present alert
}

我希望您觉得这对您的情况有用

I hope you found this useful to your case.

这篇关于通过presentModalViewController添加的视图将其解除与调用该调用的类进行通信的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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