Can I use multiple segues with one UITableViewDelegate?
Of course ! Define all your segues on your storyboard, by ctrl-dragging from the tableViewController (not a row, the tableViewController itself) to the next view. Give them IDs, so you know which one to call. When all your segues are defined visually, go to the code. In your tableView's delegate, in didSelectRowAtIndexPath
, simply call the segue you want, by checking indexPath.row
:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row) {
case 0: [self performSegueWithIdentifier:@"Segue0" sender:self];
break;
case 1: [self performSegueWithIdentifier:@"Segue1" sender:self];
break;
[...]
default: break;
}
}
That way, the segue with the ID "Segue0" will be fired when the user selects the first row, and so on.
You can also add the line : [tableView deselectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1] animated:YES];
at the beginning of didSelectRowAtIndexPath
so the row does not remain selected after the user touched it !
Edit : This works for both static and dynamic cells ! Be careful to ctrl-drag your segue from the tableViewController, not a cell !
IOS StoryBoard multiple Segue's from a TableCell
Don't try to hook up the Segues to a tableviewcell in this case. Hook them up to the View Controller itself.
How to use Storyboards with multiple segues to one view controller?
Every ViewController should have their own seque. To create, press "Ctrl" and move to the ViewController you want
And every ViewController should implement "prepareForSegue"
- (void)prepareForSegue:(UIStoryboardPopoverSegue *)segue sender:(id)sender {
if ([segue.destinationViewController isKindOfClass:[MyDestinationVC class]]) {
//do something
//Example
MyDestinationVC *screen = (MyDestinationVC *)segue.destinationViewController;
screen.someValue = self.sendedValue;
}
}
Segue to different controllers from one view controller
If didSelectRowAtIndexPath
not working, it can be caused by either your UITableViewDelegate
has not been set, or, it might possible that Selection of your UITableView has been turned off, need to Turn on, see image to find how.
Cheers.
is it possible to segue from a UITableViewCell on a UIView to another view
My Solution
I ended up using a delegation pattern
I made a segue dragging from the my UIViewController - specifically dragging from the viewController icon (the orange circle with a white square in it - from the name bar thats under the view in the storyboard - although you could also drag from the sidebar ) to the view that i wanted to segue to.
I needed to trigger this segue from a table view cell on a table view.
TableView Bit
So i declared a protocol in my tableview header file - which is called LocationTV.h - as follows
@protocol LocationTVSegueProtocol <NSObject>
-(void) makeItSegue:(id)sender;
@end
Below that I declare a property to hold my delegate
@property (nonatomic, strong) id<LocationTVSegueProtocol> makeSegueDelegate;
To actually trigger the segue i called the makeItSegueMethod on my makeSequeDelegate in my didSelectRowAtIndexPath method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section) {
DLog(@"selected row %d",indexPath.row);
case dLocation:
{
if(indexPath.row == 2){
[_makeSegueDelegate makeItSegue:self];
} else if (indexPath.row == 7){
UIViewController Bit
and set up my UIViewController (named MultiTableHoldingVC) as implementing that protocol
@interface MultiTableHoldingView : UIViewController
<EnviroTVProtocol,LocationTVSegueProtocol> {
}
Below that i declared the protocol method in the list of my classes methods (although i'm not sure that is necessary as the compiler should know about the method as the decalration of implementing a protocol is essentially a promise to implement this method)
-(void) makeItSegue:(id)sender;
And then over in the implementation file of my UIViewController i wrote the method which essentially just calls preformSegueWithIdentifier
-(void) makeItSegue:(id)sender{
[self performSegueWithIdentifier:@"ChooseCountryNow"
sender:sender];
}
And to link it all together,as in the header file I had declared my instance of the tableView as follows
@property (strong, nonatomic) IBOutlet LocationTV *dsLocationTV;
I had to set that tables views delegate property to be self - which I did in my UIViewControllers -(void)ViewDidLoad method
_dsLocationTV.makeSegueDelegate = self;
It all seems a bit of a kludge calling a method to call a method and allprog suggestion is simpler (I cant for the life of me work out why it threw up errors for me) but this works just fine . Thanks to both allprog and danypata for their suggestions.
Hope this is helpful to someone out there
How can i use uitableview cell for performing segue
Implement the UITableViewDelegate protocol method tableView(_:didSelectRowAt:)
as below
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch sections[indexPath.row] {
case "Main Menu":
performSegue(with: "toMainMenu", sender: nil)
case "Settings":
performSegue(with: "toSettings", sender: nil)
case "Profile":
performSegue(with: "toProfile", sender: nil)
}
}
If you need to configure or pass some data to the destination controller, you can override the controller's prepare(for:sender:)
method to get a reference, for example:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toMainMenu" {
let dvc = segue.destination as! MenuViewController
// now you have a reference
}
}
TableView with Segues to Multiple Views
The logic that you need to implement is:
- Check the the segmentedIndex to know whether the user wants to see Upcoming or Showing.
- Load the tableView with movies per step 1.
- When the user taps on cell, use the segmentedIndex to identify whether you should a movie from the Upcoming or Showing array was chosen.
Get the ID of that movie and send it to the respective DetailsViewController. Now that you have the movieID, use it to request the movie details from your database.
func tableView(_ tableView:UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell") as? TableViewCell else { return UITableViewCell() }
switch segmentedControl.selectedSegmentIndex {
case 0:
//handle the set up of Upcoming cells
case 1:
//handle the setup of Showing cells
default:
break
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch segmentedControl.selectedSegmentIndex {
case 0:
let DetailViewUpcoming = self.storyboard?.instantiateViewController(withIdentifier: "DetailViewUpcoming") as! DetailViewUpcoming
DetailViewUpcoming.init(forMovie: upcomingArray[indexPath.row].movieID)
self.navigationController?.pushViewController(DetailViewUpcoming, animated: true)
case 1:
let DetailViewShowing = self.storyboard?.instantiateViewController(withIdentifier: "DetailViewShowing") as! DetailViewShowing
DetailViewShowing.init(forMovie: showingArray[indexPath.row].movieID)
self.navigationController?.pushViewController(DetailViewShowing, animated: true)
default:
break
}
}
@IBAction func segmentedTapped(_ sender: Any) {
tableView.reloadData()
}
Inside DetailViewUpcoming
and DetailViewShowing
declare a variable for the movieID and add an initialiser
var movieID: String?
init(movieID:String){
self.movieID = movieID
}
Now when you load the view use the above movieID
to request the movie details and assign them to properties as you see fit.
SWIFT: Connect UITableViewCell to two different detail view controllers
One way to do this could be:
In the tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
function of your UITableViewDelegate
check if the selected cell corresponds to an object Widget
or an object Sprocket
Then, present the corresponding UIViewController
with the necessary configuration with this code:
let vc = UIStoryboard(name: "The name of your storyboard here", bundle: nil).instantiateViewController(withIdentifier: "Your VC identifier") as! YourViewController
// pass your data and configure the viewcontroller here
navigationController?.pushViewController(vc, animated: true)
In storyboard, assign the identifier that you think is convenient to your ViewController
:
Select the ViewController -> 3rd item -> Identity -> StoryboardId and check "Use Storyboard ID"
Note: Delete the segues what are you currently using
Segue to UITableView depending on Row Selection
You can segue to a single UITableViewController
here and make it display respective contents according to the category chosen. This might help you.
In your Destination View Controller, you can declare a variable and use it in your prepareForSegue
method as shown to identify what type of content do you want to see on your destination view controller.
destController.varName = @"setYourTypeHere";
Now you can also set the variable value here based on the Row you are clicking in your tableview.
And in your Destination View controller, you can check this value using switch
or if...else
cases and bind the appropriate data as per your requirement.
Related Topics
Add Uitapgesturerecognizer to Uitextview Without Blocking Textview Touches
How to Find the Kind of Errors a Method May Throw and Catch Them in Swift
Itms-90809: Deprecated API Usage - Existing App That Use Uiwebview Are No Longer Accepted
How to Determine If a User Has an iOS App Installed
Clip Image to Square in Swiftui
Thread 1:Exc_Bad_Access (Code = 1, Address = 0X30000008)
Xcode Doesn't Write Anything to Output Console
Xcode iOS App Development Code Signing
Google Maps Url Scheme Not Working on iOS 9
iPhone Camera, How to Avoid Cameraoverlay on Preivew View; How to Know When Entering Preview View
Firestore Paginating Data + Snapshot Listener
Libz.Dylib Versus Libz.1.2.3.Dylib Versus Libz.1.2.5.Dylib
Removing Wkwebview Accesory Bar in Swift
Could an iOS Application Read/Access Sms Text
Build Video from Uiimage Using Swift
Why Isn't Preferredcontentsize Used by iPhone 6 Plus Landscape