从 UIViewController 返回 NSString

发布于 2024-12-14 02:50:39 字数 289 浏览 0 评论 0原文

我想从一个名为 InputUIViewController 的 UIViewController 返回一个 NSString * 到之前启动了 InputUIViewController 的 UIViewController (名为 CallerUIViewController)。我想在 InputUIViewController 调用之前或调用时执行此操作:

[selfmissModelViewControllerAnimated:YES];

有标准方法可以执行此操作吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽袖吟 2024-12-21 02:50:39

执行此操作的标准方法是使用委托。

在您的 InputViewController 中添加一个新的委托协议以及您的委托的属性。

然后在您的 CallerUIViewController 中实现委托。然后,在您关闭模态视图控制器之前,您可以回调您的委托。

所以你的InputViewController可能看起来像这样:

@protocol InputViewControllerDelegate;

@interface InputViewControllerDelegate : UIViewController {
}

@property (nonatomic, assign) id <InputViewControllerDelegate> delegate;

@end


@protocol InputViewControllerDelegate
- (void)didFinishWithInputView:(NSString *)stringValue;
@end

关闭模式视图的方法看起来像这样:

-(void)dismissSelf
{
   [self.delegate didFinishWithInputView:@"MY STRING VALUE"];
   [self dismissModalViewControllerAnimated:YES];
}

然后在你的CallerUIViewController中你将实现InputViewControllerDelegate和didFinishWithInputView方法。

CallerUIViewController 标头看起来像:

@interface CallerUIViewController : UIViewController <InputViewControllerDelegate> {
}

并且您的 didFinishWithInputView 方法将实现类似:

- (void)didFinishWithInputView:(NSString *)stringValue
{
    // This method will be called by the InputViewController just before it is dismissed
}

在呈现 InputViewController 之前,您会将委托设置为 self。

-(void)showInputViewController
{
   InputViewController *inputVC = [[InputViewController alloc] init];
   inputVC.delegate = self;

   [self presentModalViewController:inputVC animated:YES];

   [inputVC release];
}

The standard way to do this would be to use a delegate.

In your InputViewController add a new delegate protocal, and a property for your delegate.

Then in your CallerUIViewController implement the delegate. Then just before your dismiss the modal view controller you can call back to your delegate.

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];
}

Then in your CallerUIViewController you would implement the InputViewControllerDelegate and the didFinishWithInputView method.

The CallerUIViewController header would look something like:

@interface CallerUIViewController : UIViewController <InputViewControllerDelegate> {
}

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
}

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];
}
云朵有点甜 2024-12-21 02:50:39

您可以通过简单地在前一个视图控制器中创建一个 NSString 对象作为 property 来完成此操作,然后在第二个视图中调用 dismissModelViewControllerAnimated 然后在它分配值之前到上一个视图控制器属性。这可能会帮助你 -

使用 Objective-C 在类之间传递数据

You can do this by simply creating a NSString object as property in prvious view controller and when in second view you call dismissModelViewControllerAnimated then before it assign value to previous view controller property. This might help you -

Passing data between classes using Objective-C

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文