2016-07-02 10 views
0

私は主にUITextViewsで構成される多くのUITableViewCellを持っています。UITableViewCellのUITextViewはセグを防ぎます

今の問題は、セルをクリックしたとき、それはこれが、それは難しい私は別のVCにセグエできるようにするために作られた-didSelectRowAtIndexPath:

を開始していないということです。

私は数多くのことを試しましたが、そのうちの1つはタップジェスチャ認識機能を追加してから手動でセグを試みるようにしました。-presentViewController:そこにもいくつか問題がありました。これを行うより一般的で簡単な方法はありますか?私はまた、私が表示しようとしているviewControllerにいくつかの値を渡す必要があります。

EDIT

これは私がジェスチャーを使用してしようとしていたものです。ここでは上記のこの方法を使用

func tap(sender: UITapGestureRecognizer) { 

    let tapLocation = sender.locationInView(self.tableView) 

    let indexPath = self.tableView.indexPathForRowAtPoint(tapLocation) 

    let VC1 = self.storyboard!.instantiateViewControllerWithIdentifier("showPushID") 
    let navController = UINavigationController(rootViewController: VC1) 
    self.presentViewController(navController, animated:true, completion: nil) 

は動作しますが、私は/カント私が持っているいくつかの変数にパラメータを渡す方法を知りませんコントローラ。

この方法:

let destinationVC = showPushNotificationMessage() 
    destinationVC.pushMessage = "test" 
    destinationVC.dateOpened = "test" 


    presentViewController(destinationVC, animated: true, completion: nil) 

は私にこのエラーを与える:

fatal error: unexpectedly found nil while unwrapping an Optional value 
+0

コーディングハッピーとして。 –

+0

@UmairAfzalが –

答えて

2

その非常に単純な男:)。方法instantiateViewControllerWithIdentifierUIViewControllerを返すので、UIViewControllerの変数にアクセスするには、以下のように型を返すだけです。

あなたのViewControllerにデータを渡す方法

let destinationVC = self.storyboard!.instantiateViewControllerWithIdentifier("showPushID") as! YourDestinationViewController 
destinationVC.pushMessage = "test" 
destinationVC.dateOpened = "test" 
let navController = UINavigationController(rootViewController: destinationVC) 
self.presentViewController(destinationVC, animated:true, completion: nil) 

わからないあなたのセル上のTextViewを取っていますが、あなたのCustomTableCellためのデリゲートメソッドを作成することができ、その後のタップで、あなたのデリゲートメソッドを呼び出すことができますすることを目的、手順に従ってください

デリゲートメソッド

@objc protocol MyCustomCellDelegate 
{ 
    func textViewTapped(cell : MyCustomTableViewCell) 
} 
を作成します。あなたのViewController継承 MyCustomCellDelegate

カスタムテーブルビューのセル

class MyCustomTableViewCell: UITableViewCell 
{ 
@IBOutlet weak var myTextView : UITextView! 
weak var delegate : MyCustomCellDelegate? 

override func awakeFromNib() { 
    super.awakeFromNib() 
    myTextView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.tap(_:)))) 
} 

override func setSelected(selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 
} 

func tap(sender: UITapGestureRecognizer) { 
    self.delegate?.textViewTapped(self) 
} 
} 

あなたはUITableViewDelegate でやっているようとfunc tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

であなたのViewController

func textViewTapped(cell: MyCustomTableViewCell) 
{ 
    let vc = self.storyboard!.instantiateViewControllerWithIdentifier("myDestView") 
    let navController = UINavigationController(rootViewController: vc) 
    self.presentViewController(navController, animated:true, completion: nil) 
} 

セットセルのデリゲートにデリゲートメソッドを書きます

cell.delegate = self 
あなたのコードを表示してください

アクセスしますセル

let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier") as! AssessmentTableViewCell 

:)

+1

に更新されました。また、あなたの答えを編集するだけです:self.presentViewController(destinationVC、animated:true、completed:nil)と書くと、destinationVCの代わりにnavControllerになるはずです –

+0

ハッピー・トゥ・ヘルプ・バディ –

関連する問題