Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 正在获取显示视图控制器的名称_Ios_Swift - Fatal编程技术网

Ios 正在获取显示视图控制器的名称

Ios 正在获取显示视图控制器的名称,ios,swift,Ios,Swift,我有一个视图控制器和一个表视图单元格。它是一个独立的.xib文件,位于故事板之外。在视图控制器中,我注册该xib/nib,然后在cellforrowatinexpath中使用它,如下所示: if (indexPath.row == 2 || indexPath.row == 3 || indexPath.row == 4) { if let cell = tableView.dequeueReusableCell(withIdentifier: "myCustomCell") as? M

我有一个视图控制器和一个表视图单元格。它是一个独立的.xib文件,位于故事板之外。在视图控制器中,我注册该xib/nib,然后在
cellforrowatinexpath
中使用它,如下所示:

if (indexPath.row == 2 || indexPath.row == 3 || indexPath.row == 4) {
    if let cell = tableView.dequeueReusableCell(withIdentifier: "myCustomCell") as? MyCustomCell {
        return cell
    }
}

private func registerNibs() {
    tableView.register(UINib(nibName: "MyCustomCellView", bundle: nil), forCellReuseIdentifier: "myCustomCell")
}
此单元格有一个按钮,单击该按钮时,会将应用导航到其他视图控制器。为此,我希望在用户单击按钮时显示视图控制器的名称


我可以使用UIApplication.shared.topMostViewController(),但我试图找出是否有更好的方法通过单击按钮来获取演示视图控制器?

,就我理解您的问题而言。当用户点击单元格(
MyCustomCell
)中定义的按钮时,您希望显示一个新的视图控制器

如果是这样的话,有很多好的方法可以做到这一点,下面给出了其中一种

委托设计模式 委托是一种设计模式,它使类或结构能够将其部分职责移交(或委托)给另一种类型的实例

1.创建协议
MyCustomCellDelegate
协议MyCustomCellDelegate:类{
func didTap(自定义单元格:MyCustomCell)
}
//假设它是从UITableViewCell继承的类
类MyCustomCell:UITableViewCell{
//声明公共属性,以便在初始化单元格时从外部进行设置
弱变量委托:MyCustomCellDelegate?
//该函数将在用户点击按钮后立即调用
@iAction func didUserTapped(){
代理?.didTap(自定义单元格:self)
}
}
确认协议 您想要收听按钮触摸事件的位置。在您的情况下,它是一个Viewcontroller(其名称为
HomeViewController
),具有
UITableView
,应该确认为
MyCustomCellDelegate
协议

类HomeViewController:UIViewController、UITableViewDatasource、UITableViewDelegate、MyCustomCellDelegate{
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
if(indexPath.row==2 | | indexPath.row==3 | | indexPath.row==4){
如果let cell=tableView.dequeueReusableCell(标识符为:“myCustomCell”)作为?myCustomCell{
//设置委托,这样每当用户点击按钮时,就可以得到确认类的回调(对于您的情况,它是当前的viewcontroller)
cell.delegate=self
返回单元
}
}
}
//定义用户点击按钮后将调用的委托函数
func didTap(自定义单元格:MyCustomCell){
//在这里,您可以编写相应地显示/推送新视图控制器的逻辑
}
}


就我所能理解的你的问题而言。当用户点击单元格(
MyCustomCell
)中定义的按钮时,您希望显示一个新的视图控制器

如果是这样的话,有很多好的方法可以做到这一点,下面给出了其中一种

委托设计模式 委托是一种设计模式,它使类或结构能够将其部分职责移交(或委托)给另一种类型的实例

1.创建协议
MyCustomCellDelegate
协议MyCustomCellDelegate:类{
func didTap(自定义单元格:MyCustomCell)
}
//假设它是从UITableViewCell继承的类
类MyCustomCell:UITableViewCell{
//声明公共属性,以便在初始化单元格时从外部进行设置
弱变量委托:MyCustomCellDelegate?
//该函数将在用户点击按钮后立即调用
@iAction func didUserTapped(){
代理?.didTap(自定义单元格:self)
}
}
确认协议 您想要收听按钮触摸事件的位置。在您的情况下,它是一个Viewcontroller(其名称为
HomeViewController
),具有
UITableView
,应该确认为
MyCustomCellDelegate
协议

类HomeViewController:UIViewController、UITableViewDatasource、UITableViewDelegate、MyCustomCellDelegate{
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
if(indexPath.row==2 | | indexPath.row==3 | | indexPath.row==4){
如果let cell=tableView.dequeueReusableCell(标识符为:“myCustomCell”)作为?myCustomCell{
//设置委托,这样每当用户点击按钮时,就可以得到确认类的回调(对于您的情况,它是当前的viewcontroller)
cell.delegate=self
返回单元
}
}
}
//定义用户点击按钮后将调用的委托函数
func didTap(自定义单元格:MyCustomCell){
//在这里,您可以编写相应地显示/推送新视图控制器的逻辑
}
}

什么是“演示视图控制器的名称”?您想确定按下了哪个单元格上的按钮?显示视图控制器的名称是什么?您想确定按下了哪个单元格上的按钮?