Passing data back to previous view controller after selecting table view cell - ios

I'm having trouble passing data after selecting a table view cell to the previous view controller. I'm pretty much trying to change a label from the previous view controller after selecting a table view cell. Could anyone help me go about this? I'm trying to change the UITextField after selecting a cell.
UIViewController:
class WhoToOdds: UIViewController, sendBack,UITextFieldDelegate{
#IBOutlet var chosenContact: UITextField!
#IBOutlet var oddsTextBox: UITextView!
var friend: String?
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(false, animated: true)
}
func sendNameToPreviousVC(selectedfriendName: String) {
friend = selectedfriendName
chosenContact.text = friend
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "friendList"{
let friendViewController = (segue.destinationViewController as! friendListController)
var fbRequest = FBSDKGraphRequest(graphPath:"/me/friends", parameters: nil);
fbRequest.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
if error == nil {
println("Friends are : \(result)")
PFUser.currentUser()?["friend_list"] = result
PFUser.currentUser()?.save()
print(result)
var resultdict = result as! NSDictionary
println("Result Dict: \(resultdict)")
friendViewController.friendArray = resultdict.objectForKey("data") as! NSArray
} }
}
}
#IBAction private func submitChallenge(sender: AnyObject) {
navigationController?.popViewControllerAnimated(true)
}
}
TableViewController:
protocol sendBack
{
func sendNameToPreviousVC(contact: String)
}
class friendListController: UITableViewController, UITableViewDataSource, UITableViewDelegate{
var friendArray:NSArray = ["a","b","c"]
var valueDict:NSDictionary = [:]
var mDelegate:sendBack?
var selectedFriend :String?
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(false, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return friendArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("friend", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel!.text = (friendArray[indexPath.row] as! String)
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
let indexPath = tableView.indexPathForSelectedRow();
let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!
selectedFriend = currentCell.textLabel!.text as String!
sendBackFriendList(selectedFriend!)
navigationController?.popViewControllerAnimated(true)
}
func sendBackFriendList(name: String){
self.mDelegate?.sendNameToPreviousVC(name)
}
}

Your delegate needs to be set. In your case you have to set it inside prepareForSegue method like
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "friendList"{
let friendViewController = (segue.destinationViewController as! friendListController)
friendViewController.mDelegate = self //Include this line
//rest of the code
}
}

You didn't set the delegate in your segue, so mDelegate is nil in your sendBackFriendList method

Related

Parse from TableVC to textView in other viewController swift

