gpt4 book ai didi

ios - 需要有关在 UINavigationcontroller 中将数据从 child 传递给 parent 的帮助

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:17:51 25 4
gpt4 key购买 nike

在 UINavigationController 中这是子 Controller

.h

@protocol childProtocol <NSObject>

-(void)childMethod:(NSArray*)params;

@end

@property (strong, nonatomic) id<childProtocol>childDelegate;

@property (weak, nonatomic) parentVC *pVC;

.m 

if([self.childDelegate respondsToSelector:@selector(childMethod:)]) {

[self.childDelegate performSelector:@selector(childMethod:) withObject:self.arry];

}

这是我的父 Controller

.m

-(void)childMethod:(NSArray *)params {
// some work
}

...

childVC *cVC = [[childVC alloc]init];
cVC.pVC = self;

但是 childMethod: 没有被调用所以我在互联网上搜索并得到了这篇文章 UINavigationControllers: How to pass value to higher (parent?) controller in stack?

我试图创建一个弱引用,但不知道如何使用它来让委托(delegate)将数据从子对象传递到父对象?

最佳答案

试试这个。检查附件中的示例项目

ParentViewController.h

#import <UIKit/UIKit.h>

@interface ParentViewController : UIViewController

- (void)passData:(NSString *)strText;

@end

ParentViewController.m

- (IBAction)btnGoToSecondView:(id)sender {
ChildViewController *secondVC = [[ChildViewController alloc] initWithNibName:@"ChildViewController" bundle:nil];
secondVC.delegate = self;
[self presentViewController:secondVC animated:YES completion:nil];

}

- (void)passData:(NSString *)strText {
NSLog(@"Data Passed = %@",strText);
}

ChildViewController.h

#import <UIKit/UIKit.h>
#import "ParentViewController.h"

@class ParentViewController;

@interface ChildViewController : UIViewController

@property(nonatomic, assign) ParentViewController *delegate;

@end

ChildViewController.m

- (IBAction)btnPassDataBack:(id)sender {
if([self.delegate respondsToSelector:@selector(passData:)]) {
[self.delegate passData:@"Hello"];
}
[self dismissViewControllerAnimated:YES completion:nil];
}

Sample Project

关于ios - 需要有关在 UINavigationcontroller 中将数据从 child 传递给 parent 的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17205224/

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