This is probably a basic question - but I cant seem to find the answer to it in my searches!
I have a uiview setup which contains two input areas which are linked to a child modal view via separate segues.
this is my parent prepare for segue method -
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"goalInfo"])
{
[segue.destinationViewController setYourGoalViewController:self];
}
if ([segue.identifier isEqualToString:#"longGoalInfo"])
{
[segue.destinationViewController setYourGoalViewController:self];
}
}
basically i'd like to determine which segue had been used in the child view so I can apply the relevant filed updates and alter a title/description in the child view. - I basically need something very similar to
if ([segue.identifier isEqualToString:#"goalInfo"])
but i'm not sure how to access this from teh child? Any tips?
The segue identifier is check on the source ViewController during its prepareForSegue: method. What you rather need is during prepare for segue is to set properties on the destination viewController and then when it loads, read these properties and set UI outlets.
Look at this example: I am stuck at presenting a full image on tapping the cell in UICollectionViewController. It's about CollectionVC and its detailVC, but the principle is the same.
Related
I'm new on Stackoverflow and I'm currently learning XCode from scratch and I'm in a process of making a Single Page Application with options.
Anyone knows how to efficiently make a simple menu with multiple selectable UIButtons that make the main ViewController display different datasets depending on the selection in XCode?
Tried different things (creating SecondViewController for example but can't figure out how to pass data from it to main ViewController).
Any help will be greatly appreciated.
Refer this:
https://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/
How to pass prepareForSegue: an object
Simple Test from above Answer Reference:
Simply grab a reference to the target view controller in prepareForSegue: method and pass any objects you need to there. Here's an example...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setMyObjectHere:object];
}
}
REVISION: You can also use performSegueWithIdentifier:sender: method to activate the transition to a new view based on a selection or button press.
For instance, consider I had two view controllers. The first contains three buttons and the second needs to know which of those buttons has been pressed before the transition. You could wire the buttons up to an IBAction in your code which uses performSegueWithIdentifier: method, like this...
//When any of my buttons are pressed, push the next view
- (IBAction)buttonPressed:(id)sender
{
[self performSegueWithIdentifier:#"MySegue" sender:sender];
}
// This will get called too before the view appears
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"MySegue"]) {
// Get destination view
SecondView *vc = [segue destinationViewController];
// Get button tag number (or do whatever you need to do here, based on your object
NSInteger tagIndex = [(UIButton *)sender tag];
// Pass the information to your destination view
[vc setSelectedButton:tagIndex];
}
}
Hope this help but please go through refernces!
For example I have 3 views:
SaveContactView (TableView - Editable)
DetailedContactView (View - Display only)
ListContactView (TableView - Display List)
ListContactView contains dynamic records of Customers. Tap on cell will segue to DetailedContactView.
ListContactView contains 'add' button which segue to SaveContactView.
SaveContactView when user save will go to DetailedContactView.
DetailedContactView, user can 'edit' and goes back to SaveContactView
This is my question:
ListContactView to DetailedContactView OR SaveContactView to DetailedContactView
On viewDidLoad on DetailedcontactView is it possible to call different methods/functions when he comes from a certain view?
I do not want to create some redundant extra duplicated 'alike' view. So is there any best approach?
You can simply apply a check in viewDidLoad of your DetailedContactView that who is the parent of this view.
If you are using push segue then check the parent view controller of this view controller in UINavigationControler stack.
If you are modally present this DetailedContactView then find the [self presentingViewController] and take appropriate action.
Or set a flag from last page to distinguish between parents.
May be this will help you.
The correct way is to implement -prepareForSegue:sender: of SaveContactView and of ListContactView. Cast the destination of the segue to DetailedContactView type and perform custom setup.
// SaveContactView
- (void)prepareForSegue:(UIStoryboardSegue *)segue
sender:(id)sender
{
DetailedContactView *detailedVC = segue.sourceViewController;
[detailedVC setupForShowingFromSaveView];
}
// ListContactView
- (void)prepareForSegue:(UIStoryboardSegue *)segue
sender:(id)sender
{
DetailedContactView *detailedVC = segue.sourceViewController;
[detailedVC setupForShowingFromListView];
}
Is there a way to have a different prepareForSegue's for different segues coming out the same view controller?
Because I have 2 segues coming out of one view, and there obviously are some workarounds, but I was wondering if it was possible to separate the prepareForSegues into 2 entirely separate methods depending on the segue called.
No, you cannot use two different methods.
You can however call two different methods in a conditional statement like bellow:
You can check which segue was invoked:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if ([segue.identifier isEqualToString:#"addNewCheckListItem"]) {
} else if ([segue.identifier isEqualToString:#"showExistingCheckListItem"]) {
}
}
But make sure you also set them an identifier from Interface Builder
I have a view controller namely DetailViewController in which is embedded a container view controller, IndividualDetailsController.
I would like to know whether is it possible that some change in the DetailViewController could bring about changes in the container view controller i.e, the IndividualDetailsController.
For eg, I would want a button click in the DetailViewController to change a text field value in the IndividualDetailsController controller. Is it possible to do so using IBActions? Any help would be much appreciated.
In the method
prepareForSegue
of DetailViewController keep a reference to the embedded one (IndividualDetailsController) in a property of IndividualDetailsController.
Then from an action in DetailViewController you can, for example, change a property of the IndividualDetailsController that can then change its interface accordingly.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"MyEmbedSegue"])
{
self.individualDetailsController = (IndividualDetailsController*)[segue destinationViewController];
}
}
then in the action
self.individualDetailsController.textFieldValue = newValue
I've been playing around with custom segues in iOS and am trying to use them as much as possible to allow the complete workflow of the application to be visualised in my storyboards. When I create say a popover segue, it knows about the view triggering the segue (it must in order to position the popover), but when I create a custom segue, I cannot find a way of doing the same. Is there a way of accessing the view that initiated the segue in a custom segue? Also, is there a way of passing custom parameters to a segue from Interface Builder, much like you can pass runtime arguments to a view controller?
You can access to the View that has triggered a custom segue by implementing the method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
... on your View Controller.
On that method, the value of sender will be the view that triggered the segue, if you linked it using Interface Builder. When implementing this method, it is recommended to query what segue has been triggered in the following way:
if ([[segue identifier] isEqualToString:#"segueid"])
... where "segueid" would be the identifier you have given to your segue in Interface Builder.
About your second question, I don't know any way to specify parameters for a segue in Interface Builder, but you can use the same "prepareForSegue" method, to pass them from your View Controller at Runtime.
Imagine that your custom segue has a property called animationStyle
if the perform method, you can have the following:
- (void)perform
{
switch (self.animationStyle) {
case 0:
// TODO Perform animation type 0
case 1:
// TODO Perform animation type 1
}
}
Then, let's say you have a couple of View Controllers where you want to use that custom segue. In the first one you can implement the performSegue method like:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"yourCustomSegueID"]) {
((YourCustomSegueClass *)segue).animationStyle = 0;
}
}
While in the second view controller you can implement it like:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"yourCustomSegueID"]) {
((YourCustomSegueClass *)segue).animationStyle = 1;
}
}
... So effectively, you are reusing the same custom segue class for getting different effects. It is still true that IB will not let you configure the segue directly, but there's no way around that as far as I know.