navigationController'中的调用方法;从堆栈中的第三个ViewController中删除rootViewController-iOS
我的navigationController'中的调用方法;从堆栈中的第三个ViewController中删除rootViewController-iOS,ios,objective-c,uiviewcontroller,uinavigationcontroller,Ios,Objective C,Uiviewcontroller,Uinavigationcontroller,我的NavigationController层次结构如下: vc1->vc2->vc3 我想在撤销vc3时从vc3调用一个vc1方法 我试图实现委托,但由于vc1没有vc3对象,因此无法工作 (vc3是在从vc2开始的segue时创建的) 我尝试实现委托,但由于vc1没有vc3 对象,它不起作用 是的,但当您在v2的生命周期范围内创建v3时,您可以访问v1和v3(我想,您可以在-(void)prepareForSegue:(UIStoryboardSegue*)中将v1和v3链接在一起。 更新:
NavigationController
层次结构如下:
vc1->vc2->vc3
我想在撤销vc3时从vc3调用一个vc1方法
我试图实现委托,但由于vc1没有vc3对象,因此无法工作
(vc3是在从vc2开始的segue
时创建的)
我尝试实现委托,但由于vc1没有vc3
对象,它不起作用
是的,但当您在v2的生命周期范围内创建v3时,您可以访问v1和v3(我想,您可以在-(void)prepareForSegue:(UIStoryboardSegue*)中将v1和v3链接在一起。
更新:尝试使用self.navigationController.viewControllers[0]检索vc1。尝试以下代码:
创建三个视图控制器
视图控制器1
视图控制器2
视图控制器3
//视图控制器1.h
#import <UIKit/UIKit.h>
#import "ViewController2.h"
#import "ViewController3.h"
@interface ViewController1 : UIViewController <callDelegate>
@end
//ViewController2.h
#import <UIKit/UIKit.h>
#import "ViewController3.h"
@interface ViewController2 : UIViewController
@property(nonatomic,strong) UIViewController *myView;
@end
//ViewController3.h
#import <UIKit/UIKit.h>
@protocol callDelegate <NSObject>
-(void)delegateCalled;
@end
@interface ViewController3 : UIViewController
@property(nonatomic,weak)id<callDelegate>mydelegate;
@end
我基本上想这样做:vc3.delegate=self//vc1
,但这不能在vc1类中完成,因为没有vc3对象。按照你说的,我在vc2的prepareForSegue
中尝试了这个方法:vc2.delegate=self.parentViewController代码>但运气不好,vc1中未调用still delegate方法:(由于self.parentViewController为nil,您未将它们链接在一起。请尝试使用self.navigationController.ViewController[0]要检索vc1。另外,您能告诉我为什么self.parentViewController
为零吗?vc1不是vc2的父级吗?:SparentViewController
是指实际的navigationController
而不是其中的ViewController
?当然,请先看。父级视图控制器涉及到父级子级VC hierarchies,不是先前的继任者。让我们来回答。答案已经提供。请阅读整个帖子(问题和答案(如果有的话),然后添加你的答案。谢谢!
#import "ViewController2.h"
#import "ViewController3.h"
@interface ViewController2 ()
@end
@implementation ViewController2
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// ViewController3 *v3=[[ViewController3 alloc]init];
// self.myView.mydelegate=self;
[self.navigationController pushViewController:self.myView animated:YES];
UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
[self.view addSubview:lbl];
self.view.backgroundColor=[UIColor whiteColor];
lbl.backgroundColor=[UIColor redColor];
lbl.text=@"V2";
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
#import <UIKit/UIKit.h>
@protocol callDelegate <NSObject>
-(void)delegateCalled;
@end
@interface ViewController3 : UIViewController
@property(nonatomic,weak)id<callDelegate>mydelegate;
@end
#import "ViewController3.h"
@interface ViewController3 ()
@end
@implementation ViewController3
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIButton *lbl=[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
[self.view addSubview:lbl];
self.view.backgroundColor=[UIColor whiteColor];
lbl.backgroundColor=[UIColor redColor];
[lbl setTitle:@"v3" forState:UIControlStateNormal];
[lbl addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
}
-(void)viewDidDisappear:(BOOL)animated{
[self.mydelegate delegateCalled];
}
-(void)buttonClick{
[self.mydelegate delegateCalled];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end