Ich lerne schnell, und ich habe Probleme, ein Segment zu feuern, wenn eine TableViewCell berührt wird, die eine URL an eine zweite Ansicht übergeben soll, die im Moment nur ein angezeigt wird es in einem Etikett. Ich erstelle dynamisch (ich habe gesehen, dass Leute Programmatisch verwenden, was wahrscheinlich das richtige Wort ist) jede einzelne Zelle, also habe ich im Storyboard kein Objekt, um mit einer anderen Ansicht zu verlinken, außer der Ansicht selbst ... und das ist Was ich getan habe. Also habe ich den ersten View-Controller an den zweiten angeschlossen und den Code hinzugefügt, um den Übergang durchzuführen.Swift - Segue auf Dynamic TableCell
Ich bin mir nicht sicher, ob es richtig ist, mein Wissen kommt von Tutorials, die nicht genau erklären, was ich machen wollte.
da unten ist der Code der beiden Ansichten.
ersten Blick
import UIKit
protocol sendInfoDelegate{
func userDidEnterInfo(WhichInfo info : String)
}
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var tableData = []
@IBOutlet weak var redditListTableView: UITableView!
var selectedCellURL : String?
var delegate : sendInfoDelegate? = nil
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// change the link to change the json source
getRedditJSON("http://www.reddit.com/.json")
}
//
//Creates a connection via a task (networktask) then parses the json
//
func getRedditJSON(whichReddit : String){
let mySession = NSURLSession.sharedSession()
let url: NSURL = NSURL(string: whichReddit)
let networkTask = mySession.dataTaskWithURL(url, completionHandler : {data, response, error -> Void in
var err: NSError?
var theJSON = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSMutableDictionary
let results : NSArray = theJSON["data"]!["children"] as NSArray
dispatch_async(dispatch_get_main_queue(), {
self.tableData = results
self.redditListTableView.reloadData()
})
})
networkTask.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//needs to be implemented
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData.count
}
//creates the whole table
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
let redditEntry : NSMutableDictionary = self.tableData[indexPath.row] as NSMutableDictionary
cell.textLabel?.text = redditEntry["data"]!["title"] as? String
cell.detailTextLabel?.text = redditEntry["data"]!["author"] as? String
return cell
}
// action to be taken when a cell is selected
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let redditEntry : NSMutableDictionary = self.tableData[indexPath.row] as NSMutableDictionary
self.selectedCellURL = redditEntry["data"]!["url"] as? String
self.performSegueWithIdentifier("passInfo" , sender: indexPath)
println(self.selectedCellURL!)
if delegate != nil {
let information:String = self.selectedCellURL!
println("ciao")
delegate?.userDidEnterInfo(WhichInfo: information)
self.navigationController?.popViewControllerAnimated(true)
}
zweite Ansicht
import UIKit
class WebPageController : UIViewController, sendInfoDelegate {
var infoFromSVC: String?
@IBOutlet weak var labelVC: UILabel!
func userDidEnterInfo(WhichInfo info: String) {
self.infoFromSVC = info
labelVC.text = infoFromSVC
}
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 == "passInfo"{
let firstVController : ViewController = segue.destinationViewController as ViewController
firstVController.delegate = self
}
}
}
Dank.
Wie ist es möglich, die Dateneigenschaft des zweiten View-Controllers von der ersten abzurufen? Der erste View-Controller weiß nicht, welche Eigenschaften der zweite View-Controller hat. – DeepVinicius
Sie müssen segue.destinationViewController in SecondView-Controller umwandeln. Ich habe für meine Antwort notwendige Editierungen vorgenommen –