以编程方式在UITableViewCell中显示带有按钮的视图控制器(Swift)

时间:2022-05-14 15:00:37

I am trying to make it where when a user clicks on a table view cell in my table view it takes them to a new view controller. More specifically, when a user clicks on a persons username it should take them to that users profile. the username being the table view cell and the profile being the new view controller. I thought the way to do this was to use the ".presentViewController(vc, animated: true, completion: nil) however when i do this it says "myCell does not have a member named .presentViewController"以编程方式在UITableViewCell中显示带有按钮的视图控制器(Swift)

我试图在用户点击表格视图中的表格视图单元格时将它带到新的视图控制器。更具体地说,当用户点击人员用户名时,应该将他们带到该用户档案。用户名是表视图单元格,配置文件是新视图控制器。我认为这样做的方法是使用“.presentViewController(vc,animated:true,completion:nil)”然而,当我这样做时,它说“myCell没有名为.presentViewController的成员”

If anyone could help me solve this problem it'd be greatly appreciated

如果有人能帮我解决这个问题,我将不胜感激

4 个解决方案

#1


13  

The presentViewController:animated:completion is an instance method of the UIViewController not UIView or a subclass of. You can try this:

presentViewController:animated:completion是UIViewController的实例方法,而不是UIView或其子类。你可以试试这个:

self.window?.rootViewController.presentViewController(specificViewController, animated: true, completion: nil)

However, I suggest that you should use the presentViewController:animated:completion: method from UIViewController. A callback mechanism can be achieved between the UIViewController and the cell.

但是,我建议您使用UIViewController中的presentViewController:animated:completion:方法。可以在UIViewController和单元之间实现回调机制。

Like so:Get button click inside UI table view cell

像这样:在UI表格视图单元格内单击获取按钮

#2


3  

Banning's answer works, however the syntax is old. From Swift 4:

Banning的答案有效,但语法很老。来自Swift 4:

self.window?.rootViewController?.present(vc, animated: true, completion: nil)

#3


1  

swift3 version

swift3版本

let storyboard = UIStoryboard(name: "MainViewController", bundle: nil)
let messagesViewController = storyboard.instantiateViewController(withIdentifier: "MessagesViewController") as! MessagesViewController
self.window?.rootViewController?.present(messagesViewController, animated: true, completion: nil)

#4


0  

If you have created a separate class for tableView which is a good practice then, you can do it like this:

如果你为tableView创建了一个单独的类,那么这是一个很好的做法,你可以这样做:

First, create addTarget where the cell is created (cellForRowAt):

首先,创建创建单元格的addTarget(cellForRowAt):

cell.dropdownBtn.addTarget(self, action: #selector(self.openListPickerVC(_:)), for: .touchUpInside)

After that, present view controller in openListPickerVC():

之后,openListPickerVC()中的当前视图控制器:

@objc func openListPickerVC(_ sender: UIButton) {
    let index = sender.tag
    let vc: PickerViewController = UIStoryboard(name: "Main", bundle: 
             Bundle.main).instantiateViewController(withIdentifier: 
             "PickerViewController") as! PickerViewController

     self.present(vc, animated: true, completion: nil)
}

#1


13  

The presentViewController:animated:completion is an instance method of the UIViewController not UIView or a subclass of. You can try this:

presentViewController:animated:completion是UIViewController的实例方法,而不是UIView或其子类。你可以试试这个:

self.window?.rootViewController.presentViewController(specificViewController, animated: true, completion: nil)

However, I suggest that you should use the presentViewController:animated:completion: method from UIViewController. A callback mechanism can be achieved between the UIViewController and the cell.

但是,我建议您使用UIViewController中的presentViewController:animated:completion:方法。可以在UIViewController和单元之间实现回调机制。

Like so:Get button click inside UI table view cell

像这样:在UI表格视图单元格内单击获取按钮

#2


3  

Banning's answer works, however the syntax is old. From Swift 4:

Banning的答案有效,但语法很老。来自Swift 4:

self.window?.rootViewController?.present(vc, animated: true, completion: nil)

#3


1  

swift3 version

swift3版本

let storyboard = UIStoryboard(name: "MainViewController", bundle: nil)
let messagesViewController = storyboard.instantiateViewController(withIdentifier: "MessagesViewController") as! MessagesViewController
self.window?.rootViewController?.present(messagesViewController, animated: true, completion: nil)

#4


0  

If you have created a separate class for tableView which is a good practice then, you can do it like this:

如果你为tableView创建了一个单独的类,那么这是一个很好的做法,你可以这样做:

First, create addTarget where the cell is created (cellForRowAt):

首先,创建创建单元格的addTarget(cellForRowAt):

cell.dropdownBtn.addTarget(self, action: #selector(self.openListPickerVC(_:)), for: .touchUpInside)

After that, present view controller in openListPickerVC():

之后,openListPickerVC()中的当前视图控制器:

@objc func openListPickerVC(_ sender: UIButton) {
    let index = sender.tag
    let vc: PickerViewController = UIStoryboard(name: "Main", bundle: 
             Bundle.main).instantiateViewController(withIdentifier: 
             "PickerViewController") as! PickerViewController

     self.present(vc, animated: true, completion: nil)
}