[英]Segue not passing data to ViewController
我正在嘗試從TableView傳遞數據,因此當按下某個單元格時,該單元格上的數據將被傳輸到viewController並在其中顯示它,但是我似乎無法使其正常工作。 我試過這些帖子:
在Swift中將數據從tableView傳遞到ViewController
但是我收到一個導致應用程序崩潰的錯誤,或者它什么都不顯示。 我想做的是讓一個用戶對另一個用戶發布的帖子發表評論,所以我試圖從該帖子中獲取信息以詳細顯示它,但這就是我遇到的麻煩。 這是我用來從表格視圖傳遞數據的功能:
func objectAtIndexPath(indexPath: NSIndexPath) -> PFObject {
return self.timelineData[indexPath.row] as! PFObject
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if(segue.identifier == "lookPosts"){
let VC = segue.destinationViewController as! DetailViewContoller
let indexPath: NSIndexPath! = self.tableView.indexPathForSelectedRow()
VC.post = self.objectAtIndexPath(indexPath!) as PFObject
}
}
這是DetailViewController的類:
import UIKit
import Parse
class DetailViewContoller: UIViewController, UITableViewDelegate, UITextViewDelegate {
@IBOutlet weak var usernameLabel: UILabel!
@IBOutlet weak var postTextView: UITextView!
@IBOutlet weak var commentTableView: UITableView!
var post: PFObject?
var commentView: UITextView?
var footerView: UIView?
var contentHeight: CGFloat = 0
var comments: [String]?
let FOOTERHEIGHT : CGFloat = 50;
override func viewDidLoad() {
super.viewDidLoad()
commentTableView.delegate = self
/* Setup the keyboard notifications */
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
//gets the current user username
usernameLabel.text = PFUser.currentUser()!.username as String?
if(post?.objectForKey("comments") != nil) {
comments = post?.objectForKey("comments") as? [String]
}
println(post)
println(post?.objectForKey("content"))
//self.postTextView.text = post?.objectForKey("content") as? String
}
override func viewWillAppear(animated: Bool) {
super.viewDidAppear(animated)
self.postTextView.text = post?.objectForKey("content") as? String
println(postTextView.text)
}
func keyBoardWillShow(notification: NSNotification) {
var info:NSDictionary = notification.userInfo!
var keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
var keyboardHeight:CGFloat = keyboardSize.height - 40
var animationDuration:CGFloat = info[UIKeyboardAnimationDurationUserInfoKey] as! CGFloat
var contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardHeight, 0.0);
self.commentTableView.contentInset = contentInsets
self.commentTableView.scrollIndicatorInsets = contentInsets
}
func keyBoardWillHide(notification: NSNotification) {
self.commentTableView.contentInset = UIEdgeInsetsZero
self.commentTableView.scrollIndicatorInsets = UIEdgeInsetsZero
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let count = comments?.count {
return count
}
return 0
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = commentTableView.dequeueReusableCellWithIdentifier("commentCell", forIndexPath: indexPath) as! CommentTableViewCell
cell.commentLabel?.text = comments![indexPath.row]
return cell
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
if self.footerView != nil {
return self.footerView!.bounds.height
}
return FOOTERHEIGHT
}
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
footerView = UIView(frame: CGRect(x: 0, y: 0, width: commentTableView.bounds.width, height: FOOTERHEIGHT))
footerView?.backgroundColor = UIColor(red: 243.0/255, green: 243.0/255, blue: 243.0/255, alpha: 1)
commentView = UITextView(frame: CGRect(x: 10, y: 5, width: commentTableView.bounds.width - 80 , height: 40))
commentView?.backgroundColor = UIColor.whiteColor()
commentView?.textContainerInset = UIEdgeInsetsMake(5, 5, 5, 5)
commentView?.layer.cornerRadius = 2
commentView?.scrollsToTop = false
footerView?.addSubview(commentView!)
let button = UIButton(frame: CGRect(x: commentTableView.bounds.width - 65, y: 10, width: 60 , height: 30))
button.setTitle("Reply", forState: UIControlState.Normal)
button.backgroundColor = UIColor(red: 0/0.0, green: 179/255.0, blue: 204/255.0, alpha: 100.0/100.0)
button.layer.cornerRadius = 5
button.addTarget(self, action: "reply", forControlEvents: UIControlEvents.TouchUpInside)
footerView?.addSubview(button)
commentView?.delegate = self
return footerView
}
//Hide keyboard after touching background
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
self.view.endEditing(true)
}
//remaining characters
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool{
if (text == "\n") {
textView.resignFirstResponder()
}
return true
}
func reply() {
post?.addObject(commentView!.text, forKey: "comments")
post?.saveInBackground()
if let tmpText = commentView?.text {
comments?.append(tmpText)
}
commentView?.text = ""
println(comments?.count)
self.commentView?.resignFirstResponder()
self.commentTableView.reloadData()
}
}
所以我的問題是:還有另一種傳遞數據的方式嗎? 還是數據沒有通過我在做什么錯?
這是達到上述目的的一種方法:
如果數據不大,則可以使用NSUserDefaults
。
首先將您的數據存儲到內存中:
NSUserDefaults.standardUserDefaults().setObject(yourObject, forKey: "yourKey")
在另一個viewController中從內存中讀取數據:
let yourObject = NSUserDefaults.standardUserDefaults().objectForKey("yourKey") as! PFObject
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.