为什么prepareForSegue无效

时间:2015-10-27 05:18:24

标签: ios swift

我的解析数据库中包含所有用户的集合视图,每个单元格都有唯一的标签和图像。单击该单元格时,我希望标签和图像显示在另一个视图控制器的展开视图中。该视图控制器位于滚动视图内。视图控制器正确放置在滚动视图中。我收到并收到错误:http://puu.sh/kZaRf/3a212b4063.png以下是我的代码

 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: friendcellView = collectionView.dequeueReusableCellWithReuseIdentifier("friendcell", forIndexPath: indexPath) as! friendcellView

    dispatch_async(dispatch_get_main_queue()) {
        self.collectionView.reloadData()
    }


    cell.friendname.text = arrayOfFriendsNames[indexPath.row]
    cell.friendpic.image =  arrayOfFriends[indexPath.row]
    //cell.friendpic.image = UIImage(named: arrayOfFriendsTest[indexPath.row])
    cell.friendpic.layer.cornerRadius = cell.friendpic.frame.size.width/2;
    cell.friendpic.clipsToBounds = true
    self.performSegueWithIdentifier("friendaccess", sender: self)

    return cell
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    print("Cell \(indexPath.row) selected")
    print(arrayOfFriendsNames[indexPath.row])
    self.performSegueWithIdentifier("friendaccess", sender: self)
}

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if(segue.identifier == "friendaccess"){
       (segue.destinationViewController as! FriendProfile).friendname == (sender as! friendcellView).friendname.text
        (segue.destinationViewController as! FriendProfile).friendimage == (sender as! friendcellView).friendpic.image
    }
}

以下是FriendProfile的代码

class FriendProfile: UIViewController {


@IBOutlet weak var friendimage: UIImageView!
@IBOutlet weak var friendname: UILabel!



override func viewDidLoad() {
    super.viewDidLoad()


    friendimage.layer.cornerRadius = self.friendimage.frame.size.width / 2;
    friendimage.clipsToBounds = true



    // Do any additional setup after loading the view.
}

1 个答案:

答案 0 :(得分:0)

如错误所示,当mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.app_name, R.string.app_name) { @Override public void onDrawerSlide(View drawerView, float slideOffset){ hideKeyboard(true, ActivityName); } }; public static void hideKeyboard(boolean val, Activity activity) { View view; view = activity.getWindow().getCurrentFocus(); if (val == true) { InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0); } } 实际上是segue.destinationViewController的实例时,您试图将FriendProfile投射到FriendProfilePages

您要么转换为错误的类型,要么在故事板中为视图控制器设置了错误的类型。基于上面的代码,我猜它可能是后一个问题。

在场景的根目录中选择具有标识符friendaccess的segue目标的视图控制器,打开身份检查器(command-option-3),然后更改“自定义类”字段中的值从FriendProfilePagesFriendProfile

此外,以下代码行正在对视图控制器的IBOutlets进行相等性检查。我怀疑这是你想要的。您可能需要使用赋值语句(尽管此时在视图控制器生命周期中,尚未创建连接到插座的视图)。

(segue.destinationViewController as! FriendProfile).friendname == (sender as! friendcellView).friendname.text
(segue.destinationViewController as! FriendProfile).friendimage == (sender as! friendcellView).friendpic.image