【问题标题】:How to access the title of the selected part of a segmented control from another class?如何从另一个类访问分段控件的选定部分的标题?
【发布时间】:2017-11-16 21:27:49
【问题描述】:

在我的第一个视图控制器中,我有分段控制。我正在尝试从我的第二个视图控制器获取分段控件的选定部分的标题的字符串值。任何人都知道如何做到这一点?提前致谢!

【问题讨论】:

标签: swift uisegmentedcontrol swift4


【解决方案1】:

您没有编写任何代码,但我认为这是一个呈现和关闭视图控制器方案,而不是addchildviewcontrolleraddsubview。在这种情况下,有很多方法可以做到,这里有两种:

1) 制作一个全局变量来检测分段控制的状态。

@IBOutlet weak var segContoll: UISegmentedControl!
func back() {
globalvar = segContoll.titleForSegment(at: segContoll.selectedSegmentIndex)
}

2) 制定协议,如下所示:

protocol NotifyDelegate {
    func collectionViewSelectedIndex(indexPath : Int)
}

class CollectionViewForSessions: ... {
    var delegate: NotifyDelegate?
func ... {
//The nextview delegate
    sessionCollection.delegate = self
}
    func ... {
//Instead of inputing a number, customize it to input a string segContoll.titleForSegment(at: segContoll.selectedSegmentIndex)
        delegate?.collectionViewSelectedIndex(indexPath: indexPath.row)
    }
}

class MessagesWithPerson: NotifyDelegate {
    func collectionViewSelectedIndex(indexPath: Int) {
    }
}

【讨论】:

  • 我很确定你应该通过简单地在任何类、结构或协议之外声明一个变量来声明一个全局变量,而不是使用“globalvar”。此外,当我在我的类之外声明全局变量时,它会给出错误消息“实例成员 'nameOfSegControl' 不能用于类型 'viewControllerSegControlIsIn'。
  • 我的原始信息旨在被视为蓝图。显然,你需要在你的类之外实现这个:var yourString: String! 并在你的后台函数中分配它,在那里你关闭视图:segContoll.titleForSegment(at: segContoll.selectedSegmentIndex)
猜你喜欢
  • 2021-10-12
  • 1970-01-01
  • 1970-01-01
  • 2020-09-17
  • 1970-01-01
  • 1970-01-01
  • 2018-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多