2017-11-07 11 views
0

私のアプリからメッセージを送信したい - ユーザーが画面を中止して右上の「キャンセル」をクリックするとアプリが黒くなり、クラッシュします。メッセージフォームのキャンセル後にクラッシュする

私はそれがビューとは関係があると思うので、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

TableViewCell/ – PoolHallJunkie

+0

ではなく、UIViewControllerにMFMailComposeViewControllerDelegateを移動しようとしましたが、失敗しました...私は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

もう一度大きな大きな感謝! –

関連する問題