从UIViewController返回NSString [英] Return NSString from UIViewController

查看:336
本文介绍了从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];

有没有一种标准方法可以做到这一点?

Is there a standard way to do this?

推荐答案

标准方式这样做是使用委托。

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之前,您将委托设置为自我。

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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