【问题标题】:Swift - Cannot rename button before it is clickedSwift - 在单击之前无法重命名按钮
【发布时间】:2018-04-25 06:49:31
【问题描述】:

我在重命名按钮时遇到问题。我有一些表格视图的屏幕,其中有项目,例如- “第一个”,“第二个”,“第三个”,......我想当我点击某个项目让我进入另一个屏幕时,按钮在哪里,它将根据我选择的项目更改文本。我的问题是,当我点击第一个项目时,例如“first”,按钮的名称是“Button”而不是“first_detail”,就像我在下面的代码中一样。我添加了这样的按钮:

@IBAction func testBtn(_ sender: AnyObject) {
    if names[myIndex] == "first" {
        sender.setTitle("first_detail", for: UIControlState.normal)
        self.performSegue(withIdentifier: "testSegue", sender: self)
}
    if names[myIndex] == "second" {
        sender.setTitle("second_detail", for: UIControlState.normal)
        self.performSegue(withIdentifier: "secondSegue", sender: self)
}
}

感谢您的建议。

【问题讨论】:

  • 如果你的目标是使用performSegue将数据从vc传递给另一个,那么我们应该问:你实现了prepare(for:sender:)方法吗?

标签: ios swift xcode uibutton rename


【解决方案1】:

你可以这样设置按钮标题

var buttonTitle: String {
        didSet {
            self.yourbutton.setTitle(buttonTitle, for: .normal)
        }
    }

你可以使用

self.buttonTitle = "First"

【讨论】:

    【解决方案2】:

    在 VC 中为按钮创建一个 outlet 并为按钮设置标题而不是 sender

    @IBOutlet weak var itemButton: UIButton!
    

    并检查它是否进入任何 if 条件

    @IBAction func testBtn(_ sender: AnyObject) {
        if names[myIndex] == "first" {
            itemButton.setTitle("first_detail", for: UIControlState.normal)
            self.performSegue(withIdentifier: "testSegue", sender: self)
        }
        if names[myIndex] == "second" {
            itemButton.setTitle("second_detail", for: UIControlState.normal)
            self.performSegue(withIdentifier: "secondSegue", sender: self)
        }
    }
    

    【讨论】:

    • 当我定义 @IBOutlet weak var itemButton: UIButton! 并在每个 if 条件下在代码中使用时,名称仍然没有显示。 Button 的名称仍为“Button”。谢谢
    • 你在故事板中设置按钮标题吗??
    • 您应该为按钮设置任何默认标题,否则您将默认文本为按钮
    • 名字字典是什么??
    【解决方案3】:

    您的代码不起作用的原因是在单击按钮之后触发了 IBAction。代码无法在单击按钮之前神奇地读取用户的想法并更改文本。

    在目标视图控制器上创建一个名为“buttonTitle”或类似的属性。如果可以的话,我宁愿避免使用可选项,因此您在目标 VC 上需要这样的东西:

    var buttonTitle = ""

    如果您使用的是 segue,那么您需要覆盖 prepare(for:sender:) 并在 segue 的 destination 视图控制器上设置 buttonTitle 属性(这将要求您将 destination 转换为目标视图控制器的子类)。像这个例子(我只是调用你的目标 VC 的类 MyDestinationVC。用正确的名称替换它):

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destinationVC = segue.destination as? MyDestinationVC {
            destinationVC.buttonTitle = names[myIndex]
        }
    }
    

    或者,如果您是通过代码手动推送/呈现,那么在将其放在屏幕上之前,请设置相同的 buttonTitle 属性。

    然后,在目标视图控制器的 viewDidLoad() 方法中,根据 buttonTitle 属性设置按钮的标题。

    override func viewDidLoad() {
        super.viewDidLoad()
        myButton.setTitle(buttonTitle, for: .normal)
    }
    

    注意:如果在 viewDidLoad() 之前调用其他自动为您更新按钮的解决方案,则会导致崩溃,因为它们正在访问的视图尚未初始化。

    【讨论】:

    • 感谢您的解释。我跟着你的脚步前进。在目标 VC 中,我输入了 var buttonTitle = "",然后我将其放入第二个 VC override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if let destinationVC = segue.destination as? recTableViewController { destinationVC.buttonTitle = names[myIndex] } },最后将其放入目标 VC override func viewDidLoad() { super.viewDidLoad() testBtn.setTitle(buttonTitle, for: .normal) .... ,但现在我收到错误消息 Use of unresolved identifier 'testBtn' in目标VC
    • 不,您必须在您来自的视图控制器中覆盖prepare for segue。你把 buttonTitle 变量放在你要的那个
    【解决方案4】:

    我有这样的方式:

    1. 我定义@IBOutlet weak var itemButton: UIButton!
    2. 我只放了动作按钮

      @IBAction func testBtn(_ sender: AnyObject) { 如果名称[myIndex] == "first" { self.performSegue(withIdentifier:“testSegue”,发件人:self) } 如果名称[myIndex] == "秒" { self.performSegue(withIdentifier:“secondSegue”,发件人:self) } }

    3. 我对第二个(目标)VC 提出了这些条件

      如果名称[myIndex] == "first" { itemButton.setTitle("first_detail", 对于:UIControlState.normal) } 如果名称[myIndex] == "秒" { itemButton.setTitle("second_detail", 对于:UIControlState.normal) }

    4. 它按我的需要工作

    【讨论】:

      猜你喜欢
      • 2018-08-11
      • 1970-01-01
      • 1970-01-01
      • 2018-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多