在消息表单上取消后崩溃

时间:2017-11-07 14:25:24

标签: swift xcode email view sms

我想通过我的应用程序发送消息 - 一切都像它应该的那样,除非用户使用&#34取消屏幕;取消"在右上角,应用程序将变黑并崩溃。

我认为它与视图有关,因此App View不会回来,但我不知道如何以及为什么。我已经制作了断点,但应用程序在此之前崩溃了。

以下是整个代码:

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")
}
}

如果我点击右上方的取消按钮,我将在AppDelegate文件中收到此错误:

  

线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x5a1b7831c08)

好像我忘记了什么。

1 个答案:

答案 0 :(得分:1)

UITableViewCell不应对此类操作负责,单元格应发送通知,或者应使用委托方法连接。然后UIViewController应该执行这些操作。

new PDO

并在cellForRow添加:

protocol RemindUserDelegate: class {
      func buttonPressed()
}

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

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

并在单元格上执行:cell.delegate = self

然后UIViewController可以从那里获取它。您还可以使用userData传递信息。