How to assign Two segues on a table cell in SWIFT? - ios

I am totally new at IOS swift and working with android.
Here is my issue :
I'm developing a simple chatting App of IOS. I've already completed android ver and this is a kind of clone.
It has view controllers named VCList, VCMyFriends and VCChat. VCList is my very first view controller and has a table view. Touching a table view cell changes current view controller to VCMyFriends or VCChat.
When I touch a cell, app checks the member counts of the cell, so if it has other members then go to VCChat. If not, VCMyFriends on to invite my friends. Like following pic.
What I found is, I cannot assign two segues on a cell or split a segue with two ways. So I thought that can I change my VC without a segue? However I could not find any references or tutorial about it.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if( memberCnt == 0 ) {
self.presentViewController(VCMyFriends(), animated: true, completion: nil);
} else {
self.presentViewController(VCChat(), animated: true, completion: nil);
}
}
Above is my last try, and failed. It moves somewhere, but shows nothing at all. And if I can, I wish to use segues because have some datas to pass with segue.
Take care.

What I normally do is to create an instance of the controller via the instantiateViewControllerWithIdentifier and set the necessary member variables before calling presentViewController. You can also add a NavigationController if you want the capability the go back to the previous window.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
let mainStoryBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
if( memberCnt == 0 )
{
let vcMyFriends = mainStoryBoard.instantiateViewControllerWithIdentifier("VCMyFriend") as! VCMyFriends
vcMyFriends.membervar1 = membervar1
vcMyFriends.membervar2 = membervar2
self.presentViewController(vcMyFriends, animated: true, completion: nil)
}
else
{
let vcChat = mainStoryBoard.instantiateViewControllerWithIdentifier("VCChat") as! VCChat
vcChat.membervar1 = membervar1
vcChat.membervar2 = membervar2
self.presentViewController(vcChat, animated: true, completion: nil)
}
}
membervar and membervar2 are the data that you pass to the View Controllers. You need to set it before calling presentViewController so that they will be available when viewDidLoad is called.

Simply do this
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if( memberCnt == 0 ) {
performSegueWithIdentifier("VCMyFriends_segue", sender: self)
} else {
performSegueWithIdentifier("VCChat_segue", sender: self)
}
}
In your storyboard, select the first segue (the red line in your image) and choose Attributes inspector, then add VCMyFriends_segue as identifier.
Do the same thing for the second line (blue) and add VCChat_segue identifier.
Hope this helps

Related

How can I segue based on which cell was tapped in UITableViewCell?

I have a view controller with a table view embedded inside it, although it is always the same ten cells, I made it dynamic as you cannot make static table view cell inside a UIViewController. My question is, in prepare(for:) how can I segue to a view controller based on which cell was tapped? Each cell should navigatie to a completely different VC, but I think having ten segues is a mess.
Is there a better way?
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)

Tap on cell of UITableView and the other view doesn't appear

I have a tableView with some results of a search. I have connected the first and the second view with the "storyboard Segue" from the cell to the second view.
This is the code written in the first ViewController:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let indexPath = self.tableProva.indexPathForSelectedRow?.row
let vc = segue.destination as! DettaglioNewViewController
vc.titleLibro = posts[indexPath!].name
vc.authorLibro = posts[indexPath!].author
vc.ISBNLibro = posts[indexPath!].ISBN
}
In other project it works perfectly, but in this project it doesn't work, to show the second viewController I have to tap and swipe to the right or left.
It seems that the problem is graphic.
If I try to click on the cell does not become gray. turns gray if I hold down long on it
Has this happened to anyone else?
Can anyone help me?
You should use the 'didSelectRowAtIndexPath' method in order to segue to a different view for a specific cell. Use.....
var storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle(forClass: self.dynamicType))
vc = storyboard.instantiateViewControllerWithIdentifier("LoginVC") as DettaglioNewViewController
vc.loadView()
..... To segue needed information.
* Information Example *
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
myFavIndex = indexPath.row
let storyBoard = UIStoryboard(name: "Main", bundle:nil)
let view = storyBoard.instantiateViewControllerWithIdentifier("LoginVC") as! DettaglioNewViewController
view.imageURL = self.imageArray[mySelectedIndex]
view.titleString = self.titleArray[mySelectedIndex]
self.presentViewController(view, animated: true, completion: nil)
}
In the above example I'm using the new View Controllers image and title as view.imageURL and view.titleString to populate that views elements from my selected Cells Information via [mySelectedIndex]. You should be able to do this with an Int, UIImage, String, e.g.
You don't need to initialize your controller from the code. Just create a custom segue and call 'performSegue' method in 'didSelect' method of table delegate.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
self.performSegueWithIdentifier("yourIdentifierInStoryboardForCustomSegue", sender: nil)
}

pushViewController does no action

