In a storyboard there are 5 buttons. If I pressed any button, it will go into new storyboard. In new storyboard I just want to find which button was pressed to come here.
Asked
Active
Viewed 260 times
-1
-
Check out https://stackoverflow.com/q/24222640/3151675 and https://stackoverflow.com/q/33308900/3151675. – Tamás Sengel Jul 04 '19 at 18:50
-
Why does the storyboard need to know which button was used? The controller with the buttons should provide whatever data is appropriate to the new view controller. – rmaddy Jul 04 '19 at 18:56
-
for showing different data in one table view. – Taohidur Imran Jul 04 '19 at 19:04
-
As I said, give the data to the new view controller. The new controller doesn't need to know the button. – rmaddy Jul 04 '19 at 22:37
2 Answers
0
You can create a State
class where you can set the button type represented by an enum. Then access the value in the new storyboard.
struct State {
static var btnType: ButtonType?
}
enum ButtonType {
case buttonA
case buttonB
// ..
}
In the main view controller where there are buttons, set the state on button tap.
class ViewController: UIViewController {
func buttonADidTap(_ sender: Any) {
State.btnType = .buttonA
// display the other view controller, say MyVCA
}
func buttonBDidTap(_ sender: Any) {
State.btnType = .buttonB
// ..
}
}
In the child view controller, access the state data in viewDidLoad
.
class MyVCA: UIViewController {
override func viewDidLoad() {
if let btnType = StateData.btnType {
switch btnType:
case .buttonA:
// ...
}
}
}

John Doe
- 2,225
- 6
- 16
- 44
0
Add tag to button (in code or in storyboard) and send tag to next ViewController of pressed button.
class ViewController: UIViewController {
override func viewDidLoad() {
firstButton.tag = 1
secondButton.tag = 2
thirdButton.tag = 3
}
func goToNextVC(buttonTag: Int) {
let vc = SecondVC()
vc.buttonTag = buttonTag
self.navigationController.push(vc, animated: true)
}
// action for all buttons if he just only send tag
func buttonsDidTap(_ sender: Any) {
goToNextVC(buttonTag: sender.tag)
}
}
And Second ViewController
class SecondVC: UIViewController {
var buttonTag: Int?
override func viewDidLoad() {
if buttonTag != nil {
switch buttonTag:
case 0:
// ...
}
}
}

Dmitriy Stepanov
- 17
- 1
- 4