Creating a segue for a TableViewController cell's detail accessory button - ios7

I'm trying to implement segueing from a tableview controller when the user taps the cell's accessory button.
I've searched on here and have some new things to try but I just wanted to ask whether in the storyboard you can actually create the segues from the accessory button, or whether you just need to create them from the tableviewcontroller.
It'll be within the accessoryButtonTappedForRowWithIndexPath that I'll want to act on the tap, but I see some answers on here say that you have to just go from the tableviewcontroller when creating your segues where as another answer said that the sender within prepare for segue would be set to the cell that contains your accessory view.
Whenever I try to ctrl drag from the accessory button in my prototype cell it just gets rid of the already existing on-selected segue I'd setup for that cell.
Just wanted to know for sure, what the best practice was before I started making all my detail accessory segues just go from the root TableViewController and passing through accessoryButtonTappedForRowWithIndexPath's indexPath (or it's row) as the sender to my prepareForSegue.
Cheers

Ok, after trying the suggestions I described in my original question I got the code all working how I wanted. I added segues from the root tableview controller and then performed them programatically within the accessoryButtonTappedForRowWithIndexPath delegate function.
I have a few Table View Controllers doing similar things in my navigation controller so I set about implementing this accessory button workflow in each of them. When i reached the last TVC in my nav controller hierarchy I didn't want selecting cells to go any further, so i changed the accessory type to just detail.
As I wasn't concerned at this stage to have the cell interaction sourced segues overwriting each other I just dragged a segue out from the prototype cell to the destination view controller. Upon doing so within the add segue popup I saw 2 options - Selection Segue and Accessory Action. I'm sure that when I first tried doing this I only had the single Selection Segue choice.
Regardless of whether this was my tired eyes missing it, or Xcode having a funny turn (as it seems to from time to time, being laggy at updated states of various files etc) I can now say in answer to my original question, all you have to do is setup your accessory action segue as required. You can have both selection segue and accessory action segues setup from the same prototype cell, and everything just works as you'd expect.
I've just updated my setup to work this way and all seems fine.
Cheers

Related

Segue from Custom Cell (dynamic Prototype Cell)

