Return NSString from UIViewController(从 UIViewController 返回 NSString)
问题描述
我想从一个名为 InputUIViewController 的 UIViewController 返回一个 NSString *
到之前的 UIViewController,称为 CallerUIViewController,它启动了 InputUIViewController.我想在 InputUIViewController 调用之前或调用时这样做:
I want to return a NSString *
from a UIViewController, called InputUIViewController, to the previous UIViewController, called CallerUIViewController, which started InputUIViewController. I want to do it just before or when InputUIViewController calls:
[self dismissModelViewControllerAnimated:YES];
有标准的方法吗?
推荐答案
执行此操作的标准方法是使用委托.
The standard way to do this would be to use a delegate.
在您的 InputViewController 中添加一个新的委托协议,以及您的委托的一个属性.
In your InputViewController add a new delegate protocal, and a property for your delegate.
然后在您的 CallerUIViewController 中实现委托.然后就在您关闭模态视图控制器之前,您可以回调您的委托.
Then in your CallerUIViewController implement the delegate. Then just before your dismiss the modal view controller you can call back to your delegate.
所以你的 InputViewController 可能看起来像这样:
So your InputViewController might look like this:
@protocol InputViewControllerDelegate;
@interface InputViewControllerDelegate : UIViewController {
}
@property (nonatomic, assign) id <InputViewControllerDelegate> delegate;
@end
@protocol InputViewControllerDelegate
- (void)didFinishWithInputView:(NSString *)stringValue;
@end
关闭模态视图的方法如下所示:
The method that dismisses the modal view would look something like this:
-(void)dismissSelf
{
[self.delegate didFinishWithInputView:@"MY STRING VALUE"];
[self dismissModalViewControllerAnimated:YES];
}
然后在 CallerUIViewController 中实现 InputViewControllerDelegate 和 didFinishWithInputView 方法.
Then in your CallerUIViewController you would implement the InputViewControllerDelegate and the didFinishWithInputView method.
CallerUIViewController 标头类似于:
The CallerUIViewController header would look something like:
@interface CallerUIViewController : UIViewController <InputViewControllerDelegate> {
}
您的 didFinishWithInputView 方法将被实现为:
and your didFinishWithInputView method would be implemented something like:
- (void)didFinishWithInputView:(NSString *)stringValue
{
// This method will be called by the InputViewController just before it is dismissed
}
就在您呈现 InputViewController 之前,您需要将委托设置为 self.
Just before your present the InputViewController you would set the delegate to self.
-(void)showInputViewController
{
InputViewController *inputVC = [[InputViewController alloc] init];
inputVC.delegate = self;
[self presentModalViewController:inputVC animated:YES];
[inputVC release];
}
这篇关于从 UIViewController 返回 NSString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 UIViewController 返回 NSString
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01