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
基础教程推荐
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