I am new in programming, and have problem. I am using parse for my array in tableview. When the row is selected i want to segue on another view controller to textView. The tableview works good but i can't get text to textView.
tableVC:
import UIKit
import Parse
class ThirdTableVC: UITableViewController {
#IBOutlet weak var refresherQuotes: UIRefreshControl!
#IBOutlet var quoteTable: UITableView!
var selectedQuote: PFObject?
var quoteItems = [PFObject]()
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func updateQuotesResults(_ sender: UIRefreshControl) {
fetchQuotesData()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
loadQuoteTexts(selectedQuote: selectedQuote)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return quoteItems.count
}
func fetchQuotesData() {
let quotesQuery = PFQuery(className: "TotalTest")
quotesQuery.whereKey("Subcategory", equalTo: selectedQuote ?? nil)
quotesQuery.findObjectsInBackground { (objects, error) in
if let realCategoryObjects = objects {
self.quoteItems = realCategoryObjects
self.tableView.reloadData()
self.refresherQuotes.endRefreshing()
}
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let quoteCell = tableView.dequeueReusableCell(withIdentifier: "quoteCell", for: indexPath)
let quoteItem = quoteItems[indexPath.row]
let quoteUserTitle = quoteItem.object(forKey: "TextQuote") as? String
quoteCell.textLabel?.text = quoteUserTitle
return quoteCell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showQuoteDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let quoteobject = quoteItems[indexPath.row] as? NSDate
let quoteController = (segue.destination as! UINavigationController).topViewController as! DetailViewController
quoteController.detailItem = quoteobject
quoteController.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
quoteController.navigationItem.leftItemsSupplementBackButton = true
}
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Row tapped: \(indexPath.row)")
let selectedQuotes: PFObject = quoteItems[indexPath.row]
let FourthVC = self.storyboard?.instantiateViewController(withIdentifier: "FourthViewController") as! FourthViewController
FourthVC.fourthTextView.text = quoteItems[indexPath.row] as? String
self.navigationController?.pushViewController(FourthVC, animated: true)
}
func loadQuoteTexts(selectedQuote: PFObject!) {
let quoteQuery = PFQuery(className: "TotalQuote")
quoteQuery.whereKey("QuoteSubs", equalTo: selectedQuote ?? nil)
quoteQuery.includeKey("QuoteSubs")
quoteQuery.findObjectsInBackground { (result: [PFObject]?, error) in
if let searchQuoteResults = result {
self.quoteItems = searchQuoteResults
self.quoteTable.reloadData()
}
}
}
}
How can I change this?
viewController with textView:
import UIKit
import Parse
class FourthViewController: UIViewController {
var getQuote: PFObject?
#IBOutlet weak var fourthTextView: UITextView!
#IBOutlet weak var fourthLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
fourthLabel.text! = getQuote as! String
fourthTextView.text! = getQuote as! String
}
}
Please help me to passing texts
If you use pushViewController do it like that , in did selectRowAt
let MainStory: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let desVC = MainStory.instantiateViewController(withIdentifier: "FourthViewController") as! FourthViewController
and now pass your text
desVC.getText = "here goes your text u want to pass"
FourthViewController
set up your var
var getText = String()
so you can finally use
self.navigationController?.pushViewController(desVC, animated: true)
so it will pass all parameters you previous add with desVC.getSomething
in FourthViewController you just need to use getText.
The problem is that you are changing from a view to another with pushViewController, by doing that your prepareForSegue won't be executed.
On your didSelectRow you need to call performSegue(withIdentifier:sender:).
You can lookup this question for more information on how to do it.

Swift prepareForSegue object is nil

I'm trying to pass the event Object into DetailViewController, but it crashes and says: "fatal error: unexpectedly found nil while unwrapping an Optional value". I'm positive the error is in my prepare(for segue:) function, but I've been at this for hours and I can't find out how to fix it. Any help would be appreciated. Thanks!
Here is my code:
class MainViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var textView: UITextView!
#IBOutlet weak var tableView: UITableView!
var events = [Event]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tableView.delegate = self
tableView.dataSource = self
parseCSV()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func logOutAction(sender: AnyObject) {
if FIRAuth.auth()?.currentUser != nil {
do {
try FIRAuth.auth()?.signOut()
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SignUp")
present(vc, animated: true, completion: nil)
} catch let error as NSError {
print(error.localizedDescription)
}
}
}
func parseCSV() {
let path = Bundle.main.path(forResource: "eventData", ofType: "csv")
do {
let csv = try CSV(contentsOfURL: path!)
let rows = csv.rows
for row in rows {
let eventTitle = row["Title "]!
let eventLoc = row[" Location "]!
let eventStart = row[" Start_Time "]!
let eventEnd = row [" End_Time"]!
let event = Event(title: eventTitle, loc: eventLoc, start: eventStart, end: eventEnd)
events.append(event)
}
} catch let err as NSError {
print(err.debugDescription)
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "eventCell", for: indexPath) as? EventCell{
let event = events[indexPath.row]
cell.updateUI(event: event)
return cell
} else {
return UITableViewCell()
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return events.count
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let event = events[indexPath.row]
performSegue(withIdentifier: "detailSegue", sender: event)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "detailSeque" {
if let detailVC = segue.destination as? DetailViewController {
if let event = sender as? Event {
detailVC.event = event
}
}
}
}
}
My DetailViewController class:
class DetailViewController: UIViewController {
var event: Event!
#IBOutlet weak var eventTitle: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
eventTitle.text = event.title // error here. says event is nil.
}
}
Since the segue is connected from your cell, it already performed automatically when you tap the cell. Performing it programmatically is resulting in your segue being performed twice: once with the value passed correctly and once without.
You can either change your segue to be connected from the view controller instead of the cell in which case your current code would work, or update your code to do all of the logic in prepareForSegue and remove your implementation of didSelectRowAt:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "detailSeque" {
if let detailVC = segue.destination as? DetailViewController {
let row = tableView.indexPathForSelectedRow!.row
detailVC.event = events[row]
}
}
}

