我正在尝试使用swift 2创建一个应用程序,该应用程序将在用户名称中添加一个用户名称后,我希望用户能够点击该名称并将其带到另一个页面他们可以看到细胞的含义。 这就是我所拥有的,但我不确定如何创建新的segue或视图控制器。所有名称都存储在CoreData中。
查看控制器:
import UIKit
import CoreData
class ViewController: UIViewController,UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var people = [NSManagedObject]()
@IBAction func addName(sender: AnyObject) {
let alert = UIAlertController(title: "New Client",
message: "Add a new client",
preferredStyle: .Alert)
let saveAction = UIAlertAction(title: "Save",
style: .Default,
handler: { (action:UIAlertAction) -> Void in
let textField = alert.textFields!.first
self.saveName(textField!.text!)
self.tableView.reloadData()
})
let cancelAction = UIAlertAction(title: "Cancel",
style: .Default) { (action: UIAlertAction) -> Void in
}
alert.addTextFieldWithConfigurationHandler {
(textField: UITextField) -> Void in
}
alert.addAction(saveAction)
alert.addAction(cancelAction)
presentViewController(alert,
animated: true,
completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
title = "Clients"
tableView.registerClass(UITableViewCell.self,
forCellReuseIdentifier: "Cell")
}
func tableView(tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return people.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell =
tableView.dequeueReusableCellWithIdentifier("Cell")
let person = people[indexPath.row]
cell!.textLabel!.text =
person.valueForKey("name") as? String
return cell!
}
func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) {
// either present the view controller for new page or perform the Segue
//self.people = self.tableView[indexPath.row]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func saveName(name: String) {
//1
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
//2
let entity = NSEntityDescription.entityForName("Person",
inManagedObjectContext:managedContext)
let person = NSManagedObject(entity: entity!,
insertIntoManagedObjectContext: managedContext)
//3
person.setValue(name, forKey: "name")
//4
do {
try managedContext.save()
//5
people.append(person)
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//1
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
//2
let fetchRequest = NSFetchRequest(entityName: "Person")
//3
do {
let results =
try managedContext.executeFetchRequest(fetchRequest)
people = results as! [NSManagedObject]
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
}
ViewController2:
import Foundation
class ViewController2{
}
答案 0 :(得分:1)
在您的故事板中
在storyboard
中,您需要拖动要从中调出的viewControllers之间的关系。在您的情况下,从ViewController
到ViewController2
。然后选择该关系并转到“属性检查器”并添加标识符。
在您的代码中
在你的tableView didSelectRowAtIndexPath
中添加以下行:
self.performSegueWithIdentifier("YOUR IDENTIFIER ID", sender: nil)
添加此segue功能
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!)
{
// you can skip this line if you want, but good to have if you have multiple segues
if (segue.identifier == "YOUR IDENTIFIER ID")
{
// create an instance to your second viewController
let second = segue.destinationViewController as! ViewController2
// now you can access variables and function in ViewController2 by using "second"
}
}
然后,当您将ViewController中的行单击到ViewController2时,您应该可以进行segue
在您的故事板中
在storyboard
中,您需要拖动要从中调出的viewControllers之间的关系。在您的情况下,从ViewController
到ViewController2
。然后选择该关系并转到“属性检查器”并添加标识符。
在您的代码中
在你的tableView didSelectRowAtIndexPath
中添加以下行:
self.performSegueWithIdentifier("YOUR IDENTIFIER ID", sender: nil)
添加此segue功能
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!)
{
// you can skip this line if you want, but good to have if you have multiple segues
if (segue.identifier == "YOUR IDENTIFIER ID")
{
// create an instance to your second viewController
let second = segue.destinationViewController as! ViewController2
// now you can access variables and function in ViewController2 by using "second"
}
}
然后,当您将ViewController中的行单击到ViewController2时,您应该可以进行segue
<强>更新强>
您的代码示例
func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("YOUR IDENTIFIER ID", sender: self.tableView[indexPath.row])
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!)
{
let secondViewController = segue.destinationViewController as! ViewController2
// secondViewController = name of the instance to ViewController2
// second = the name of a variable that you have created in ViewController2
// sender is the value that was passed as a parameter from self.tableView[indexPath.row] in performSegueWithIdentifier. You have to check what this value consists
secondViewController.second = sender
}
如果你这样做就可以了。