I'm making a todolist app in swift. Here is what it looks like :
So far to create a todo I used an alertController but I changed it and now using a new viewcontroller opening when click on the "+" to add. When the ViewController opens itself, I fill the title and then click a button. On the click button I'd like it to add a new row in the tableViewController with the title I put in the textField of the viewController.
I'm having trouble to pass the value of the textField in the new row created in the tableViewController.
Anyone can give some help ?
[EDIT]
Here is my addTodoController :
var updateTableViewClosure: ((String) -> Void)?
#IBAction func AddButton(_ sender: Any) {
var todoValue = titleField.text
updateTableViewClosure?(todoValue!)
}
#IBOutlet weak var titleField: UITextField!
Here is my tableviewcontroller :
if segue.identifier == "CreateAdd" {
let addItemViewController = segue.destination as! CreateAdd
addItemViewController.updateTableViewClosure = {
newItem in myItems(title: todoValue, content:"hello", ontent: "Ceci est le contenu" , startDate: "16/08/1997", author: "Clem")
}
}
I get an error:
" Use of unresolved "todoValue"
You need a way to pass data back to the view controller containing the table view. This can be achieved in many ways, for example using the delegate pattern.
One simpler way to achieve that is using closures, like this:
In the view controller containing the table view:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "identifier" {
let addItemViewController = segue.destination as! AddItemViewController
addItemViewController.updateTableViewClosure = {
newItem in
// Add the new item to your data source and reload your table view.
}
}
}
In AddItemViewController add a new variable called updateTableViewClosure which will be your closure:
var updateTableViewClosure: (String -> Void)?
and call it wherever you want like this:
updateTableViewClosure?("The new item")
Hope this helps!
Related
I'm building an app to keep track of scores i have a struct
*/
var teamA_name: String!
var teamB_name: String!
var teamA_points: [Int] = []
var teamB_points: [Int] = []
/*
- Add points to the teams
*/
mutating func addPoints(teamA: Int, teamB: Int){
self.teamA_points.append(teamA)
self.teamB_points.append(teamB)
}
as you can see i have two int arrays that will hold the user points. I have a controller with two tableviews to show the array of points added by the user, i'm going to skip some of the code since i know is not needed for my problem, this is my Main ViewController where tables will show the points
class GameScoreViewController: UIViewController {
/*
- Properties
*/
var gameScore = GameScore()
override func viewDidLoad() {
super.viewDidLoad()
//- Setup delegates & datasources
teamA_tableview.delegate = self
teamA_tableview.dataSource = self
teamB_tableview.delegate = self
teamB_tableview.dataSource = self
// - Button configuration
addPointsButton.layer.cornerRadius = 5
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toPopUp"{
let popUpView = segue.destination as! PopUpViewController
// this is where i call my popup view
}
}
}
}
now here is where my problem occurs, when i segue to my pop up and the user enters the score needed and taps done, the data doesn't append to the array and my tableview won't reload, i've tried many different ways, using callbacks, delegates, i tried userdefaults since is not very important data but nothing seems to work, and i'm stuck, this is my pop up view controller button action where it should happen, i left the textfield.text in the parameter for reference
#IBAction func addBtnPressed(_ sender: Any) {
// this is the func to append data to the array
gameScore.addPoints(teamA: Int(pointsTextField.text!)!, teamB: 0)
self.dismiss(animated: true, completion: nil)
//after dismissed it should reload table view or insert row with the user entered score
}
any help will be appreciated, thank you.
It seems that you declare a separate instance inside the popup
gameScore.addPoints(teamA: Int(pointsTextField.text!)!, teamB: 0)
You need to set a delegate to the real object in segue
let popUpView = segue.destination as! PopUpViewController
popUpView.delegate = self
And declare it inside the popup like
var delegate:GameScoreViewController?
Then use it
delegate?.addPoints(teamA: Int(pointsTextField.text!)!, teamB: 0)
I've set up a simple Swift project to try and wrap my head around delegates & protocols. The goal is to pass data between two classes (SendingClass & ReceivingClass). Two buttons in the SendingClass are linked to the delegate which should trigger the Protocol conforming function in the ReceivingClass to execute. This doesn't work unfortunately, I suspect it has to do with where and how I am declaring the ReceivingClass as the delegate.
Appreciate your insights, i'm just starting out!
I've tried setting the delegate in various locations (presently within viewDidLoad, but cant get it to work).
let vc = SendingClass()
vc.statusDelegate = self
SendingClass.swift
import UIKit
protocol StatusDelegate {
func statusChanged(state: Bool, sender: String)
}
class SendingClass: UIViewController {
var statusDelegate : StatusDelegate?
#IBAction func button1Pressed(_ sender: UIButton) {
statusDelegate?.statusChanged(state: true, sender: "Button 1")
}
#IBAction func button2Pressed(_ sender: UIButton) {
statusDelegate?.statusChanged(state: false, sender: "Button 2")
}
}
ReceivingClass.swift
import Foundation
import UIKit
class ReceivingClass: UIViewController, StatusDelegate {
override func viewDidLoad() {
let vc = SendingClass()
vc.statusDelegate = self
}
func statusChanged(state: Bool, sender: String) {
print("Sender = \(sender) , State = \(state)")
}
}
Expected: the ReceivingClass protocol conforming function (func statusChanged) should execute each time the buttons are pressed within the SendingClass.
Actual: Nothing happens
I am using this..
// create extension in your receiving class
extension ReceivingClass: PopUpVCDelegate {
func statusChanged(state: Bool, sender: String) {
print("Sender = \(sender) , State = \(state)")
}
}
// on sending class, when you present your receiving class on any button click
eg.
let resultController = self.storyboard?.instantiateViewController(withIdentifier: "PopUpVCID") as? PopUpVC
resultController?.delegate = self
self.present(resultController!, animated: true, completion: nil)
//or if not have button add on viewdidload in receiving class
// here is full eg
How to get data from popup view controller to custom table view cell?
For protocol and delegate, you use it when u want to bring a value from 2nd VC (presented by 1st or pushed by 1st VC) to 1st VC, which is the original.
From your code, I dont see you presenting or pushing your 2nd VC. that's why it's not working. Hopefully I answered your doubt.
However if you still want to bring a value over from 1st VC to 2nd VC. In second VC, create a variable to receive it
var ReceivedData = String()
then from your first VC, when u are going to push it,
let vc = SendingClass()
vc.ReceivedData = "Whatever you want it to receive"
If you're using storyboard segues, maybe the view controller is instantiated from there so probably you have to use the prepareForSegue and get the destination view controller (which is already instantiated for you) in the ReceivingClass view controller:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
if let destination = segue.destination as? SendingClass {
destination.delegate = self
}
}
Also be careful with delegate patter: the delegate property should be declared as a weak property to avoid retain-cycle
weak var delegate: MyDelegate?
I have an app setup to have a Master root viewController with a Navigation bar that has a "Settings button". When the user taps the settings button, it brings up a 'settingsTableViewController' that is not dynamic, but rather static so i can format the tableviews with settings like style.
in this settings view, i have a UITextField that takes a Person's name.
When the user clicks "Done" in the navigation bar, i want to pass that name back to the Master Root ViewController so i can use it in the title to display the Person's name. (I want to pass the name upon dismissing the view)
I have tried to use segue's but no luck. Here is a bit of what i tried.
SettingsViewController (pop's over the MasterVC)
class SettingsTableViewController: UITableViewController {
#IBOutlet weak var nameTextfield: UITextField!
#IBAction func done(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
override viewDidLoad(){
self.title = "Settings"
}
// MARK: - Navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Trying to pass what was typed in the textfield to the Root ViewController and store this name in a variable of type String called 'name'
if segue.identifier == "masterView"{
let masterViewController = segue.destinationViewController as! MasterTableViewController
masterViewController.name = nameTextField.text
print("Segue was used")
}
}
}
MasterTableViewController.swift
class MasterTableViewController: UITableViewController {
var name: String!
// I am trying to then display the name entered in the pop-over view in this root view controller's title
override viewDidLoad(){
self.title = name!
}
}
My question is, what did i do wrong? I have tried to use delegation but i get nil and the data doesn't seem to get passed either way so any leads will greatly help. Thanks
There is a different kind of segue called Unwind Segue to do that. It doesn't create a new mvc and its used to pass data back to the vc that presented the current one. here is how to do it
First, go to your storyboard and control drag from settingVc to the exit button on top of it (to itself). it will give you 'Selection Segue' and choose IBAction goBack. This means any vc that presented the current one will get to prepare if they implement this method. In other words, you're putting out a protocol and the presenter vc will conform by implementing goBack IBAction. here is how to implement that. In your Mater vc
//You must have this function before you do the segue in storyboard
#IBAction func goBack(segue: UIStoryboardSegue){
/* this is optional
if let stv = segue.sourceViewController as? SettingsTableViewController{
self.name = stv.nameTextfield?.text
}
*/
}
In your setting vc (current vc)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//make sure you set the id of your segue to "Your Go back Unwind segue". ofc u can name it whatever
if segue.identifier == "Your Go back Unwind segue"{
if let mtvc = segue.destinationViewController as? MasterTableViewController{
mtvc.name = nameTextField.text
print("Segue was used")
}
}
}
and done method should be something like this
#IBAction func doneEditing(sender: UIButton) {
presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
}
I have a program in which the user will enter data in one view controller and the information is calculated in another. I've gotten to the point where I can enter the data and when I segue to the SecondViewController I can access my data. But when I switch back to the first ViewController, the UITextFields are blank because the ViewController is re-instantiated.
Basically my application so far has one ViewController with two UITextfields and a SecondViewController that shows the data. I need a way to save the instance of the first ViewController when I am switching between the two.
I've tried using the answer from iOS Swift, returning to the same instance of a view controller , but yourVariable cannot be set to nil, so I got stuck there.
This is my code for segue-ing from the first ViewController to the SecondViewController
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if (segue.identifier == "Show")
{
var destinationVC = segue.destinationViewController as! MainViewController
}
}
I only have a button that I press to go back to the first ViewController.
You can create a class to store your text file, create instance of it in appdelegate(in a global scope) and get your text data to the textfield using observer. global instance of a class may not be the best choice if your app is not as simple as you described it in which case you can use NSUsersDefault to save it as plist. I hope I helped :)
//in first view controller
#IBOutlet weak var textfield: UITextField!{
didSet{
textfield.text = text
}
}
var text:String = ""{
didSet{
textfield?.text = text
}
}
func override viewWillAppear(animated: Bool) {
//set value of text from your global class instance or
//decode it here from the archive file if you used NSUsersDefault
text =
}
help me please. how to make a button to do some operation or change to another view? I want that by pressing on it, commands were complete then need that moved on another view. please tell me how else to do to another kind in which a transition is updated when you press the button, all the same. And there were set data from datamodel
#IBAction func addNew(sender: AnyObject) {
let entity = NSEntityDescription.entityForName("Items", inManagedObjectContext:managedObjectContext!)
var item = Items(entity: entity!, insertIntoManagedObjectContext:managedObjectContext)
// I make the init from row coredata
item.titleIt = titleItem.description
item.textIt = textViewItem.description
var error: NSError?
managedObjectContext?.save(&error)
// MasterViewController.setValue(Items(), fromKey: "titlIt")
}
Add this at the end of your method:
let viewControllerToGoTo = ClassNameOfViewController()
self.presentViewController(viewController, animated: true, completion: nil)
Note: You may need to instantiate the view controller you want to present in a different way than just (). For example, you may want to load a view controller defined in a nib or storyboard.
To move to another view controller, you write these lines:
var viewController = ViewControllerNew()
self.presentViewController(viewController, animated: true, completion: nil)
But if you want to pass data to another view, I'd recommend using this method instead:
In your storyboard, select the view controller and drag from the blue-outlined yellow square at the top of the view controller to another view controller. A popup will appear, showing a list of segues. Select the item named 'Show'. Segues are placeholders for presenting view controllers -- you can call them at any time in your program. Select the segue, and under the menu on the right hand side type a name, for example 'toOtherScreen' into the text field labeled 'Identifier'. This will let you call the segue from a specific name.
Now go back to your IBAction for the button, and type this:
self.performSegueWithIdentifier("toOtherScreen", sender: self)
This will transition to the other screen. But what if you want to pass data to a screen, or do something when a segue is about to happen? Luckily for you, UIViewController class has a method named 'prepareForSegue', as shown below:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toOtherScreen" {
println("yes")
}
}
In this, we check if the segue has an identifier of "toOtherScreen", and if so, print "yes" to the logs. Now, to pass data to another view controller, it is a little more tricky. In the other view controller file, add a variable at the start of the class:
class OtherViewController: UIViewController {
var dataPassed: String = ""
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
And in your prepareForSegue method in the main view controller, type this:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toOtherScreen" {
let newViewController = segue.destinationViewController as OtherViewController()
newViewController.dataPassed = "NEW DATA"
}
}
Now it will change the variable named dataPassed in OtherViewController to 'NEW DATA'. You can see how you can achieve a lot through this simple way of passing data to other view controllers.
Hope I helped.