我有2个viewcontroller,第一个VC包含backButton。

firstVC.h

@protocol DVDelegate <NSObject>
-(void)DVViewControllerDismissed:(NSString *)stringForFirst;
@end


并且还包含委托属性

@property (nonatomic, assign) id<DVDelegate> myDelegat;


firstVC.m

firstVC的backButton代码

- (IBAction)backButton_Click:(id)sender {
    NSLog(@"EEEEEE:%@",_DFCJ);
    if([_DFCJ isEqual:@"DL"]){
        NSLog(@"159");
        if([self.myDelegat respondsToSelector:@selector(DVViewControllerDismissed:)])
        {
            [self.myDelegat DVViewControllerDismissed:_DFCJ];//this method not call
            NSLog(@"aPP");
        }
        [self dismissViewControllerAnimated:YES completion:nil];
        NSLog(@"5555");
    }
}



SecondVC.m

@interface DiamondListVC ()<DVDelegate>


接收以下方法

-(void)DVViewControllerDismissed:(NSString *)stringForFirst;{
    NSLog(@"AASASS");
   [self performSelector:@selector(callService) withObject:self afterDelay:0.1];
}


结果记录

2018-04-21 09:33:39.382 search[907:16573] EEEEEE:DL
2018-04-21 09:33:39.382 search[907:16573] 159
2018-04-21 09:33:39.383 search[907:16573] 5555


但firstVC不会拒绝,请查看我的代码并提出建议。

提前致谢。

最佳答案

无法显示firstVC的原因,因为firstVC是从secondVC推入的,因此未显示。

要将数据从firstVc传递到secondVc,无需使用委托。您可以使用secondVCfirstVC中获取self.navigationController.viewControllers

FirstVC.m

- (IBAction)backButton_Click:(id)sender {
  NSLog(@"EEEEEE:%@",_DFCJ);
  if([_DFCJ isEqual:@"DL"]){
    NSLog(@"159");

    // Get |SecondVC| by using |self.navigationController.viewControllers|
    NSArray* viewControllers = [[self navigationController] viewControllers];
    SecondVC *previousViewController = viewControllers[viewControllers.count - 2];
    [previousViewController DVViewControllerDismissed:_DFCJ];

    [self.navigationController popViewControllerAnimated:YES];
    NSLog(@"5555");
  }
}


SecondVC.h

@interface DiamondListVC : YourClass

-(void)DVViewControllerDismissed:(NSString *)stringForFirst;

@end

关于ios - dismisscontroller委托(delegate)方法不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49952269/

10-11 20:56