2017-11-07 12 views
0

내 앱을 통해 메시지를 보내려고합니다. 사용자가 화면 오른쪽 상단의 "취소"를 취소하면 앱이 검은 색으로 표시되고 충돌.메시지 양식 취소 후 오류 발생

내 생각에는 뷰와 관련이있어서 앱 뷰가 다시 돌아 오지 않을 것이라고 생각합니다.하지만 어떻게 그리고 왜 모릅니다. 내가 중단 점을 만들었지 만, 응용 프로그램이 충돌하기 전에. 내가 오른쪽 상단에있는 취소 버튼을 누르면 경우 내가 AppDelegate에 파일이 오류를 얻을 것이다,

import UIKit 
import MessageUI 

class LoanTableCell: UITableViewCell, UIAlertViewDelegate, UINavigationControllerDelegate, MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate { 

let storyboard = UIStoryboard(name: "Main", bundle: nil) 

var imageData: Data? 
var userEmail: String? 
var userNumber: String? 

var popUp: UIView? 

// Label Outlets 
@IBOutlet weak var nameLabel: UILabel! 
@IBOutlet weak var amountLabel: UILabel! 
@IBOutlet weak var currencyLabel: UILabel! 
@IBOutlet weak var dateLabel: UILabel! 
@IBOutlet weak var noteLabel: UILabel! 
@IBOutlet weak var dueLabel: UILabel! 
@IBOutlet weak var moreInfos: UIView! 
@IBOutlet weak var showImageButton: UIButton! 
@IBOutlet weak var remindButton: UIButton! 

@IBAction func remindUser(_ sender: Any) { 
    let alert: UIAlertController = UIAlertController(title: "remind by:", message: nil, preferredStyle: .actionSheet) 
    let view = storyboard.instantiateViewController(withIdentifier: "LoanView") as! LoanViewController 
    let appDelegate = UIApplication.shared.delegate as! AppDelegate 

    let remindByEmail = UIAlertAction(title: "E-Mail", style: UIAlertActionStyle.default) { 
     UIAlertAction in 
     self.sendEmail() 
    } // Email senden.... 
    let remindByNumber = UIAlertAction(title: "Phone Message", style: UIAlertActionStyle.default) { 
     UIAlertAction in 
     self.sendSMS() 
    } // SMS senden... 
    let cancelRemind = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) { 
     UIAlertAction in 
     print("cancel") 
    } // Abbrechen 

    //Aktionen ausführen 
    if(userNumber != ""){ 
    alert.addAction(remindByNumber) 
    } 
    if(userEmail != ""){ 
    alert.addAction(remindByEmail) 
    } 
    alert.addAction(cancelRemind) 
    //Controller anzeigen 
    appDelegate.window?.rootViewController = view 
    view.present(alert, animated: true, completion: nil) 
} 




@IBAction func showImage(_ sender: Any) { 

    if (imageData?.isEmpty == false) { 
     popUp = UIView(frame: CGRect(x: 0, y: 20, width: (window?.frame.width)!, height: (window?.frame.height)!)) 
     popUp?.backgroundColor = UIColor.black 
     window?.addSubview(popUp!) 
     popUp?.isHidden = false 


     let imageView = UIImageView(frame: CGRect(x: 15, y: 15, width: ((window?.frame.maxX)! - 30), height: ((window?.frame.maxY)! - 50))) 
     imageView.image = UIImage(data: imageData!) 
     imageView.contentMode = .scaleAspectFit 
     popUp?.addSubview(imageView) 

     let closeButton = UIButton(frame: CGRect(x: ((window?.frame.maxX)! - 40), y: 5, width: 35, height: 35)) 
     closeButton.setImage(imageLiteral(resourceName: "closeImage"), for: .normal) 
     closeButton.addTarget(self, action: #selector(closeImageView), for: .touchUpInside) 
     popUp?.addSubview(closeButton) 
    } 
} 

@objc func closeImageView() { 
    popUp?.isHidden = true 
} 








func sendEmail() { 
    let view = storyboard.instantiateViewController(withIdentifier: "LoanView") as! LoanViewController 
    let appDelegate = UIApplication.shared.delegate as! AppDelegate 


    if MFMailComposeViewController.canSendMail() { 
     let reminderMessage: String = „Some Text„ 

     let mail = MFMailComposeViewController() 
     mail.mailComposeDelegate = self 
     mail.setToRecipients([userEmail!]) 
     mail.setSubject("Urgent Loan-Reminder") 
     mail.setMessageBody(reminderMessage, isHTML: true) 

     appDelegate.window?.rootViewController = view 
     view.present(mail, animated: true, completion: nil) 

    } else { 
     let alert = UIAlertController(title: "No E-Mail Account found...", message: "to send E-Mails, you have to configure at least one E-Mail account on your Device.", preferredStyle: UIAlertControllerStyle.alert) 
     alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil)) 
     appDelegate.window?.rootViewController = view 
     view.present(alert, animated: true, completion: nil) 
    } 
} 

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

func sendSMS() { 
    let view = storyboard.instantiateViewController(withIdentifier: "LoanView") as! LoanViewController 
    let appDelegate = UIApplication.shared.delegate as! AppDelegate 


    if MFMessageComposeViewController.canSendText() { 
     let reminderMessage: String = „Some Text„ 

     let message = MFMessageComposeViewController() 
     message.messageComposeDelegate = self 
     message.recipients = [userNumber!] 
     message.body = reminderMessage 


     appDelegate.window?.rootViewController = view 
     view.present(message, animated: true, completion: nil) 

    } else { 
     let alert = UIAlertController(title: "No Message Account...", message: "to send Messages, you need a Phone account.", preferredStyle: UIAlertControllerStyle.alert) 
     alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil)) 
     appDelegate.window?.rootViewController = view 
     view.present(alert, animated: true, completion: nil) 
    } 
} 

func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) { 
    controller.dismiss(animated: true, completion: nil) 
    print("message sent") 
} 
} 

:

스레드 1 : 여기

이 전체 코드 EXC_BAD_ACCESS (코드 = 1, 주소 = 0x5a1b7831c08)

나는 뭔가를 잊어 버린 것처럼 보입니다.

+1

TableViewCell/ – PoolHallJunkie

+0

보다는 MFMailComposeViewControllerDelegate를 UIViewController로 이동해보십시오.하지만 실패했는데 ... sendSMS 및 sendEmail func도 이동해야합니까? 죄송합니다. 이해하지 못합니다./ –

+1

예 셀에서 이러한 작업을 담당해서는 안됩니다. 셀은 단추를 누른 대리자를 사용하여 알림을 보내야합니다. 그런 다음 viewController가 나머지 작업을 수행해야합니다. – PoolHallJunkie

답변

1

UITableViewCell이 이러한 동작을 담당해서는 안되며, 셀이 알림을 보내거나 위임 메서드와 연결되어야합니다. 그런 다음 UIViewController는 이러한 작업을 수행해야합니다.

protocol RemindUserDelegate: class { 
     func buttonPressed() 
} 

class LoanTableCell: UITableViewCell { 
     weak var delegate: RemindUserDelegate? 
} 

class ViewController: UIViewController, RemindUserDelegate,MFMailComposeViewControllerDelegate, MFMessageComposeVie wControllerDelegate { 
     func buttonPressed() { 
     } 
} 

및 cellForRow 추가 기능에서

는 :
cell.delegate = self 

셀에

수행 :

self.delegate?.buttonPressed()는 다음의 UIViewController는 거기에서 걸릴 수 있습니다. userData를 사용하여 정보를 전달할 수도 있습니다.

+0

다시 큰 큰 감사! –