代码之家  ›  专栏  ›  技术社区  ›  Sjero Markus

消息表单上取消后崩溃

  •  0
  • Sjero Markus  · 技术社区  · 7 年前

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

    以下是整个代码:

    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\u BAD\u访问(代码=1,地址=0x5a1b7831c08)

    好像我忘了什么。

    1 回复  |  直到 7 年前
        1
  •  1
  •   PoolHallJunkie    7 年前

    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传递信息。