iOS unwind CollectionView at didSelectItemAtIndexPath

So i have created an app where users create custom workouts.
You enter the first view add a x number of rounds,than you click on a button in the tableview where you add rounds.That click opens another activity with a collectionview.
So the problem is here when i click on an element witch represents the exercise that needs to be in the round it sends me an empty result.By default if i add a value to that string it works.
I have noticed that the unwind happens before my didSelectItemAtIndexPath
import UIKit
class InsertWorkout: UIViewController{
#IBOutlet var InsertRoundTable: UITableView!
#IBOutlet weak var txtName: UITextField!
var RoundNumber : NSMutableArray = ["Round 1"]
var RoundLabel : NSMutableArray = [""]
var RoundExercise : NSMutableArray = ["add-1"]
var RoundExerciseImages : NSMutableArray = ["providno"]
var RoundNumber_Count=1
var RoundNumber_Add_Counter=1
var studentData : StudentInfo!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: UIButton Action methods
#IBAction func btnBackClicked(sender: AnyObject)
{
self.navigationController?.popViewControllerAnimated(true)
}
#IBAction func btnSaveClicked(sender: AnyObject)
{
if(txtName.text == "")
{
Util.invokeAlertMethod("", strBody: "Please enter workout name.", delegate: nil)
}
else
{
let studentInfo: StudentInfo = StudentInfo()
studentInfo.workout_name = txtName.text!
studentInfo.workout_benefit_1=" CUSTOM "
studentInfo.workout_benefit_2=" WORKOUT "
studentInfo.workout_benefit_3=""
studentInfo.workout_requiremnets="arsutech.com"
studentInfo.workout_time="4min"
let isInserted = ModelManager.getInstance().addWorkoutData(studentInfo)
if isInserted {
Util.invokeAlertMethod("", strBody: "Workout added", delegate: nil)
} else {
Util.invokeAlertMethod("", strBody: "Error while adding workout.", delegate: nil)
}
self.navigationController?.popViewControllerAnimated(true)
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
InsertRoundTable.endEditing(true)
}
//UITableView
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return RoundNumber.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell : InsertRoundCell! = tableView.dequeueReusableCellWithIdentifier("InsertRoundCell") as! InsertRoundCell
if(cell == nil)
{
cell = NSBundle.mainBundle().loadNibNamed("InsertRoundCell", owner: self, options: nil)[0] as! InsertRoundCell;
}
let exerviseName = RoundNumber[indexPath.row]
let exerciseLabels = RoundLabel[indexPath.row]
let exerciseImage = RoundExercise[indexPath.row]
let exerciseImage_Holder = RoundExerciseImages[indexPath.row]
cell.insert_label_Round.text = exerviseName as? String
cell.addWorkout_Label.text = exerciseLabels as? String
cell.addExercise_Holder?.image = UIImage(named: exerciseImage_Holder as! String) as UIImage?
cell.addWorkoutBut.setBackgroundImage(UIImage(named: exerciseImage as! String) as UIImage?, forState: UIControlState.Normal)
cell.addWorkoutBut.tag = indexPath.row
cell.addWorkoutBut.addTarget(self, action: "logAction:", forControlEvents: .TouchUpInside)
cell.selectionStyle = UITableViewCellSelectionStyle.None
return cell as InsertRoundCell
}
#IBAction func logAction(sender: UIButton){
//self.RoundNumber.replaceObjectAtIndex(sender.tag, withObject: "Ezel")
//let titleString = self.RoundNumber[sender.tag] as? String
//Util.invokeAlertMethod("", strBody: titleString!, delegate: nil)
self.InsertRoundTable.reloadData()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
let destinationVC = segue.destinationViewController as! ChoseExercise
destinationVC.id=sender.tag
RoundNumber_Count=sender.tag
}
#IBAction func unwinndChoseExercise(segue:UIStoryboardSegue){
if let svc = segue.sourceViewController as? ChoseExercise{
self.RoundLabel.replaceObjectAtIndex(RoundNumber_Count, withObject: "\(svc.exercise_label)")
self.RoundExercise.replaceObjectAtIndex(RoundNumber_Count, withObject: "providno")
self.RoundExerciseImages.replaceObjectAtIndex(RoundNumber_Count, withObject: "\(svc.exercise_image)")
self.InsertRoundTable.reloadData()
}
}
//Add Round
#IBAction func addRound(sender: AnyObject) {
if(RoundNumber_Add_Counter<12){
RoundNumber_Add_Counter++
self.RoundNumber.addObject("Round \(RoundNumber_Add_Counter)")
self.RoundLabel.addObject("")
self.RoundExercise.addObject("add-1")
self.RoundExerciseImages.addObject("providno")
self.InsertRoundTable.reloadData()
}else{
Util.invokeAlertMethod("", strBody: "This is the maximum number of rounds", delegate: nil)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.exercisesNames.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellexercises_insert", forIndexPath: indexPath) as! Insert_ExerciseCollectionViewCell
cell.insert_exerciseImage?.image = self.exercisesImages[indexPath.row]
cell.insert_exerciseLabel?.text = self.exercisesNames[indexPath.row]
cell.insert_exercisesHardnessImg?.image = self.exercisesHardnessImg[indexPath.row]
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
exercise_label="\(exercisesNames[indexPath.row])"
exercise_image="\(exercises_Exervise_Row_Names[indexPath.row])"
dismissViewControllerAnimated(true, completion: nil)
}
Here is how unwinding works:
First, put your IBAction unwind(segue:UIStoryboardSegue) in the vc where you intend to go back (presenter vc, InsertWorkout in this case).
Second, go to storyboard and choose the vc where you'll unwind from (collection view in this case) and ctrl-drag from the button/cell that causes the unwind (collection view cell in your case) to the exit button on top of that same vc and you should see a pop-up with your IBAction unwind method name under 'Selection Segue'.
Third, implement prepareforsegue in the current vc (collection vc in this case) to pass your data back.
class InsertWorkout
{
IBAction func unwind(segue:UIStoryboardSegue) {
//use the data passed from prepare to update your ui or you can also communicate to the sender using segue.sourceVC
}
}
class CollectionVC
{
override func prepareForSegue(segue: UIStoryboadSegue, sender: AnyObject?){
if segue.identifier == "whatever you put in storyboard for segue id" {
if let inserWorkoutVC = segue.destinationViewController as? InsertWorkout {
//pass the data here but don't attempt to update your destination's view ui!!,
}
}
}
}

