从 UIViewController 返回 NSString

Return NSString from UIViewController(从 UIViewController 返回 NSString)

本文介绍了从 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

基础教程推荐