How do I compose an email through Swift with multiple UITextField? It seems like I can only enter one data (named UITextField) under messageBody. How do can I add multiple UITextField to my messageBody?
import UIKit
import CoreData
import MessageUI
class EmailTableViewController: UITableViewController, MFMailComposeViewControllerDelegate, UITextFieldDelegate {
#IBOutlet weak var name: UITextField!
#IBOutlet weak var phone: UITextField
#IBOutlet weak var email: UITextField!
#IBOutlet weak var base: UITextField!
#IBOutlet weak var rig: UITextField!
#IBOutlet weak var wellhead: UITextField!
#IBOutlet weak var connector: UITextField!
#IBOutlet weak var size: UITextField!
#IBOutlet weak var depth: UITextField!
#IBOutlet weak var pressure: UITextField!
#IBOutlet weak var temp: UITextField!
#IBAction func SendEmailButton(sender: AnyObject) {
var emailTitle = "Interface Information"
var messageBody = name.text
var toRecipents = ["test.com"]
var mc: MFMailComposeViewController = MFMailComposeViewController()
mc.mailComposeDelegate = self
mc.setSubject(emailTitle)
mc.setMessageBody(messageBody, isHTML: false)
mc.setToRecipients(toRecipents)
self.presentViewController(mc, animated: true, completion: nil)
}
func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError) {
switch result.value {
case MFMailComposeResultCancelled.value:
println("Mail cancelled")
case MFMailComposeResultSaved.value:
println("Mail saved")
case MFMailComposeResultSent.value:
println("Mail sent")
case MFMailComposeResultFailed.value:
println("Mail sent failure: %#", [error.localizedDescription])
default:
break
}
self.dismissViewControllerAnimated(true, completion: nil)
}
You can just create a little helper function and put the fields in an array, like this:
func appendTextFromTextField(string: String, textField: UITextField) -> String {
return string + textField.text + "\n"
}
#IBAction func SendEmailButton(sender: AnyObject) {
var fields: [UITextField] = [name, phone, email, base, rig, wellhead, connector,
size, depth, pressure, temp]
var messageBody = ""
for f in fields {
messageBody = appendTextFromTextField(messageBody, textField: f)
}
// etc.
}
Related
It takes data in my #Published variable in CarDetailViewModel, but I could not pass the data inside this variable to the variable in the ViewController.
ViewModel:
class CarDetailViewModel: ObservableObject {
#Published var carDetail: Car = Car(brand: "", features: CarFeatures(model: "", km: 0, year: 0, price: 0, image: "", gearType: "", fuelType: "", carVersion: ""))
private var anyCancellable = Set<AnyCancellable>()
var carSubject = PassthroughSubject<Car, Never>()
func prepareCarDetail() {
carSubject
.sink {[weak self] car in
self?.carDetail = car
print("car: \(self?.carDetail)") // it is working.
}
.store(in: &anyCancellable)
}
}
ViewController:
When the assignmentDataToUI function runs, the label on the screen is blank.
class CarDetailViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var brandLabel: UILabel!
#IBOutlet weak var modelLabel: UILabel!
#IBOutlet weak var gearTypeLabel: UILabel!
#IBOutlet weak var fuelTypeLabel: UILabel!
#IBOutlet weak var priceLabel: UILabel!
var carDetailViewModel = CarDetailViewModel()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
carDetailViewModel.prepareCarDetail()
assignmentDataToUI()
}
func assignmentDataToUI() {
brandLabel.text = carDetailViewModel.carDetail.brand
}
}
I listened to carDetail in ViewModel with sink.
carDetailViewModel.$carDetail
.sink {[weak self] car in
guard let self = self else { return }
self.assignmentDataToUI()
}
.store(in: &anyCancelable)
I'm adding a new TableVIewto the project and I'm also creating a custom class fro the cell. I'm doing as usual : New file/ Cocoa Touch Class / UITableViewCell / name. As soon as I start adding properties I get the error dough properties are declared as!. It doesn't happen on my other custom cell class. Can you see what am I doing wrong with this new class?
No error on this class :
import UIKit
class CalendarTableViewCell: UITableViewCell {
#IBOutlet weak var dayLabel: UILabel!
var cellId: String!
var cellWeekday: Int!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func prepareForReuse() {
super.prepareForReuse()
// Set your default background color, title color etc
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
and the new class that makes xCode complain:
import UIKit
class ProductTableViewCell: UITableViewCell {
#IBOutlet weak var productImageView: UIImageView!
#IBOutlet weak var productIDLabel: UILabel!
#IBOutlet weak var productIDInfoLabel: UILabel!
#IBOutlet weak var categoryLabel: UILabel!
#IBOutlet weak var categoryInfoLabel: UILabel!
#IBOutlet weak var nameLabel: UILabel!
#IBOutlet weak var nameInfoLabel: UILabel!
#IBOutlet weak var priceLabel: UILabel!
#IBOutlet weak var priceInfoLabel: UILabel!
#IBOutlet weak var quantityLabel: UILabel!
#IBOutlet weak var quantityInfoLabel: UILabel!
var productImage: UIImage!
var category: String!
var productId: String!
var name: String!
var price: String
var vendor: String!
var cellId: Int64
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
It's something I overlooked for sure but I can't spot it.
What should I check?
Your var cellId: Int64 is not initialized. In first cell you explicitly specified that you will initialize it before use with exclamation mark, but not in the second cell.
I have a form I am creating
this form gets filled with textfields the user inputs. After answering all the questions a button pops up to save.
I am having a problem making this tableviewcontroller to pass the data to a new tableviewcontroller. I'm stuck and not sure how to go about this.
import UIKit
class TableViewController: UITableViewController, UITextFieldDelegate {
#IBOutlet weak var saveBtn: UIButton!
#IBOutlet var firstNameField: UITextField!
#IBOutlet var middleNameField: UITextField!
#IBOutlet weak var lastNameField: UITextField!
#IBOutlet weak var addressField: UITextField!
#IBOutlet weak var aptNumField: UITextField!
#IBOutlet weak var cityField: UITextField!
#IBOutlet weak var stateField: UITextField!
#IBOutlet weak var zipField: UITextField!
#IBOutlet weak var phoneOneField: UITextField!
#IBOutlet weak var phoneTwoField: UITextField!
#IBOutlet weak var allergiesField: UITextField!
#IBOutlet weak var DobField: UILabel!
#IBOutlet weak var sexField: UILabel!
#IBOutlet weak var hospitalField: UITextField!
#IBOutlet weak var doctorField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
//Notifications to push datepicker
NotificationCenter.default.addObserver(forName: .saveDateTime, object: nil, queue: OperationQueue.main) { (notification) in
let dateVc = notification.object as! DatePopupViewController
self.DobField.text = dateVc.formattedDate
}
//Notifications to push genderpicker
NotificationCenter.default.addObserver(forName: .saveGender, object: nil, queue: OperationQueue.main) { (notification) in
let genderVc = notification.object as! GenderPopupViewController
self.sexField.text = genderVc.selectedGender
}
updateWidthsForLabels(labels: labels)
}
//Save Button Function
func textFieldDidChange(_ textField: UITextField) {
if textField == firstNameField || textField == lastNameField || textField == middleNameField || textField == addressField || textField == lastNameField || textField == cityField || textField == cityField || textField == stateField || textField == zipField || textField == phoneOneField || textField == phoneTwoField || textField == allergiesField {
saveBtn.isHidden = true
} else {
saveBtn.isHidden = false
}
}
#IBAction func saveBtnPressed(_ sender: Any) {
performSegue(withIdentifier: "saveFirstPageSegue", sender: self)
}
}
what about starting creating a model:
Form.swift
struct Form {
var firstname: String?
var middlename: String?
....
var doctor: String?
init(firstname: String, middlename: String, ..., doctor: String) {
self.firstname = firstname
self.middlename = middlename
...
self.doctor = doctor
}
}
now you can create this form instance when saving and pushing the data to the new VC:
yourCurrentForm.swift
#IBAction func saveBtnPressed(_ sender: Any) {
let formData = Form(firstname: firstNameField.text, middlename: middleNameField.text, ..., doctor: doctorField.text)
let newVC = myNewViewController()
newVC.form = formData
self.navigationController?.pushViewController(newVC, animated: true)
}
NewViewController.swift
class myNewViewController: UIViewController {
var form: Form?
.....
}
UPDATE:
Here is the repo: https://github.com/FlorianLdt/LFEasyDelegate
If you have some question just ask me
Hope it helps.
First Option - Structs - Preferred
Make use of Structs :
struct Manager
{
static var value : String = ""
}
Noe Update value of that function by just calling
Manager.value = "newValue"
Access that value anywhere Assign it to other Variables
let newStr : String = Manager.value
Second Option - AppDelegate - Not ideal
Create new object in AppDelegate
Now create a new object to access appDelegate
let appDel = UIApplication.shared.delegate as! AppDelegate
Access Value and update as below
appDel.Frequency = 1.0
Third Option - NSObjectClass
Create a new NSObject class as below
//Instance created when NSObject class is first time loaded in memory stack
static let shared = wrapperClass()
//Create a value to access Globally in Object class
var newValueInClass : String = ""
Now time to access that created Object
wrapperClass.shared.newValueInClass = "iosGeek"
Now Anywhere write this Line
print(wrapperClass.shared.newValueInClass)
Console Output
Better to use struct classes to manage data globally
I have created the following view controller and wired up the controls in my storyboard to it.
import Foundation
import CoreData
import AppKit
protocol SubmissionViewControllerDelegate {
func controller(_controller: SubmissionViewController,
didAddSubmissionWithID profileID: Int64)
}
class SubmissionViewController : ViewController {
#IBOutlet var profileIDComboBox: NSPopUpButton!
var profileSelectedValue: String!
#IBOutlet var transcriptTypeComboBox: NSPopUpButton!
var transcriptTypeSelectValue: String!
#IBOutlet var submissionTypeComboBox: NSPopUpButton!
var submissionTypeSelectValue: String!
#IBOutlet var transcriptionTurnaroundComboBox: NSPopUpButton!
var turnaroundSelectValue: String!
#IBOutlet var persistentNoteComboBox: NSPopUpButton!
var persistentNoteSelectValue: String!
#IBOutlet var notesTextField: NSTextField!
#IBOutlet var batchIdentifierTextField: NSTextField!
#IBOutlet var languageInAudioComboBox: NSPopUpButton!
var languageSelectValue: String!
#IBOutlet var addResultViewButton: NSButton!
#IBOutlet var submissionFiles: NSTableView!
var delegate: SubmissionViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func save(_ sender: Any) {
guard case profileIDComboBox.titleOfSelectedItem =
profileSelectedValue else { return }
guard case transcriptTypeComboBox.titleOfSelectedItem =
transcriptTypeSelectValue else { return }
guard let delegate = delegate else { return }
delegate.controller(self, didAddSubmissionWithID:
profileSelectedValue)
}
}
I now want to store the titleOfSelectedItem from each NSPopUpButton in my Core Data Submission model. However, when I set the variables to be strings, I receive the error as follows:
Cannot convert value of type 'String?' to expected argument type '_OptionalNilComparisonType'
How do I accomplish this task?
ProgressBar only shows the progress when the loop ends.
How do in this example for the bar to be updated in real time?
#IBAction func btnStart(sender: AnyObject) {
for var xx:Float = 0 ; xx<=1.0; xx=xx+0.00001 {
progressView.setProgress(xx, animated: false)
}
}
#IBOutlet weak var progressLabel: UILabel!
#IBOutlet weak var progressView: UIProgressView!
UI only updates in the main loop and at the end of the current scope,
so try this
#IBAction func btnStart(sender: AnyObject) {
NSThread.detachNewThreadSelector("process", toTarget: self, withObject: nil)
}
func process() {
for var xx:Float = 0 ; xx<=1.0; xx=xx+0.00001 {
dispatch_async(dispatch_get_main_queue(), {
progressView.setProgress(xx, animated: false)
})
}
}
#IBOutlet weak var progressLabel: UILabel!
#IBOutlet weak var progressView: UIProgressView!