Reputation: 111
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.
Upvotes: 2
Views: 1650
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.
didSelectRowWithIndexPath
use the delegate to pass the selected cell info and pop the view controller.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
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
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
Reputation: 1800
@IBOutlet
tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
. Here you can get your cell with [tableView cellForRowAtIndexPath:indexPath]
[cell.yourButton setTitle...]
Upvotes: 1