How do I pass the same textview, button, and label after clicking the Cell swift?

I would like to make it so that when the user clicks on the cell, it shows exactly everything in the cell. TextView, Buttons, and label. How can I do this?
Here is the code:
TableCell:
import UIKit
class TableViewCell: UITableViewCell {
#IBOutlet weak var textView: UITextView!
#IBAction func 1Button(sender: AnyObject) {
}
#IBAction func 2Button(sender: AnyObject) {
}
#IBOutlet weak var counter: UILabel!
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
}
}
TableViewController:
import UIKit
let reuseIdentifier = "Cell"
class UserFeedTableViewController: UITableViewController, ComposeViewControllerDelegate {
private var posts: [PFObject]? {
didSet {
tableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
Downloader.sharedDownloader.queryForPosts()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "queryFeeds:", name: queryNotification, object: nil)
}
// Notification SEL
func queryFeeds(notification: NSNotification) {
posts = notification.object as? [PFObject]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "postSegue" {
let nav = segue.destinationViewController as! UINavigationController
let composeVc = nav.topViewController as! ComposeViewController
composeVc.delegate = self
}
if segue.identifier == "commentsSegue" {
let vc = segue.destinationViewController as! CommentsViewController
let cell = sender as! UITableViewCell
let indexPath = tableView.indexPathForCell(cell)
let object = posts![indexPath!.row]
vc.postObject = object
}
}
//dismiss compose vc
func dismissComposeViewController(ViewController: ComposeViewController) {
dismissViewControllerAnimated(true, completion: nil)
}
func reloadTableViewAfterPosting() {
dismissViewControllerAnimated(true, completion: nil)
Downloader.sharedDownloader.queryForPosts()
}
}
extension ViewController {
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return posts?.count ?? 0
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! UserFeedTableViewCell
// Configure the cell...
if let posts = posts {
let object = posts[indexPath.row]
cell.textView?.text = object["post"] as? String
}
return cell
}

