Ios 需要有关在UINavigationcontroller中将数据从子级传递到父级的帮助吗
在UINavigationController中,这是子控制器Ios 需要有关在UINavigationcontroller中将数据从子级传递到父级的帮助吗,ios,objective-c,uinavigationcontroller,Ios,Objective C,Uinavigationcontroller,在UINavigationController中,这是子控制器 .h @protocol childProtocol <NSObject> -(void)childMethod:(NSArray*)params; @end @property (strong, nonatomic) id<childProtocol>childDelegate; @property (weak, nonatomic) parentVC *pVC; .m -(void)chil
.h
@protocol childProtocol <NSObject>
-(void)childMethod:(NSArray*)params;
@end
@property (strong, nonatomic) id<childProtocol>childDelegate;
@property (weak, nonatomic) parentVC *pVC;
.m
-(void)childMethod:(NSArray *)params {
// some work
}
...
childVC *cVC = [[childVC alloc]init];
cVC.pVC = self;
这是我的家长控制器
.h
@protocol childProtocol <NSObject>
-(void)childMethod:(NSArray*)params;
@end
@property (strong, nonatomic) id<childProtocol>childDelegate;
@property (weak, nonatomic) parentVC *pVC;
.m
-(void)childMethod:(NSArray *)params {
// some work
}
...
childVC *cVC = [[childVC alloc]init];
cVC.pVC = self;
但是childMethod:没有人打电话,所以我在网上搜索并得到了这篇文章
我试图创建一个弱引用,但不知道如何使代理将数据从子级传递到父级?这是child controller.h
@protocol childProtocol <NSObject>
-(void)childMethod:(NSArray*)params;
@end
@property (strong, nonatomic) id<childProtocol>childDelegate;
@property (weak, nonatomic) parentVC *pVC;
#import <UIKit/UIKit.h>
#import "ChildController.h"
@interface perentController : UIViewController < childProtocol >
这是我的家长控制器
@protocol childProtocol <NSObject>
-(void)childMethod:(NSArray*)params;
@end
@property (strong, nonatomic) id<childProtocol>childDelegate;
@property (weak, nonatomic) parentVC *pVC;
#import <UIKit/UIKit.h>
#import "ChildController.h"
@interface perentController : UIViewController < childProtocol >
编辑:
不要忘记添加childViewOBJ.childDelegate=self代码>在创建ChildViewController的
对象时。诸如此类
childVC *cVC = [[childVC alloc]init];
cVC.childDelegate = self;
cVC.pVC = self;
[self presentModalViewController:cVC animated:YES];
有关的详细信息,首先,您没有检查与您在协议声明中声明的选择器相同的选择器,因此它不会对此做出响应。您声明了方法childMethod:
,而您正在检查您的childDelegate是否响应myMethod:
选择器,该选择器不响应,因此它不会进入if条件
此外,父视图控制器在其.m中缺少方法childMethod:
的实现。在父视图控制器中实现它,否则它将由于找不到确切的选择器定义而崩溃
由于您使用的是UINavigationController
,因此在子视图控制器存在之前,父视图控制器不会丢失,因此childDelegate
属性不能很强,除非出于某种原因您打算在子视图控制器中保留代理。尝试此操作。检查所附的示例项目
ParentViewController.h
#import <UIKit/UIKit.h>
@interface ParentViewController : UIViewController
- (void)passData:(NSString *)strText;
@end
#import <UIKit/UIKit.h>
#import "ParentViewController.h"
@class ParentViewController;
@interface ChildViewController : UIViewController
@property(nonatomic, assign) ParentViewController *delegate;
@end
ChildViewController.h
#import <UIKit/UIKit.h>
@interface ParentViewController : UIViewController
- (void)passData:(NSString *)strText;
@end
#import <UIKit/UIKit.h>
#import "ParentViewController.h"
@class ParentViewController;
@interface ChildViewController : UIViewController
@property(nonatomic, assign) ParentViewController *delegate;
@end
为什么要复制粘贴我的问题作为答案,如果你是我没有做这个UIViewController的东西,那么这是错误的,我已经实现了这个。@S.J-首先检查一下,它和你的问题不完全一样。。我只是想帮你,没什么了好吧……)请再次检查我的问题,我更正了我的复制粘贴错误。@S.J只需按照我编辑的答案:)如果仍然存在问题,请按照我添加到答案中的链接:)我知道如何使用委托,并且在我的init方法中,我已经完成了这个self.cVC=[[childVC alloc]init];self.cVC.childDelegate=self;很抱歉,复制粘贴错误,在我的代码中,我调用了正确的选择器。请告诉我们您是如何将委托分配给父视图控制器的。@Zen ParentViewController我的意思是pVC属性如何符合子委托
。i、 e.如果存在类似于self.childDelegate=self.pVC的内容
或类似的东西告诉pVC
当childDelegate
被撤销时,它必须采取行动?@S.J请检查所附的示例。请告诉我们如何在uinavigationcontroller@S.J这与UINavigationViewControllerso相同,因此堆栈中的子对象不需要任何父对象的弱引用或强引用uinavigationcontroller?@S.J弱引用即可。