How to make a push segue when a UITableViewCell is selected
control drag From View Controller to View Controller
You will need to give an identifier to your segue:
Perform the segue:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"yourSegue" sender:self];
}
Now here is the thing, this will just perform the segue, if you ever needed to pass some data to that view controller. Then you have to implement the following segue delegate:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:@"yourSegue"])
{
//if you need to pass data to the next controller do it here
}
}
How to make a push segue when a specific UITableViewCell is selected
see what I did here? You can use the indexPath
parameter to get the section & row the user touched, and then you can set up your programmatic segue.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
if ((indexPath.section == 0) && (indexPath.row == 5)) {
performSegue(withIdentifier: "GoToSecondViewController", sender: nil)
} else {
let productLine = productLines[indexPath.section]
let product = productLine.products[indexPath.row]
selectedProduct = product
performSegue(withIdentifier: "ShowProductDetail", sender: nil)
}
}
Push segue from UITableViewCell to ViewController in Swift
The problem is that you're not handling your data correctly.
If you look into your currentResponse
Array, you'll see that it holds NSDictionaries but in your prepareForSegue
you try to cast a NSDictionary to a NSArray, which will make the app crash.
Change the data
variable in RestaurantViewController
to a NSDictionary and change your prepareForSegue
to pass a a NSDictionary
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if let cell = sender as? UITableViewCell {
let i = redditListTableView.indexPathForCell(cell)!.row
if segue.identifier == "toRestaurant" {
let vc = segue.destinationViewController as RestaurantViewController
vc.data = currentResponse[i] as NSDictionary
}
}
}
For Swift 5
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if let cell = sender as? UITableViewCell {
let i = self.tableView.indexPath(for: cell)!.row
if segue.identifier == "toRestaurant" {
let vc = segue.destination as! RestaurantViewController
vc.data = currentResponse[i] as NSDictionary
}
}
}
How to push segue to specific View Controller depending on a selected cell?
I think we can keep it simple as posible
class ViewControllerOne: UIViewController, UITableViewDelegate {
var isSubVC = false
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: false)
if self.isSubVC {
// Push to ViewControllerTwo
} else {
// MainVC
if indexPath.row == 2 || indexPath.row == 6 || indexPath.row == 7 {
let subVC = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerOne") as! ViewControllerOne
subVC.isSubVC = true
self.navigationController?.pushViewController(subVC, animated: true)
} else {
// Push to ViewControllerTwo
}
}
}
}
Here is the code using your manager ideas for navigation without any tests.
enum ViewControllerType {
case main, subGroup1, subGroup2, subGroup3
}
class Manager {
public var currentState = ViewControllerType.main
public func initializeGroupState(row: Int) -> UIViewController {
if self.currentState == .main {
// MainVC, should push to SubVC if match condition
switch row {
case 2:
return self.makeSubViewController(state: .subGroup1)
case 6:
return self.makeSubViewController(state: .subGroup2)
case 7:
return self.makeSubViewController(state: .subGroup3)
default:
return self.makeViewControllerTwo()
}
} else {
// Current is SubVC, dont care kind of row, should push to ViewControllerTwo
return self.makeViewControllerTwo()
}
}
private func makeViewControllerTwo() -> UIViewController {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ViewControllerTwo")
return vc
}
private func makeSubViewController(state: ViewControllerType) -> UIViewController {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ViewControllerOne") as! ViewControllerOne
vc.manager.currentState = state
return vc
}
}
Perform segue depending on the UITableViewCell row selected
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//handle your code here
}
what mistake you have done is:you have handled in didDeselectRowAtIndexPath
for multiple segues: refer the image
How can I segue based on which cell was tapped in UITableViewCell?
You can implement the UITableViewDelegate
protocol to actually detect the cell being tapped:
extension MyOriginViewController {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// I'm assuming you have only 1 section
switch indexPath.row {
case 0:
// Instantiate the VC
let vc = storyboard?.instantiateViewController(withIdentifier: "MyViewControllerIdentifier")
// Present the view controller, I'm using a UINavigationController for that
navigationController?.push(vc, animated: true)
case 1:
// Another VC and push or present modally
let vc = ...
self.present(vc, animated: true, completion: nil)
default:
// Default case (unconsidered index)
print("Do something else")
}
}
}
So if you have this:
You can do:
let vc = storyboard?.instantiateViewController(withIdentifier: "MyNavController")
self.navigationController?.present(vc, animated: true, completion: nil)
Push a segue at selected row swift 3
You haven't set tableview.delegate
to self
.
tableView(_:didSelectRowAt)
is the Table View Delegate
method. Somewhere set this in your viewDidLoad
.
override func viewDidLoad() {
...
tableview.delegate = self
...
}
Performing a segue from a button within a custom UITableViewCell
Create a Protocol with the Method, that will be called by the CustomCell's Delegate, defined on your TableViewController
//Pass any objects, params you need to use on the
//segue call to send to the next controller.
protocol MyCustomCellDelegator {
func callSegueFromCell(myData dataobject: AnyObject)
}
Now use the Protocol on your UITableViewController
class MyTableViewController : UITableViewController, MyCustomCellDelegator {
//The usual Defined methods by UIViewController and UITableViewController
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//Set the CustomCell new Delegate
var cell = tableView.dequeueReusableCellWithIdentifier(customIdentifier) as MyCustomCell
cell.delagete = self
return cell
}
//MARK: - MyCustomCellDelegator Methods
func callSegueFromCell(myData dataobject: AnyObject) {
//try not to send self, just to avoid retain cycles(depends on how you handle the code on the next controller)
self.performSegueWithIdentifier("showComments", sender:dataobject )
}
}
Define the Delegate on your Custom Cell and the Call inside your New Button the Delegate Function.
class MyCustomCell : UITableViewCell {
var delegate:MyCustomCellDelegator!
@IBOutlet weak var myButton:UIButton
@IBAction func buttonPressed(sender:AnyObject){
var mydata = "Anydata you want to send to the next controller"
if(self.delegate != nil){ //Just to be safe.
self.delegate.callSegueFromCell(mydata)
}
}
}
Hopefully this can be clean and clear for you to understand and implement in your code.
Related Topics
Detecting Sheet Was Dismissed on iOS 13
How to Compare Two Uiimage Objects
How to Crop a Uiimageview to a New Uiimage in 'Aspect Fill' Mode
Swift - Stored Values Order Is Completely Changed in Dictionary
How to Use Uivisualeffectview to Blur Image
How to Share an Image on Instagram in Ios
Move a View Up Only When the Keyboard Covers an Input Field
Check For Internet Connection Availability in Swift
How to Scale Down a Uiimage and Make It Crispy/Sharp At the Same Time Instead of Blurry
Nsurlconnection and Basic Http Authentication in Ios
How to Create Launch Images For Iphone 6/6 Plus Landscape Only Apps
Ios9 Getting Error "An Ssl Error Has Occurred and a Secure Connection to the Server Cannot Be Made"
Nsurlsession With Nsblockoperation and Queues
Navigation Bar With Uiimage For Title