J.S.O
J.S.O

Reputation: 111

How to change button name when cell selected in UITableView in swift?

I have two UIButton and when you click on each you will go to table view controller and there you can select something.

My question is how to change the button title on selection of any cell. I want to replace button title with selected cell's name.

enter image description here

Upvotes: 2

Views: 1650

Answers (4)

Pradeep K
Pradeep K

Reputation: 3661

Instead of using two navigation controllers you can use a single NavC and then make your view controller with the two buttons as the root view controller. Then when the buttons are pressed you load the appropriate table view controller. Also if your table view controller has the same data source then you can use a single table view controller itself.

Then depending on which button is pressed you push the appropriate table view controller but in the prepareForSegue keep track of which button was pressed so that you can change its title when a cell is selected in the table view controller.

To pass the selected cell details from the table view controller you can make use of a delegate which your view controller can implement and the table view controller will call and pass the selected cell details.

So in short do this.

  1. Make your navC as the initial view controller.
  2. Make your view controller with the two buttons as the root view controller of the navC
  3. In the IBAction of the two buttons store which button was pressed so that you will know which buttons's title has to be changed.
  4. When you load table view controller set the view controller as a custom delegate.
  5. In the language list table view controller's didSelectRowWithIndexPath use the delegate to pass the selected cell info and pop the view controller.
  6. In the delegate method of the view controller change the button title that you have stored in step 3 to the selected cell's text.

I have another solution which I have uploaded here. It does not contain any custom view controllers except the ViewController that has the two buttons. It makes use of segues and unwind segues.

http://www.filedropper.com/twobuttonnavc

Upvotes: 0

vivek takrani
vivek takrani

Reputation: 4010

There are two ways to pass data back to the previous viewController.

1) Using Delegates, check this

2) Using Notifications, check this

Upvotes: 2

Doug
Doug

Reputation: 3190

If you were to make your navigation controller into you root view controller and put the view controller you wish to access the buttons from as its top view controller then you could do the following when a cell is tapped:

if let topVC = navigationController?.topViewController as? MyButtonViewController {
    topVC.button1Name.setTitle("New Title", forState: .Normal)
}

Then you can hide the navigation bar on the top view controller with the following:

override func viewWillAppear(animated: Bool) {
    navigationController?.setNavigationBarHidden(true, animated: animated)
}

override func viewWillDisappear(animated: Bool) {
    navigationController?.setNavigationBarHidden(false, animated: animated)
}

Upvotes: 1

anatoliy_v
anatoliy_v

Reputation: 1800

  1. Bind button in your cell as @IBOutlet
  2. When cell selected delegate method tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath. Here you can get your cell with [tableView cellForRowAtIndexPath:indexPath]
  3. Change your button's title [cell.yourButton setTitle...]

Upvotes: 1

Related Questions