【问题标题】:Changing label text of second ViewController upon clicking button on first ViewController without Segue在没有 Segue 的情况下单击第一个 ViewController 上的按钮时更改第二个 ViewController 的标签文本
【发布时间】:2018-06-28 04:36:42
【问题描述】:

我有一系列 10 个问题,人们通过按下按钮(3 个选项)来回答这些问题。我想在第二个 VC 滚动测试时更改它们的标签。

到目前为止,我已经在我的第一个 VC 中设置了我的按钮,这是我不知道要编写什么代码来更改第二个 VC 中的标签的地方:

@IBAction func levreun(_ sender: Any) {
    levrescore = 1
    print(levrescore, "sèches, gercées ou rouges")
} 

(打印下是我要发送到第二个VC上的标签)

这就是我将标签准备为第二个 VC 中的字符串的方式:

@IBOutlet var LevreResultText: String!   

override func viewDidLoad() {
    super.viewDidLoad()
    LevreResultText.text = LevreResultText
}

编辑:请注意,在测试结束时该人按 COMPLETE 之前,我不想执行 segue。因此,标签必须在回答 10 个问题时更改!

【问题讨论】:

  • 第二个 VC 孩子在第一个吗?
  • 不,它们是 2 个独立的视图控制器。
  • 你试过 didSet 和 willSet 吗?

标签: ios iphone swift uitableview uiviewcontroller


【解决方案1】:

您可以在 Second VC 中制作静态字符串变量。 当您在第一个 VC 中点击按钮时,您可以更改静态变量的变量。

并且在Second VC的ViewDidLoad函数中,您可以将标签文本更改为静态变量。

第一个 VC

@IBAction func levreun(_ sender: Any) {

    levrescore = 1
    print(levrescore, "sèches, gercées ou rouges")

    SecondVC.staticLabelText = "sèches, gercées ou rouges"
} 

第二个VC

static var staticLabelText = ""
@IBOutlet var LevreResultText: String!   

override func viewDidLoad() {
    super.viewDidLoad()

    LevreResult.text = 
    SecondVC.staticLabelText
}

【讨论】:

  • 我得到错误:类型'SecondVC'在第一个VC中没有成员'staticLabelText'
  • 你的 SecondVC 类名是什么?你必须使用 "SecondVC 类名".staticLabelText。
  • 这里,SecondVC 是类名,不是变量。
  • oups 我的班级名称打错了谢谢!我会尝试构建的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多