繁体   English   中英

单击分段 controller 后如何将 UIButton 更改为不同的 Uibutton?

[英]How to change UIButton to a different Uibutton after clicking segmented controller?

下图是带有两个 uibarbuttons 和一个分段控件的视图图片。 到达 salesforce 按钮时,会将用户重定向到网站。当到达段控件的第三个索引时,如何将 salesforce 按钮更改为不同的按钮? 第二个按钮用于显示新视图 controller。

在此处输入图像描述

我认为更改外观和按钮行为比更改按钮本身更容易。

遗憾的是,如果此视图来自 storyboard 或由代码创建,您没有提供任何上下文。 在我的回答中,我假设它来自 storyboard。

  • 为您创建一个出口按钮:

     @IBOutlet weak var myButton: UIButton!
  • 从 storyboard 中的UISegmentedControl在您的视图控制器中创建一个IBAction 该操作应该看起来像这样。

     @IBAction func segmentedControl(sender: AnyObject) { configureButton(for: segmentedControl.selectedSegmentIndex) }
  • 然后创建 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") } }

如果没有为myButton定义IBAction ,则需要从viewDidLoad function 调用configureButton function。

首先使用.selectedSegemtIndex属性获取所选段的索引。然后,检查条件说明,如果索引等于您的目标段索引,定义语句以更改标题、背景颜色,甚至是按钮的操作必需的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM