尝试使用`prepareForSegue`但由于某种原因它不会被调用。我正确地命名了segue并且我在我的应用程序的其他部分使用了几乎相同的代码但是由于某种原因,当我进入下一个视图时,应用程序一直在崩溃说找到了nil。
TableViewController:
class ChartsTableViewController: UITableViewController {
var song: NSURL!
var player:AVPlayer!
@IBOutlet var backButton: UIBarButtonItem!
var songs = [PFObject]()
var parseImage: PFFile!
@IBOutlet var hideDislikeButton: UIButton!
@IBOutlet var hideLikeButton: UIButton!
@IBOutlet var segment: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
var stopButton = UIBarButtonItem(title: "Stop", style: .Plain, target: self, action: Selector("stopMusic"))
self.navigationItem.rightBarButtonItem = stopButton
var query = PFQuery(className: "Songs")
query.orderByDescending("createdAt") //new songs
query.limit = 100
query.cachePolicy = kPFCachePolicyCacheThenNetwork
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
//self.songs.removeAll(keepCapacity: false)
for object in objects {
self.songs.append(object as PFObject)
}
self.tableView.reloadData()
}
}
self.tableView.backgroundColor = UIColor.greenColor()
self.tableView.separatorColor = UIColor.blackColor()
Flurry.logEvent("Charts View")
}
func stopMusic() {
if (isPlaying == true) {
if (player.rate > 0) && (player.error == nil) {
//playing
player.pause()
}
isPlaying = false
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func objectAtIndexPath(indexPath: NSIndexPath!) -> PFObject! {
var obj : PFObject? = nil
if(indexPath.row < self.songs.count){
obj = self.songs[indexPath.row] as PFObject
}
return obj
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return songs.count
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "commentSegue" {
if let indexPath = self.tableView.indexPathForSelectedRow() {
let object: PFObject = songs[indexPath.row]
println(indexPath.row)
println(songs)
println(object)
(segue.destinationViewController as CommentsViewController).detailItem = object
}
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TableViewCell
cell.contentView.layer.cornerRadius = 20.0
cell.contentView.layer.masksToBounds = true
cell.contentView.layer.borderColor = UIColor.whiteColor().CGColor
cell.contentView.layer.borderWidth = 8
let objectSong: PFObject = songs[indexPath.row]
println(objectSong)
parseImage = objectSong.valueForKey("picture") as PFFile
let imgURL = NSURL(string: parseImage.url)
cell.songImage.layer.borderWidth = 1
cell.songImage.layer.masksToBounds = false
cell.songImage.layer.borderColor = UIColor.blackColor().CGColor
cell.songImage.layer.cornerRadius = cell.songImage.frame.height/4
cell.songImage.clipsToBounds = true
cell.songImage.sd_setImageWithURL(NSURL(string: parseImage.url), placeholderImage: UIImage(named: "MeipLogo"))
cell.songTitleLabel.text = objectSong.valueForKey("title") as? String
cell.artistNameLabel.text = objectSong.valueForKey("artistName") as? String
let user = PFUser.currentUser()
return cell
}
NextViewController:
@IBOutlet var commentsView: UITableView!
var commentsArray = [String]()
@IBOutlet var songImage: UIImageView!
var songObject: PFObject!
var detailItem: AnyObject? {
didSet {
// Update the view.
self.configureView()
}
}
override func viewDidLoad() {
super.viewDidLoad()
songObject = configureView()
songImage.layer.borderWidth = 1
songImage.layer.masksToBounds = false
songImage.layer.borderColor = UIColor.blackColor().CGColor
songImage.layer.cornerRadius = songImage.frame.height/4
songImage.clipsToBounds = true
println(songObject)
let parseImage: PFFile = songObject.valueForKey("picture") as PFFile
songImage.image = UIImage(data: parseImage.getData())
if songObject["comments"] != nil {
commentsArray = songObject["comments"] as [String]
}
commentsView.delegate = self
commentsView.dataSource = self
}
// MARK: UITextFieldDelegate Methods
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return commentsArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("textCell", forIndexPath: indexPath) as UITableViewCell
if (commentsArray.count == 0) {
cell.textLabel.text = "No Comments! Be The First One smile emoticon"
} else {
cell.textLabel.text = commentsArray[indexPath.row]
}
return cell
}
}
答案 0 :(得分:0)
听起来detailItem
未按预期设置,因此当您在configureView
中展开时会发生崩溃但值为nil。这种设计也很奇怪。我会做这样的事情:
var detailItem: AnyObject? {
didSet {
self.configureView()
}
}
func configureView() -> PFObject? {
if let song = self.detailItem as? PFObject {
// Do something with the `song` here
return song
}
return nil
}
答案 1 :(得分:0)
在viewDidLoad
中detailItem
尚未设置,因此会崩溃。为什么?可能是因为prepareForSegue
中的分配错误,请记录对象和indexPath。
此外,您可能希望将songObject
的分配移动到configureView
以及将来最终对所述对象执行的操作。从configureView
或viewDidLoad
拨打didSet
- 两者都是常见的...