2017-09-18 17 views
-1

저는 Swift 서적을 사용하여 코드를 배우려고합니다. 뷰를 닫을 대리자 메서드를 추가했지만 작동하지 않습니다. 내가 여기서 무엇을 놓치고 있니?메일을 닫을 수있는 앱을 볼 수없는 경우보기 컨트롤러 작성 및 첫 번째 화면으로 돌아 가기

@IBAction func emailButtonTapped(_ sender: UIButton) { 

    if !MFMailComposeViewController.canSendMail() { 
     print("Can not send mail") 
     return 
    } 

    guard MFMailComposeViewController.canSendMail() else { return } 

    let mailComposer = MFMailComposeViewController() 
    mailComposer.mailComposeDelegate = self 

    mailComposer.setToRecipients(["[email protected]"]) 
    mailComposer.setSubject("Look at this") 
    mailComposer.setMessageBody("Hello, this is an email from the app I made.", isHTML: false) 

    present(mailComposer, animated: true, completion: nil) 

    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { 
     dismiss(animated: true, completion: nil) 
    } 

} 
당신은 외부에서 위임 기능을 선언 할 필요가
+3

'mailComposeController (_ 컨트롤러 : MFMailComposeViewController, didFinishWith 결과 : MFMailComposeResult, 오류 : 오류)'IBAction를 내부에 선언 그건 그냥 오타예요? 그것은 밖에 있어야합니다. – Samantha

+0

아 맞습니다. 나는 이것에 아주 새롭다 그래서 아직도 배우기. 고마워요 :) – shayaa

+0

@Samantha 답변으로 귀하의 의견을 추가 답변없이 대답을 피하십시오 –

답변

1

IBAction :

@IBAction func emailButtonTapped(_ sender: UIButton) { 
    if !MFMailComposeViewController.canSendMail() { 
     print("Can not send mail") 
     return 
    } 

    guard MFMailComposeViewController.canSendMail() else { return } 

    let mailComposer = MFMailComposeViewController() 
    mailComposer.mailComposeDelegate = self 

    mailComposer.setToRecipients(["[email protected]"]) 
    mailComposer.setSubject("Look at this") 
    mailComposer.setMessageBody("Hello, this is an email from the app I made.", isHTML: false) 

    present(mailComposer, animated: true, completion: nil) 
} 

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { 
    dismiss(animated: true, completion: nil) 
}