我认为这与视图有关,因此应用程序视图不会回来,但我不知道如何以及为什么。我已经设置了断点,但在此之前应用程序崩溃了。
以下是整个代码:
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?
@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()
}
let remindByNumber = UIAlertAction(title: "Phone Message", style: UIAlertActionStyle.default) {
UIAlertAction in
self.sendSMS()
}
let cancelRemind = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
UIAlertAction in
print("cancel")
}
if(userNumber != ""){
alert.addAction(remindByNumber)
}
if(userEmail != ""){
alert.addAction(remindByEmail)
}
alert.addAction(cancelRemind)
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)
好像我忘了什么。