Is it possible to get a UIButton to change a segue identifier for another button? - cocoa-touch

I have a button (Say button "A") that links to another view controller via a modal segue and that segue has an identifier to pass information on using the method:
prepareForSegue
Now I want to add another button (Say button "B") to my first view controller that when pressed will simply change the segue identifier for my previous button (button "A"). So that I can still use prepareForSegue but have different data passed on.
Is this possible??

Have a variable declared in your ViewController as segueId of type String assign the default segue id to it.
When "buttonA" is pressed, issue performSegueWithIdentifier.
Now ad your another button "buttonB" to your view controller and in its action, change the value of the segueId variable.
By this way you can invoke multiple segues from same button.
Also, if you wanted to send different data to different VC, then check for the segueID in your prepareForSegue method and send accordingly
note: All the segues should go from VC and not from button.
Hope it helps.

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.

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

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

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.

Create a storyboard segue that isn't attached to any UI element?

I'm trying to trigger a storyboard segue from a UI element which isn't in my storyboard (in this case, it's a UIAlertView). As such, I can't draw the arrow from any of the existing components in my storyboard.
What's the proper way to deal with this situation? I need a segue with an identifier, so that I can call it from the relevant UIAlertView button.
You can add your segue by right click + drag from your source controller to your destination controller in your Storyboard. Then just click on the created segue and set the identifier.
You can trigger your segue programmatically like this:
// Note: In this example self is the source controller
// (the controller that holds the segue)
[self performSegueWithIdentifier:#"mySegue" sender:self];
Here is a screenshot you can use as a reference:

Objective C: How to reload tableview immediately after switching tabs?

I have 2 tabbars in my tabbar controller. I am currently in the second tabbar, after I click on a 'done' button, the tabbar controller needs to switch to the first tab and auto refresh the tableview in it.
I am able to execute on the first part
//Switch to the first tab's view
self.tabBarController.selectedViewController
= [self.tabBarController.viewControllers objectAtIndex:0];
However I need some advise on how to refresh the first view's tableview immediately after the switch is made. Any advise is appreciated.
EDIT: Updated requirement
Note: I do not want the view in the tabbar to reload everytime someone clicks on the tab. The reload should only happen in tab 1's tableview when
1) User is in tab 2
2) User clicks on 'Done button' in tab 2
3) App switches to tab 1 + Reloads the tableview in tab 1
Rationale is that the user will update some information in tab 2 which can only be viewed in tab 1. I want the user to be able to view the updated info immediately after switching to tab 1.
You could just do [tableView reloadData] in the first view's viewDidAppear: method?
EDIT: Editing to add additional data.
General caveat here, Apple tends to frown upon having custom UI Patterns like this. I'm not sure what your app is trying to do here, but you should mostly stick with switching tabs only if the user explicitly clicks on that tab. Have you looked at modal view controllers if you want to present a screen and get some data back, instead of having it in a tab bar?
If not, I guess you could have a solution where the first tab is a delegate of the second tab. When the button is clicked, you could call [delegate doneBtnClicked:] so the control shifts to the first tab. In that function, you could use the setSelectedIndex: to your current tab and call reloadData: on the table view.
You can send NSNotification after
pressing done button on second tab
and handle this notification on first tab controller.
So, create a DONE Button as a UIBarButtonItem, and in it's IBAction, do the following.
For reloading the tableView for the view in the first tab, put that tableView as that class' property , and reference the class like #class class1 , and create an object as a property class1 *object1 for View1 (Tab1) in the class for View2 (Tab2), and access the array1 using object1 like
[class1.object1.array1 reloadData];
And for bringing the view to tab one, use
self.tabBarController.selectedIndex = 0;
Thats pretty much it !
Alternatively you can also use a singleton class for this purpose.