i have a tableview and i want to go to another vc when one of rows tapped. my didSelectRowAtIndexPath function is as below. print command works and shows the right clicked row. but when i use self.navigationController?.pushViewController it does not go to vc with playVideo storyBoardId. after changing it to presentViewController it works. what's wrong about my code that push doesn't work?
thanks
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("clicked " + String(indexPath.row) )
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("playVideo") as! PlayVideoViewController
vc.id = self.video[indexPath.row].id
// Present View as Modal
//presentViewController(vc as UIViewController, animated: true, completion: nil)
// Push View
//self.navigationController?.pushViewController(vc, animated: true)
}
Its because the view controller you are currently in may not have a navigation controller.
You cannot push a view controller without having a UINavigationController.
If your requirement is to push a view controller, then perform the following steps.
Embed a navigation controller for the current view controller.(Open storyboard -> Choose your view controller -> Choose "Editor" -> "Embed in" -> UINavigationController)
Now your code
self.navigationController?.pushViewController(vc, animated: true)
will be working fine.
Make sure that your ViewController is indeed built on a NavigationController. Try forcing the line:
self.navigationController!.pushViewController
if it crashes, you know there is not nav set up. Or you could just check in your storyboard but this is a quick way to tell.
I dont know why are you doing it like this. I think its unnecessarily complicated.
This should by your function didSelectRowAtIndexPath:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("playVideo", sender: tableView)
}
Then you can specify as much segues as you want in this function:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "playVideo") {
let indexPath:NSIndexPath = tableView.indexPathForSelectedRow!
let PlayVideoViewController = segue.destinationViewController as! PlayVideoViewController
//you can pass parameters like project id
detailVC.projectID = ids[indexPath.row]
}
}
Your segue has to be named like this in interface builder:

How to push segue from cell click in table view to another view controller [duplicate]

This question already has answers here:
How to make a push segue when a UITableViewCell is selected
(10 answers)
Closed 6 years ago.
I have one table view controller and by clicking the cell i am redirecting the user to detail view controller.But when i perform segue " present view controller" its working fine.But what i need is?
I need to perform push segue by clicking the table view cell.How to do that?
Here is my code:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: BusinessDetailViewController = storyboard.instantiateViewControllerWithIdentifier("BusinessDetailViewController") as! BusinessDetailViewController
vc.BusinessData = arrDict[indexPath.row]
self.presentViewController(vc, animated: true, completion: nil)
}
I have tried this below line :
self.navigationController?.pushViewController(vc, animated: true)
But it doesn't work.Any Solution Please !!
First of all your UITableViewController must have a UINavigationController
To do it rapidly embedded it to your table from the xCode menu
:
Then , you must create the segue, ctrl-drag from the UITableViewController to the destination viewController:
Then select the segue and give it an identifier in the property inspector:
Now you are able to perform this segue in code:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
performSegueWithIdentifier("mySegue", sender: cell)
}
If you are not using UINavigationController than add a segue in storyboard from table cell to destination viewController and give a identifier to segue.
then on cell click call
- performSegueWithIdentifier:sender:
and implement
- prepareForSegue:sender:
Without a navigation controller it's not possible to push a controller. Your self.navigationController property is nil (you can check it)
How to solve it?
A :You need to embed your view controller in navigation controller either via code or via storyboard
try this code, its working for me,
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("PlayViewController") as! PlayViewController
self.navigationController?.pushViewController(controller, animated: true)
}
and set the storyboard Id, example given below,
hope its helpful

Create and perform segue without storyboards

i got an app without storyboards, all UI creation is made in code and I got a splitView which I would make it usable on iPhone, because as the app as been first designed for iPad only, so that when you select a row in the list in the Master view it does nothing on iPhone but is working fine on iPad.
So my question is can I create and perform the segue that allows to show the Detail View on the didSelectRowAtIndexPath method ?
Here's what i've done so far :
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let segue = UIStoryboardSegue(identifier: "test", source: self, destination: detailViewController!)
performSegueWithIdentifier("test", sender: self)
}
but when running and selecting a row the app was crashing telling it needed a performhandler so i added this :
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let segue = UIStoryboardSegue(identifier: "test", source: self, destination: detailViewController!, performHandler: { () -> Void in
let object = self.fetchedResultsController.objectAtIndexPath(indexPath)
let controller = self.detailViewController!
controller.detailItem = object
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
controller.navigationItem.leftItemsSupplementBackButton = true
})
performSegueWithIdentifier("test", sender: self)
}
and now when selecting a row xcode says that there is no segue with such identifier "test".
I also tried to call it by segue.perform() and add the performHandler content into the prepareForSegueMethod :
if segue.identifier == "test" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let object = self.fetchedResultsController.objectAtIndexPath(indexPath)
let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
controller.detailItem = object
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
and it does just nothing, doesn't crash, just highlight the row i selected and that's all
Can you guys help me ?
EDIT : As Oleg Gordiichuk said, it's not possible to do what I want to do without Storyboards, so thanks for his help :)
Segue it is component of the storyboard interaction it is possible to understand from name of the class UIStoryboardSegue. It is bad idea to create segues programmatically. If i am not making mistake storyboard creates them for you.
For solving of you're issue try to use some common ways like simply present ViewController.
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("id") as! MyController
self.presentViewController(vc, animated: true, completion: nil)
As i understand from our conversation in comments. You would like to create navigation for tableview to details view using segues without storyboard. For now it is impossible to do this without storyboard.
For future learning try to investigate this information.
One way is to use the didSelectRow method for tableView
Swift 3.x
// MARK: - Navigation & Pass Data
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Selected Row \(indexPath.row)")
let nextVC = YourNextViewController()
nextVC.YourLabel.text = "Passed Text"
nextVC.YourLabel.text = YourArray[indexPath.row]
// Push to next view
navigationController?.pushViewController(nextVC, animated: true)
}

Resources