SWIFT: When i create a secondViewController, how to access to variables from original ViewController

When I go to my secondViewController using
let secondViewController:SecondViewController = SecondViewController()
self.presentViewController(secondViewController, animated: true, completion: nil)
I know I can send varibles to SECOND ONE using secondViewController.theNum = num, but while secondViewController is presented how to send varibles bar to the original ViewController.
Thing is I would like to start viewdidload() on original ViewController after this part of code is finished
self.dismissViewControllerAnimated(true, completion:nil)
Here are the full classes for two Views from a project where I pass data to a detail view and use a protocol/delegate method to return data to the first view:
View 1:
import UIKit
class Contacts: UITableViewController, dataUpdated {
//Declaring contact structure
struct contactInfo {
var name: String
var phoneNumber: String
}
var listOfContacts: [contactInfo] = []
var Duration = 100
//Sample contacts
var firstContact = contactInfo(name: "John Coffey" , phoneNumber: "(111) 111-1111")
var secondContact = contactInfo(name: "Cathy Kane" , phoneNumber: "(222) 222-2222")
//TableView delegates
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listOfContacts.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("contact", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = listOfContacts[indexPath.row].name
cell.detailTextLabel?.text = listOfContacts[indexPath.row].phoneNumber
return cell
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
listOfContacts.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
}
//ViewController lifecycle
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.leftBarButtonItem = self.editButtonItem()
listOfContacts.append(firstContact)
listOfContacts.append(secondContact)
}
//Passing details to detail VC
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ToDetail" {
let indexPath = self.tableView.indexPathForSelectedRow()
let theSelectedRow = listOfContacts[indexPath!.row]
let theDestination = (segue.destinationViewController as ContactDetails)
theDestination.contactName = theSelectedRow.name
theDestination.contactPhone = theSelectedRow.phoneNumber
} else if segue.identifier == "ToInput" {
(segue.destinationViewController as ContactInput).delegate = self
}
}
override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
let fromContact = listOfContacts[sourceIndexPath.row]
listOfContacts.removeAtIndex(sourceIndexPath.row)
listOfContacts.insert(fromContact, atIndex: destinationIndexPath.row)
}
//Delegate method to update the array with new contact
func didUpdateContact(senderClass: AnyObject, aName: String, aPhoneNumber: String) {
var newContact = contactInfo(name: aName, phoneNumber: aPhoneNumber)
listOfContacts.append(newContact)
println(listOfContacts)
self.tableView.reloadData()
}
}
View2:
import UIKit
protocol dataUpdated:NSObjectProtocol {
func didUpdateContact(senderClass: AnyObject, aName: String, aPhoneNumber: String)
}
class ContactInput: UIViewController, UITextFieldDelegate {
//Properties
var name = ""
var phoneNumber = ""
var delegate: dataUpdated?
#IBOutlet weak var nameField: UITextField!
#IBOutlet weak var phoneField: UITextField!
//Textfield delegates
func textFieldShouldReturn(textField: UITextField!) -> Bool {
if textField.tag == 1 {
self.name = textField.text
}
else {
self.phoneNumber = textField.text
}
textField.resignFirstResponder()
return true
}
//Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
self.nameField.delegate = self
self.phoneField.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
if name != "" && phoneNumber != "" {
self.delegate!.didUpdateContact(self, aName: self.name, aPhoneNumber: self.phoneNumber)
}
}
}

Resources