Im using the Storyboard to create my UI.
I have a SplitViewController. The MasterViewController holds a TableViewController where I created a CustomCell with Custom Design.
The Cells are shown pretty good with my data.
The point is: The Custom Cell also holds an Info-Button, with should Popup a little 300x88 TableViewController with some data.
If the Cells in my MasterViewController->TableViewController where a Static one, I just would drag & drop a Segue from the Info-Button to my Popup-TableViewController.
But sadly I cant do this with a dynamic Prototype Cell... I just get the error:
Couldn't compile connections ... >> anchor >> UIButton...
So how can I implement this?
Kind Regards.
Define a manual segue dragging the whole master view onto the detail view, then add a manual target/action to your custom info button and perform the manual segue there. Of course you must set a segue identifier in the storyboard to later reference it in the code, IB will tell you if you forget to.
Instead of connecting and creating segue from individual cells, you can connect all those segues from the View Controller button, lying below your view in your storyboard. In this case, you will have multiple segues and none of them will be individually connected to cells. And when segues are ready, you can use this method for moving on to the next View Controller depending on which cell is tapped from the tableView.
[self performSegueWithIdentifier:#"yourSegue" sender:nil];
You just have to check which cell is tapped and then perform the appropriate Segue on tap using the above code and the respective segue identifier. The prepareForSegue method will be called immediately after this method is called.
Look out for the yellow button as shown below your View Screen in your storyboard.
Drag and drop a segue from that button onto the View Controller that you want to connect.
Hope this helps.

Distinguishing taps on UITableViewCell in general from taps on its accessory button in particular

I would like to implement a UITableViewController that works in a similar fashion to the Episodes (under the My Podcasts) view in the iOS 7 Podcasts app in that its cells should perform one segue if I tap on the (left part of the) cell and perform another segue if I tap on the cell's accessory view.
My UITableViewController implements both tableView:didSelectRowAtIndexPath: and tableView:accessoryButtonTappedForRowWithIndexPath: from UITableViewDelegate, however only the former method gets called, regardless of whether I tap on a cells accessory button or somewhere else inside the cell.
So how can the necessary distinction be made. I assume it would be possible to place a UITapGestureRecognizer on the cell's contentView and use its event handler instead of tableView:didSelectRowAtIndexPath:, but hope there is a simpler scheme as well.
I have meanwhile found what appears to be the right answer here:
iOS will make a distinction and call either tableView:didSelectRowAtIndexPath: or tableView:accessoryButtonTappedForRowWithIndexPath: if the accessory (as set in IB) is a detail disclosure button (or detail button), but not if it is a disclosure indicator. Indeed Apple's Podcasts app uses a detail button in the described context.
This is the relevant excerpt from (an older version of) the documentation:
Note that the detail disclosure button, unlike the disclosure
indicator, can perform an action that is separate from the selection
of the row.

3 programmatically created UIButtons - how to go between 3 UIViewControllers using those buttons with Storyboard?

Playing around with some Objective-C (well, iOS) and have done the following... loaded some UIButtons programmatically into a UIScrollView. That works well. Though I've always just connected UIViewControllers together using control-click and drag. Now I've created buttons programmatically, I have no idea how to go from one view controller to another in a Storyboard because there is nothing to drag from!
I'm not really sure what code to post as such, because I haven't done anything that /nearly/ works or doesn't work as such. I get how to do it with XIBs. But I suppose the question is : 3 UIButtons have been created programmatically and I have 3 UIViewControllers. How do I access those ViewControllers using my UIButtons?
Thanks
In the Interface builder view control click and drag from the viewcontroller icon under the first view controller, to the middle of the second view controller. A segue will be created, selected the appropriate type.
Now select the segue and in the inspector give it a unique identifier (say 'myNewSegue').
Now in your first viewcontroller you can create a method that has the following code:
-(void)myButtonAction:(id)sender {
[self performSegueWithIdentifier:#"myNewSegue" sender:self];
}
And add this method as a target action to your button:
[myButton addTarget:self
action:#selector(myButtonAction:)
forControlEvents:UIControlEventTouchUpInside];]
A segue doesn't have to have a button at the leading end of it; you can instead draw it from an entire view controller to another. You can also give a segue an identifier, a string that's used as a name for that segue. Once you've done that, you can programmatically trigger that segue by calling -performSegueWithIdentifier:sender:.
To actually call -performSegueWithIdentifier:sender:, though, you'll need to connect the button to a target and action. If you've never done that, read the Event Handling Guide for iOS in your documentation.

Determine if viewcontroller is already on stack and if so, go to or dismiss it

I have an app I am working on that has a main screen with two buttons. One will take you to a view of a GPS (map) and then once there (new VC) it has options for setting that position or bringing up a list (tableview, another VC) of all locations already tagged.
At the list VC, if you click on the table cell, it will bring up the VC with the map. Problem is, this then adds the same VC bak on the stack. If a user clicks the Cancel button, they go back ones screen, then cancel goes back another screen, etc... until back to the main.
I know I can do the [self.navigationController popToRootViewControllerAnimated:YES]; to pop back to root but that is not always what I want.
Also, I know I can do: [[[self presentingViewController] presentingViewController] dismissModalViewControllerAnimated:YES];
I guess what I am saying is I want to "reuse" the GPS map view so I can call it from other VC's, so that is why I didn't go with the "pass back" to calling VC. So, is there away to either when a button is pressed and is to present a new VC, can I dismiss the prior one after the new one is shown? This way, a dismiss of current VC would take me back to where I need to be.
I hope makes sense and also that this question doesn't fall into the "Not an actual question" category.
Any help or better suggestions is greatly appreciated. Thx
Geo...
If you want to jump back some number of levels in a navigation controller's VC stack, you'll probably want to use its popToViewController:animated: method. To figure out if a particular view controller is on that stack, look at the navigation controller's viewControllers property. Be careful, though, as this kind of jumping around is a rather nonstandard UI behavior (even though there's API for it) which might confuse your users.
Also, using navigation controllers and presenting modally aren't the only ways to manage multiple view controllers -- you can always set the window's rootViewController yourself (and animate the change with UIView animations), even wrapping up your custom transition type in a custom UIStoryboardSegue if you like.
You can put a delegate in the table view. So that when a cell is pressed the info is passed to the delegate method in the VC which will dismiss the table view and reloads itself with the new info. You will have to implement refresh method in that VC.

Increasing number of living Views

I've set up a really simple project using storyboards including two views as shown here: http://i.stack.imgur.com/iRx21.png. The navigation can be done by either selecting a cell in the custom table view or hitting the back button labelled with "<<". Everything works fine except the following:
when I switch between the views, every time an instantiation happens. The profiling shows an increasing number of view objects. I would like to keep only one of each view and instantiation should be happen only once. What am I doing wrong? (I'm using ARC.)
Thanks in advance!
You should not link your back button to the parent view controller. This is what causes the new instantiation.
The way to go is to embed the table view into UINavigationController (in IB, choose Editor -> Imbed In -> Navigation Controller. Then change your segue to a Push segue. You can of course hide the navigation bar etc. to make things look exactly as you like. Then, link the back button to the controller with an IBAction and in the handler do a simple
[self.navigationController popViewControllerAnimated:YES];
This would be the appropriate logic of what you are doing. Of course, you can also push the web view modally and then handle the button click with
[self dismissModalViewControllerAnimated:YES];