【问题标题】:Unable to assign delegate in global method无法在全局方法中分配委托
【发布时间】:2026-02-23 21:55:01
【问题描述】:

我有一个单例,我在其中创建了一个自定义方法,供多个视图控制器使用。方法是显示一个电子邮件作曲家。

-(void)emailSend:(NSString*)bodyStr inVC:(UIViewController*)vc {

    if ([MFMailComposeViewController canSendMail]) {

        NSString *messageBody =  bodyStr;
        MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
        mc.mailComposeDelegate = vc; // <-- warning
        [mc setSubject:@"Say Hello"];
        [vc presentViewController:mc animated:YES completion:NULL];
    }else{

        // Not setup
    }

}

在其他视图控制器上,我称之为:

[[MySingle singleton] emailSend:@"Testing" inVc:self];

警告消息分配给

id __Nullable 从不兼容 输入 UIViewController *__strong

有什么办法让它工作吗?

【问题讨论】:

  • 嗨,亲爱的,你能试试这个吗:__weak typeof(self)weakSelf = self; [[MySingle 单例] emailSend:@"Testing" inVc:weakSelf]; 将 self 替换为 classname(尝试使用强和弱)
  • 您需要将委托添加到您的 VC 类 MyVC:
  • 更改为:** nVC:(id)vc**。
  • @KKRocks 感谢它有效。如果你写一个答案,我会接受它。

标签: ios objective-c delegates singleton


【解决方案1】:

您需要对您的方法进行一些更改:

发件人:

-(void)emailSend:(NSString*)bodyStr inVC:(UIViewController*)vc 

收件人:

-(void)emailSend:(NSString*)bodyStr inVC:(id)vc 

【讨论】: