lundi 1 octobre 2018

Send checkbox data (with label) to MFMailComposeViewController

I want to ask a question about best way to use of checkbox at swift. I have a view, which has four checkboxes and independent labels.

What is the best way to take that label data to send an email if checked? if does not, I want to send N/A. at mail.

I want to pass checked labels data to MFMailComposer. Here is my sample code, any help would be appreciable.

    import MessageUI

class DailyTasksViewController: UIViewController, MFMailComposeViewControllerDelegate {

@IBOutlet weak var dailyTaskOne: UILabel!
@IBOutlet weak var dailyTaskTwo: UILabel!
@IBOutlet weak var dailyTaskThree: UILabel!
@IBOutlet weak var dailyTaskFour: UILabel!

var mailComposer: MFMailComposeViewController = MFMailComposeViewController()

override func viewDidLoad() {
    super.viewDidLoad()
    addCheckboxSubviews()
}

func addCheckboxSubviews() {   
    // tick1
    let tickBox1 = Checkbox(frame: CGRect(x: 16, y: 110, width: 29, height: 28))
    tickBox1.borderStyle = .square
    tickBox1.borderWidth = 5
    tickBox1.checkmarkStyle = .tick
    tickBox1.checkmarkSize = 0.7
    tickBox1.uncheckedBorderColor = UIColor ( red: CGFloat(234/255.0), green: CGFloat(100/255.0), blue: CGFloat(76/255.0), alpha: CGFloat(1.0) )
    tickBox1.checkedBorderColor = UIColor ( red: CGFloat(194/255.0), green: CGFloat(57/255.0), blue: CGFloat(32/255.0), alpha: CGFloat(1.0) )
    tickBox1.checkmarkColor = UIColor ( red: CGFloat(255/255.0), green: CGFloat(255/255.0), blue: CGFloat(255/255.0), alpha: CGFloat(1.0) )
    tickBox1.checkboxBackgroundColor = UIColor ( red: CGFloat(234/255.0), green: CGFloat(100/255.0), blue: CGFloat(76/255.0), alpha: CGFloat(1.0) )
    tickBox1.addTarget(self, action: #selector(tickBox1ValueChanged(sender:)), for: .valueChanged)
    view.addSubview(tickBox1)

    // tick2
    let tickBox2 = Checkbox(frame: CGRect(x: 16, y: 168, width: 29, height: 28))
    tickBox2.borderStyle = .square
    tickBox2.borderWidth = 5
    tickBox2.checkmarkStyle = .tick
    tickBox2.checkmarkSize = 0.7
    tickBox2.uncheckedBorderColor = UIColor ( red: CGFloat(234/255.0), green: CGFloat(100/255.0), blue: CGFloat(76/255.0), alpha: CGFloat(1.0) )
    tickBox2.checkedBorderColor = UIColor ( red: CGFloat(194/255.0), green: CGFloat(57/255.0), blue: CGFloat(32/255.0), alpha: CGFloat(1.0) )
    tickBox2.checkmarkColor = UIColor ( red: CGFloat(255/255.0), green: CGFloat(255/255.0), blue: CGFloat(255/255.0), alpha: CGFloat(1.0) )
    tickBox2.checkboxBackgroundColor = UIColor ( red: CGFloat(234/255.0), green: CGFloat(100/255.0), blue: CGFloat(76/255.0), alpha: CGFloat(1.0) )
    tickBox2.addTarget(self, action: #selector(tickBox2ValueChanged(sender:)), for: .valueChanged)
    view.addSubview(tickBox2)

    // tick3
    let tickBox3 = Checkbox(frame: CGRect(x: 16, y: 226, width: 29, height: 28))
    tickBox3.borderStyle = .square
    tickBox3.borderWidth = 5
    tickBox3.checkmarkStyle = .tick
    tickBox3.checkmarkSize = 0.7
    tickBox3.uncheckedBorderColor = UIColor ( red: CGFloat(234/255.0), green: CGFloat(100/255.0), blue: CGFloat(76/255.0), alpha: CGFloat(1.0) )
    tickBox3.checkedBorderColor = UIColor ( red: CGFloat(194/255.0), green: CGFloat(57/255.0), blue: CGFloat(32/255.0), alpha: CGFloat(1.0) )
    tickBox3.checkmarkColor = UIColor ( red: CGFloat(255/255.0), green: CGFloat(255/255.0), blue: CGFloat(255/255.0), alpha: CGFloat(1.0) )
    tickBox3.checkboxBackgroundColor = UIColor ( red: CGFloat(234/255.0), green: CGFloat(100/255.0), blue: CGFloat(76/255.0), alpha: CGFloat(1.0) )
    tickBox3.addTarget(self, action: #selector(tickBox3ValueChanged(sender:)), for: .valueChanged)
    view.addSubview(tickBox3)

    // tick4
    let tickBox4 = Checkbox(frame: CGRect(x: 16, y: 284, width: 29, height: 28))
    tickBox4.borderStyle = .square
    tickBox4.borderWidth = 5
    tickBox4.checkmarkStyle = .tick
    tickBox4.checkmarkSize = 0.7
    tickBox4.uncheckedBorderColor = UIColor ( red: CGFloat(234/255.0), green: CGFloat(100/255.0), blue: CGFloat(76/255.0), alpha: CGFloat(1.0) )
    tickBox4.checkedBorderColor = UIColor ( red: CGFloat(194/255.0), green: CGFloat(57/255.0), blue: CGFloat(32/255.0), alpha: CGFloat(1.0) )
    tickBox4.checkmarkColor = UIColor ( red: CGFloat(255/255.0), green: CGFloat(255/255.0), blue: CGFloat(255/255.0), alpha: CGFloat(1.0) )
    tickBox4.checkboxBackgroundColor = UIColor ( red: CGFloat(234/255.0), green: CGFloat(100/255.0), blue: CGFloat(76/255.0), alpha: CGFloat(1.0) )
    tickBox4.addTarget(self, action: #selector(tickBox4ValueChanged(sender:)), for: .valueChanged)
    view.addSubview(tickBox4)
}

// target action example
@objc func tickBox1ValueChanged(sender: Checkbox) {
    //print("Tick box value change: \(sender.isChecked)")
    //print(dailyTaskOne.text as Any, (sender.isChecked))

    if sender.isChecked == true {
        print("ok")
    } else {
        print("nah")
    }
}

@objc func tickBox2ValueChanged(sender: Checkbox) {
    //print("Tick box value change: \(sender.isChecked)")
}

@objc func tickBox3ValueChanged(sender: Checkbox) {
    //print("Tick box value change: \(sender.isChecked)")
}

@objc func tickBox4ValueChanged(sender: Checkbox) {
    //print("Tick box value change: \(sender.isChecked)")
}

@IBAction func sendEmailClicked(_ sender: AnyObject) {

    let dateString: String = DateFormatter.localizedString(from: Date(), dateStyle: .short, timeStyle: .short)

    mailComposer = MFMailComposeViewController()
    mailComposer.mailComposeDelegate = self

    mailComposer.setToRecipients(["acme@mail.com"])
    mailComposer.setSubject("\(dateString) Daily email")
    mailComposer.setMessageBody("lorem ipsum dolor sit amet.",  isHTML: true)

    self.present(mailComposer, animated: true) {}
}

}




Aucun commentaire:

Enregistrer un commentaire