How to use 2 actions in 1 function in Swift? - swift

I am having trouble formulating my question here, but I am doing an online course on Swift, it is kind of outdated and not many people reply.
So we are making an app with a todo list, simple todo list that has a list of things you need to do and you can add a checkmark to things you've done.
I decided to make it more channelling for me and decided that I want to be able to edit tasks that I already added. So now when I click on a task I can edit it, but I did not take into account that a regular click also set the checkmark for done.
My question is:
How can I make that the checkmark is set by regular touch, but the edit function is triggered by a long touch, or that is not possible?
Here is the part of the code that I have for the editing of the list, which is also when the checkmark was added, but I commented that out for now.
func editItems(tableView: UITableView, indexPath: IndexPath){
var textField = UITextField()
let alert = UIAlertController(title: "Edit Item", message: "", preferredStyle: .alert)
let action = UIAlertAction(title: "Save", style: .default) { [self] (action) in
self.itemArray[indexPath.row].title = textField.text
self.saveItems()
}
alert.addTextField { (alertTextField) in
alertTextField.text = self.itemArray[indexPath.row].title
textField = alertTextField
}
alert.addAction(action)
present(alert, animated: true, completion: nil)
tableView.reloadData()
}

Related

Cannot use UIAlertController inside a prototype cell in tableView

I have a tableView with a prototype cell. Inside this prototype cell I have a button which I want to use to report a comment.
I tried to use an example UIAlertController but it gives me the following error.
Value of tableViewCell has no member present.
And also this error:
"nil" requires a contextual type.
I used the code below also somewhere else and it worked fine.
What exactly did I do wrong here? I already googled the error types but don't find anything that could help me.
func displayAlert() {
// Declare Alert message
let dialogMessage = UIAlertController(title: "Test", message: "Test", preferredStyle: .alert)
// Create OK button with action handler
let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
print("Ok button tapped")
self.deleteRecord()
})
//Add OK and Cancel button to dialog message
dialogMessage.addAction(ok)
// Present dialog message to user
self.present(dialogMessage, animated: true, completion: nil)
}
func deleteRecord()
{
print("Delete record function called")
}

how to add a password to a button event which cause the event to proceed or fail

I have a Xcode project which is a questionnaire, that has a few questions in and buttons that pass the data selected from one view to the other using segue tags.
At the end of the questionnaire I have a show data button in which will take the admin to the page with the data on as a whole, my issue is anyone can trigger the show data event and I was wondering how would I go about adding a password so that only the admin can see the data.
I've looked online and haven't seen anything that relates to what I need to happen, most things online go over creating a sign up page.
#IBAction func DataReveal(_ sender: UIButton)
{
ID = ID2.text
date = date2.text
Answer1 = answ1.text
Answer2 = answ2.text
}
self.performSegue(withIdentifier: "Link6", sender: self)
}
This takes the results from the other pages and passes them to the page that has the show data button, I just need to add password protection to it so only the password holder can see the data.
You need to Show UIAlert Action with textfield for pick password from user and match text with your password from Coredata,keychain or with API whatever you want.
UIAlert with textfield code is
let alertController = UIAlertController(title: "Test Title", message: "", preferredStyle: .alert)
alertController.addTextField { textField in
textField.placeholder = "Enter Password"
textField.isSecureTextEntry = true
}
let confirmActionBtn = UIAlertAction(title: "OK", style: .default) { [weak alertController] _ in
guard let alertController = alertController, let textField = alertController.textFields?.first else { return }
print("Password is \(String(describing: textField.text))")
// Just compare Entered String in textfield with your original password password
//if password is matched push or segue your required controller
}
alertController.addAction(confirmActionBtn)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)

How to implement code in two different UIAlertActions from a single UIAlertController?

I am new in Swift and Xcode world. In my app, user has to choose if he/she wants to scan QRCode to get data from it, or if he/she wants to enter data(type String) and generate QRCode via that String. I am trying to implement QRScanner and QRGenerator but I am stuck.
I am using UIAlertController which has two UIAlertActions for now.
After I get my data using QRScanner/QRGenerator, I have to store it in my custom UITableViewCell. Should I be using UIAlertController or something else? Any tips and tricks how should I do this?
https://imgur.com/a/qqhzG8Q
This is what I have for now.
#IBAction func cameraButtonClicked(_ sender: UIBarButtonItem) {
var textField = UITextField()
let alert = UIAlertController(title: "What do you want to do?", message: "", preferredStyle: .alert)
let scanAction = UIAlertAction(title: "SCAN QR", style: .default) { (action) in
}
let manualAction = UIAlertAction(title: "MANUAL", style: .default) { (action) in
}
alert.addAction(scanAction)
alert.addAction(manualAction)
present(alert, animated: true, completion: nil)
}
I tried several different ways to make this work, but my app is constantly crashing. As I said, I am new in this world, so any suggestion is more than welcome.

How to make editable table cell

