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
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!
I have 2 view controller
Main has a button
Detail has a label: lblDetail
I'm trying to pass data by segue : "sgPushDetail". When click on the button on Main, the label on Detail will be changed.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"sgPushDetail"]) {
DetailViewController *detail = segue.destinationViewController;
detail.lblDetail.text = #"you've already clicked";
}
}
But when i build, the label doesn't change. Please help me with this case!
The DetailViewController is not loaded when you are setting value for the label in prepareForSegue method. Do the following:
Create a property (i.e. NSString) in DetailViewController
Set the value for this property in prepareForSegue method
In viewDidLoad method of your DetailViewController, assign this property value to the label.
In my app, I use segment to switch in three view. Every segmment represented with container view and they become hidden or appear according to segment. That is how my the design works, and it works well. You can find the picture below, so you can understand the structure more:
I'm having trouble with giving an instance (namely, user) created in the view controller to segment's container's view controller. I created user in the user and assign it the values. user instance has the values, I checked it. So, as usual to give with segue, I tried prepareForSegue:sender method to achieve my purpose. In the viewLoad method of the table views(you can find the table views: each of them are attached to its own container in the view controller that has segment. There are 3 containers.) user instances of table views are null. I can't give it, basically. Shouldn't the view controller pass the user value? It might being container view, but still I think it should be :) Anyway, please find below what I have written so far:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
DovizTableViewController *controller = (DovizTableViewController *)navController.topViewController;
controller.user = self.user;
}
You would remove the sender part of your code.
It should look like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue{
//your code
//check if the next view exists by checking for the id
if ([[segue identifier] isEqualToString:#"Segue_Name"])
{
DovizTableViewController *controller = [segue destinationViewController];
controller.user = self.user;
}
}
Try this.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
DovizTableViewController *controller = [segue destinationViewController];
// Pass any objects to the view controller here, like...
controller.user = self.user;
}
}
Hope it helps.
I have a UIPageViewController and have a button in it. Whenever the button is pressed I want to perform a Segue from the parent view controller (which has a navigation controller embedded) to the next view controller in the navigation stack. I also want to be able to pass data through the segue. I've tried a couple of things but I'm very new to iOS development and have not been successful at all.
You need to select the segue in your storyboard and give it a unique identifier and the put that in the code below.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"YourSegueIdentifier"]) {
// get a reference to the destination View Controller
UIViewController *destinationVC = [segue destinationViewController];
XXYourViewControllerSubclass *yourVC = (XXYourViewControllerSubclass*)destinationVC;
// create the data and pass it to the view controller
id someData = // create your data (unless you have a property holding it already)
[yourVC acceptData:(id)someData]; // make a public method on your VC subclass to accept the data
}
// after this method has returned the seque will be performed
}
Does that make sense?
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.