The image below is a picture of a view with two uibarbuttons and a segmented control. The salesforce button when reached redirects the user to a website.How can I change the salesforce button to a different button when the third index of the segment control is reached ? This second button is meant to display a new view controller.
CodePudding user response:
I think it would be easier to change the appearance and the button behaviour instead of changing the button itself.
Sadly you didn´t provide any context if this view is from a storyboard or created by code. In my answer I´m assuming it´s from storyboard.
Create an outlet for you button:
@IBOutlet weak var myButton: UIButton!
From your
UISegmentedControl
in storyboard create anIBAction
in your viewcontroller. That action should look something like this.@IBAction func segmentedControl(sender: AnyObject) { configureButton(for: segmentedControl.selectedSegmentIndex) }
Then create that function:
func configureButton(for index: Int){ switch index{ case 1: // assign the function that should execute as soon as the button gets pressed myButton.addTarget(self, action: #selector(functionName), for: .touchUpInside) // change title myButton.setTitle("title for button", for: .normal) // add all other cases // add default default: print("not implemented") } }
If no IBAction
is defined for your myButton
you would need to call the configureButton
function from your viewDidLoad
function.
CodePudding user response:
Firstly get the index of the selected segment using, .selectedSegemtIndex
property.Then, check the condition stating that, if the index is equals your target segment index, define statements to change the title, background color, or even the action of the button if required.