gpt4 book ai didi

ios - 是否可以在 dismissmodelviewcontroller 时返回一个值

转载 作者:行者123 更新时间:2023-11-28 19:00:27 25 4
gpt4 key购买 nike

我想在关闭 Model View Controller 时将一个值返回到前一个 View 。我不想使用通知或将值设置为全局字符串,我想做这样的事情。

 NSString *Val=[self presentModalViewController:ViewController animated:YES completion:nil];

并且在关闭它时应该返回值。

有什么办法吗?提前致谢。

最佳答案

按照步骤创建自定义协议(protocol)。

ViewController.h

@protocol textSendProtocol<NSObject>
-(void)sendText:(NSString *)strText;
@end
@interface ViewController : UIViewController
@property(nonatomic,strong)id <textSendProtocol> delegate;
@end

ViewController.m

if ([self.delegate respondsToSelector:@selector(sendText:)])
[self.delegate sendText:@"yourText"];
[self dismissViewControllerAnimated:YES completion:nil];

在您呈现 ViewController 的其他 View Controller 中说 ExampleVC

@interface ExampleVC ()<textSendProtocol>
@end


ViewController *obj = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
obj.delegate = self;
[self presentViewController:obj animated:YES completion:^{}];

-(void)sendText:(NSString *)strText
{
// you wil get text here.
NSLog(@"%@",strText);
}

也许这对你有帮助。

关于ios - 是否可以在 dismissmodelviewcontroller 时返回一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26420756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com