2 Replies
      Latest reply on May 12, 2019 1:37 PM by DelawareMathGuy
      UNIVAC Coder Level 1 Level 1 (0 points)

        I need to open the view which allows the user to enter their name.  I modified the Master-Detail View Controller code to change the NSDate() to an UITextField hoping to make use of UITextFieldDelegate to input the name.  The .becomesFirstResponder() is not working as I would expect.

        What am I missing?

            func insertNewObject(_ sender: Any) {

                let aName = UITextField(frame: CGRect(x: 0, y: 0, width: 100, height: 22))

                aName.text = "Bowler's Name"

                aName.tag = 502

                aName.delegate = self


                //objects.insert(NSDate(), at: 0)  orginal code

                let indexPath = IndexPath(row: objects.count-1, section: 0) //was row: 0

                tableView.insertRows(at: [indexPath], with: .automatic)


                tableView.cellForRow(at: indexPath)?.becomeFirstResponder()

                //need code to open input view and get a bowler's name.



        • Re: UITextFieldDelegate
          DelawareMathGuy Level 3 Level 3 (160 points)

          hi UNIVAC Coder,


          there are plenty of things going on here.


          the default master-detail VC template model is a good, general place to start; and although it declares the MasterViewController as having


          var objects = [Any]()


          this array of objects is really (as the program uses it) an array of NSDates.  if you change the declaration to


          var objects = [NSDate]()


          you'll see that what you're trying to do (adding a UITextField to this array) is not really the right direction to take.


          if you really want to show the names of bowlers (i.e., strings), start with


          var objects: [String] = ["Fred", "Murray", "Ginger", "Rogers"]


          then change the insertNewObject function to be


            func insertNewObject(_ sender: Any) {
             objects.insert("Inserted Name", at: 0)
             let indexPath = IndexPath(row: 0, section: 0)
             tableView.insertRows(at: [indexPath], with: .automatic)


          two small changes in MasterViewController are required, so that you remove casts of the object items as NSDates (change both occurrences)


          let object = objects[indexPath.row] // as! NSDate


          and one small change is required in DetailViewController to work with Strings rather than NSDates


            var detailItem: String? {
            didSet {
                // Update the view.


          now you should have a functioning list-of-strings display application.


          from here, editing is a whole new level.  two quick thoughts for you to research:


          1. i highly recommend Mark Moeykins's Moeykens's (Big Mountain Studios) Youtube videos, especially the one titled "Popups to Add Data - Part 13 - Itinerary App" (and this entire series).  this shows how you bring up a simply dialog to capture the name of a new bowler in response to pressing the + button, BEFORE you then call insertNewObject  to add the bowler's names to the objects array.
          2. if you want to allow the user to edit names after-the-fact, in place in the UITableView, that's a lot more work.  the cells in the UITableView would have to be of a different type (not a Basic UITableViewCell with a simple UILabel, but a custom type with with, yes, a UITextField.)  managing this might be beyond where you are now, so i'd recommend putting this off until you really want or need to do it.


          hope this helps get you started, and good luck,