1

I have MyTableViewCell class and their .xib

on my TableViewController want to click button inside cell and then goto my other UIViewController , UITableViewDataSource, UITableViewDelegate

from this

 class OverviewViewController: UITableViewController 
 { 

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.registerNib(UINib(nibName: "MyTableViewCell", bundle: nil), forCellReuseIdentifier: "MyTableViewCell")
}

To this

class HistoryController: UIViewController , UITableViewDataSource,   UITableViewDelegate{

@IBOutlet var MyTable: UITableView! 

override func viewDidLoad() {
    super.viewDidLoad()

    self.MyTable.registerNib(UINib(nibName: "HistoryTableViewCell", bundle: nil), forCellReuseIdentifier: "HistoryTableViewCell")


}

historyBtn in MyTableViewCell

an I use that

         ....

     cell.historyBtn.tag = indexPath.row 
     cell.historyBtn.addTarget(self, action: "getHistoryList", forControlEvents: UIControlEvents.TouchUpInside)

     ....

My HistoryList Method

    func getHistoryList()
    {
     presentViewController(HistoryController(), animated: true, completion: nil)

  }

But when go to the HistoryController I get this error :

unexpectedly found nil while unwrapping an Optional value

MyTableView nil

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
idris yıldız
  • 2,097
  • 20
  • 22

2 Answers2

0

you need delegate for you custom cell

enter image description here

enter image description here

after you should use it in action button inside cell

enter image description here

in your tableView controller you add protocol

enter image description here

in your tableView controller set delegate to cell

 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

   cell.delegate = self;

after you can use your delegate method in your tableview controller

-(void)selectButtonWithDate:(NSDate *)date
{
 [self.navigationController pushViewController:picker animated:YES];//or another transition code
}

P.S I'm sorry my example on objective-c code but , it is only right way to do what you want.

Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
Joe Hallenbeck
  • 1,452
  • 12
  • 24
0

Solution for Swift:

You have to write a delegate method within your ViewController containing the tableView like this:

protocol PushButtonDelegate {

    func pushBtn()
}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, PushButtonDelegate {

}

Within your ViewController you have to define the function pushBtn like this:

protocol PushButtonDelegate {

     func pushBtn()
}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, PushButtonDelegate {

     func pushBtn() {

        let stb = UIStoryboard(name: "Main", bundle: nil)
        let vc = stb.instantiateViewControllerWithIdentifier("UserSettingsVC")            
        self.presentViewController(vc, animated: true, completion: nil)
    }
}

Next step is to call the delegate methos within your TableViewCell.swift like this:

import UIKit

class TableViewCell: UITableViewCell {

    var delegate : PushButtonDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

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

        // Configure the view for the selected state
    }

    @IBAction func menuBtn(sender: AnyObject) {

        delegate?.pushBtn()
    }
}

The last step is to set the delegate in the cellForRowAtIndexPath function at ViewController like this:

protocol PushButtonDelegate {

    func pushBtn()
}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, PushButtonDelegate {

    func pushBtn() {

    let stb = UIStoryboard(name: "Main", bundle: nil)
    let VC4 = stb.instantiateViewControllerWithIdentifier("UserSettingsVC")
    VC4.title = "Settings"

    self.presentViewController(VC4, animated: true, completion: nil)
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("tableViewCell") as! TableViewCell            
    cell.delegate = self     
    return cell

}

DONE. If you need any further help, just let me know.

David Seek
  • 16,783
  • 19
  • 105
  • 136