从堆栈中的第三个ViewController调用navigationController的rootViewController中的方法 - iOS

时间:2015-10-09 08:33:37

标签: ios objective-c uiviewcontroller uinavigationcontroller

我的array_validate([$object, "method"], ["something"]); array_validate("is_array", [ [], [] ]); // true 层次结构如下:

vc1 - > vc2 - > VC3

我想在vc3被解雇时从vc3调用vc1方法。

我试图实现委托,但由于vc1没有vc3对象,所以它不起作用。

(vc3是在navigationControllers vc2时创建的)

2 个答案:

答案 0 :(得分:1)

  

我试图实现委托,但因为vc1没有vc3   对象,它不起作用。

是的,但是当您在v2生命周期的范围内创建v3时,您可以同时访问v1和v3(我想,您可以在- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender中将v1和v3链接在一起)。 更新:尝试使用self.navigationController.viewControllers [0]检索vc1。

答案 1 :(得分:0)

试试这段代码: 创建三个viewcontrollers ViewController1 ViewController2 ViewController3

// ViewController1.h

    #import <UIKit/UIKit.h>
    #import "ViewController2.h"
    #import "ViewController3.h"


    @interface ViewController1 : UIViewController <callDelegate>

    @end

// ViewController1.m

#import "ViewController1.h"
#import "ViewController2.h"

@interface ViewController1 ()

@end

@implementation ViewController1 

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.


    ViewController3 *v3=[[ViewController3 alloc]init];
    v3.mydelegate=self;


    ViewController2 *v2=[[ViewController2 alloc]init];
    v2.myView=v3;
    [self.navigationController pushViewController:v2 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=@"v1";



}

-(void)delegateCalled{
    NSLog(@"I am V1");
}


- (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

// ViewController2.h

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


@interface ViewController2 : UIViewController 
@property(nonatomic,strong) UIViewController *myView;
@end

// ViewController2.m

#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

// ViewController3.h

#import <UIKit/UIKit.h>
@protocol callDelegate <NSObject>

-(void)delegateCalled;

@end
@interface ViewController3 : UIViewController 
@property(nonatomic,weak)id<callDelegate>mydelegate;

@end

// ViewController3.m

#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