似乎无法获得SegmentedControl变量以在listView中显示

时间:2017-10-14 05:27:11

标签: ios swift segmentedcontrol

我是开发iOS应用程序的极端初学者,所以对我来说很容易:)

我正在创建一个核心数据库,您可以在其中添加记录,然后使用Swift 3在Xcode上查看它们。我在存储/显示男性/女性的分段控件时遇到了一些问题。我可以正确显示文本字段和标签,但由于某种原因无法使其正常工作。

friendsViewController.swift

//
//  friendsViewController.swift
// 
//
//  Created by  on 11/10/17.
//  Copyright © 2017 . All rights reserved.
//

import UIKit
import CoreData

class friendsViewController: UIViewController {
    //OUTLETS
    @IBOutlet weak var firstName: UITextField!
    @IBOutlet weak var lastName: UITextField!
    @IBOutlet weak var genderSegControl: UISegmentedControl!
    @IBOutlet weak var ageStepper: UIStepper!

    //AGECOUNT
    @IBOutlet weak var ageLabel: UILabel!
    @IBOutlet weak var address: UITextField!

    var context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

    //actions
    @IBAction func ageStepperStep(_ sender: UIStepper) {
        let step = Int(sender.value) // using an integer variable
        ageLabel.text = String(step)
        // label1.text = Int(sender.value).description
        // another way without using any integer variable
    }

    //SAVE FRIEND BUTTON
    @IBAction func saveFriendBtn(_ sender: Any) {
        if firstName.text != "" && lastName.text != "" {
            var gender : String?
            if self.genderSegControl.selectedSegmentIndex == 0 {
                gender = "male"
            }
            else {
                gender = "female"
            }

            let newFriend = NSEntityDescription.insertNewObject(forEntityName: "Friends", into: context)
            newFriend.setValue(self.firstName.text, forKey: "firstName")
            newFriend.setValue(self.lastName.text, forKey: "lastName")
            newFriend.setValue(self.address.text, forKey: "address")
            newFriend.setValue(self.ageLabel.text, forKey: "age")
            newFriend.setValue(gender!, forKey: "gender")

            //SAVE THE CONTEXT
            do {
                try context.save()
            }
            catch{
                print(error)
            }
        }
        else {
            print("please enter your first and last name!")
        }
    }
} //end class

friendslistViewController.swift(显示单元格中所有记录的屏幕)

//
//  friendslistViewController.swift
// 
//
//  Created by on 11/10/17.
//  Copyright © 2017. All rights reserved.
//

import UIKit
import CoreData

class friendslistViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    //outlets
    @IBOutlet weak var tableView: UITableView!

    var userArray:[Friends] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self

        self.fetchData()
        self.tableView.reloadData()
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return userArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        //save first users data into array
        let name = userArray[indexPath.row]
        cell.textLabel!.text = name.firstName! + " " + name.lastName! + " " + name.age! + " " +
            name.gender! + " " +
            name.address!

        return cell
    }

    func fetchData(){
        let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

        do{
            userArray = try context.fetch(Friends.fetchRequest())
        }
        catch{
            print(error)
        }
    }
} //end class

0 个答案:

没有答案