I'm looking for a way to make UITableView editable like in phone app in iOS when I want to edit the contact data, user should view data and shall be able to press on edit in order to edit the data.
I've been searching for a way to do this for weeks on the web, but I didn't find anything
There are two methods that I use for editing table cells. The first method is tableView(_: canEditRowAtIndexPath indexPath:). In this method you specify the section and row of the table view that can be edited. For instance>
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
if indexPath.section == 0 {
if (indexPath.row == 0)||(indexPath.row == 2) {
return true
}
}
return false
}
In this example you specify that only the first and third row of the first section of the table can be edited.
The second method I use is the method tableView(_: didSelectRowAtIndexPath:). In this method you specify how to react when the user selects the cell (row). In this method you can specify anything you like to change the cell. You can for instance segue to another view and do whatever you want to do. I, however, usually use an alert or action sheet to change the cell. Here is an example that extends the previous example:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//First check whether the right cell is being selected.
let selectedIndexPath = self.trackDetailsTable.indexPathForSelectedRow
//If the selected row is not in the first section the method returns without doing anything.
guard selectedIndexPath?.section == 0 else {
return
}
if selectedIndexPath?.row == 0 {
//The first row is selected and here the user can change the string in an alert sheet.
let firstRowEditAction = UIAlertController(title: "Edit Title", message: "Please edit the title", preferredStyle: .Alert)
firstRowEditAction.addTextFieldWithConfigurationHandler({ (newTitle) -> Void in
newTitle.text = theOldTitle //Here you put the old string in the alert text field
})
//The cancel action will do nothing.
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action) -> Void in
self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
})
//The Okay action will change the title that is typed in.
let okayAction = UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in
yourObject.thatNeedsTheNewString = (firstRowEditAction.textFields?.first?.text)!
//Do some other stuff that you want to do
self.trackDetailsTable.reloadData() //Don’t forget to reload the table view to update the table content
self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
})
trackTitleEditAction.addAction(okayAction)
trackTitleEditAction.addAction(cancelAction)
self.presentViewController(trackTitleEditAction, animated: true, completion: nil)
}
if selectedIndexPath?.row == 2 {
//The third row is selected and needs to be changed in predefined content using a standard selection action sheet.
let trackLevelAction = UIAlertController(title: "Select Predefined Content”, message: "Please, select the content”, preferredStyle: UIAlertControllerStyle.ActionSheet)
for content in arrayOfPredefinedContent {
predefinedContentAction.addAction(UIAlertAction(title: content, style: UIAlertActionStyle.Default, handler: { (UIAlertAction) -> Void in
yourObject.thatNeedsTheNewContent = content
//Do some other stuff that you want to do
self.trackDetailsTable.reloadData() //Don’t forget to reload the table view to update the table content
}))
}
trackLevelAction.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: { (UIAlertAction) -> Void in
}))
presentViewController(trackLevelAction, animated: true, completion: nil)
}
}
Hope this helps.
It's a general question but here's a general answer - you link your table cell with a segue to a new scene where you can display an editable form to the user. The user clicks the table cell which triggers the segue. You might find this Apple tutorial useful.

UIAlertController button click navigates to a NavigationController's first ViewController

I am new to swift and iOS programming. I have StartViewController which has a button which when clicked a UIAlertController with two buttons - Decline & Accept. When clicking on Accept, I want to navigate to a MyNavigationController's - ViewController. The MyNavigationController has four ViewControllers which I navigate to using a slide menu.
I am attaching a sample code and screenshot of my storyboard for reference.
#IBAction func showAlert() {
let alertController = UIAlertController(title: "Disclaimer", message: "Before using this teaching resource, you confirm that you agree:\n1. To obey the law regarding data protection and patient confidentiality.\n2. To us this app professionally and appropriately in clinical settings.\n3. This is for your personal use and you may not modify, distribute, publish, transfer any information obtained from this teaching resource without the developers' permission.\n4. In no event shall the developer be liable to you for any loss arising from your use of this resource.", preferredStyle: .Alert)
let declineAction = UIAlertAction(title: "Decline", style: .Cancel, handler: nil)
alertController.addAction(declineAction)
let acceptAction = UIAlertAction(title: "Accept", style: .Default) { (_) -> Void in
//I think the code to navigate should go here, help please.
}
alertController.addAction(acceptAction)
presentViewController(alertController, animated: true, completion: nil)
}
Storyboard Screenshot -
In the screenshot above, the Login button opens up a UIAlertController alertController. Accept button on this AlertController should navigate to ViewController on the MyNavigationController.
This MyNavigationController has three other ViewControllers which are navigated using a slide menu.
Thank you in advance.
You need to create a segue between your current view controller and the destination view controller and make sure the segue ID matches the id in your performSegueWithIdentifier call
#IBAction func showAlert() {
let alertController = UIAlertController(title: "Disclaimer", message: "Before using this teaching resource, you confirm that you agree:\n1. To obey the law regarding data protection and patient confidentiality.\n2. To us this app professionally and appropriately in clinical settings.\n3. This is for your personal use and you may not modify, distribute, publish, transfer any information obtained from this teaching resource without the developers' permission.\n4. In no event shall the developer be liable to you for any loss arising from your use of this resource.", preferredStyle: .Alert)
let declineAction = UIAlertAction(title: "Decline", style: .Cancel, handler: nil)
let acceptAction = UIAlertAction(title: "Accept", style: .Default) { (_) -> Void in
self.performSegueWithIdentifier("SomeSegue", sender: self) // Replace SomeSegue with your segue identifier
}
alertController.addAction(declineAction)
alertController.addAction(acceptAction)
presentViewController(alertController, animated: true, completion: nil)
}