【问题标题】:dismisscontroller delegate method not working解除控制器委托方法不起作用
【发布时间】:2018-04-21 04:10:23
【问题描述】:

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

firstVC.h

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

还包含委托属性

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

firstVC.m

firstVC 的后退按钮代码

- (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 不会解散,请查看我的代码并给我一个建议。

提前致谢。

【问题讨论】:

  • 我认为你的代表应该是weak而不是assign
  • @trungduc: 等等,我试试。不工作兄弟。
  • 我认为这是因为您的代表有问题。也许它是零。你能说明你在哪里设置委托吗?
  • 如您所见,我的代码。在 firstVC 中设置。
  • 不,我没看到。我需要类似self.myDelegat = secondVC

标签: ios objective-c delegates dismissviewcontroller


【解决方案1】:

firstVC不能被dismiss的原因是因为firstVC是从secondVC推送的,没有呈现出来。

要将数据从firstVc 传递到secondVc,您不需要使用委托。你可以使用self.navigationController.viewControllersfirstVC获取secondVC

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

【讨论】:

  • 这一行触发没有可见界面 [(SecondVC*)self.presentingViewController DVViewControllerDismissed:_DFCJ];
  • fire no visible interface 是什么意思?
  • 显示没有可见的界面
  • 更新了我的答案。
  • 如果firstVC是从secondVC呈现出来的,我敢肯定当你调用[self dismissViewControllerAnimated:YES completion:nil];时它会被解散
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-16
  • 2019-02-13
相关资源
最近更新 更多