2014-10-16 44 views
1

访问变量当我去使用SWIFT:当我创建一个secondViewController,如何从原来的ViewController

let secondViewController:SecondViewController = SecondViewController() 

self.presentViewController(secondViewController, animated: true, completion: nil) 

我知道我可以用发送可变因素对第二个我secondViewController secondViewController.theNum = NUM​​,但同时介绍secondViewController如何将变量条发送到原始ViewController。

事情是我想先在原来的视图控制器的viewDidLoad()这部分代码完成后

self.dismissViewControllerAnimated(true, completion:nil) 
+1

您可以通过secondViewController上的'self.presentingViewController'访问原始viewController。 – 2014-10-16 21:48:15

+0

为什么不使用viewWillAppear或viewDidAppear来调用代码? – marcusficner 2014-10-16 22:22:14

回答

1

下面是完整的类从一个项目中的两个意见,我将数据传递到详图和使用协议/委托方法将数据返回到所述第一视图:

视图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() 
    } 

} 

视图2:

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) 
     } 
    } 
} 
相关问题