How to make work preparForSegue :sender: XCode 6? - ios

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.

Related

Need some insight to make a 3 UIButton menu in XCode that toggles and changes dataset in the main ViewController

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!

Can you println something and do a function based on the last println?

So I have a tableview and when someone taps on a row, I want to println(rowtext) and then segue to another viewcontroller. In the new view controller I was hoping to use the rowtext from the println to use as a title and query rowtext. Is this possible.
Woops, fixed the post "rowtext" wasnt showing.
You cant get data from a print statement. Try making a global variable to pass the data...
Follow these steps...
1: Create a Separate file called Manager.swift and place this code in it...
//manager.swift
import Foundation
struct Manager {
static var rowNumber = Int()
}
2: Clean your project by pressing Shift+Command+K.
3: In the first view controller set the rowNumber to the data you want to pass...
Manager.rowNumber = self.dataToPass
4: In the second view controller retrieve the data and set the content to the rowNumber...
self.dataToReceive = Manager.rowNumber
5: Your Finished!!
From the segue you can access the destination view controller with:
ViewController *destViewController = [segue destinationViewController];
Set a property on the destination controller (say rowText) and do:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"yourSegueIdentifier"])
{
ViewController *destViewController = [segue destinationViewController];
destViewController.rowText = <rowtext that you want to pass>;
}
}
If you are embedded in a nav controller, use:
UINavigationController *navController = (UINavigationController*)[segue destinationViewController];
ViewController *destViewController = (YourViewController* )[navController topViewController];
destViewController.rowText = <rowtext that you want to pass>;

IBAction in a container view controller

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

Access to container view controller

I have simple ViewController which have one container view with TableViewController.
At ViewController I loading data from external API and one part of that data, I need to pass into TableViewController in container.
Is there any way how to do that?
Thank you!
The controllers contained in a container view can be accessed by self.childViewControllers from the parent controller. If you only have one, then it will be at self.childViewControllers[0].
Note: regarding RD's excellent technique explained in the comment below; here's a typical example, tested and working: this simply goes in the VC of the overall scene. Just click on the segue itself (ie, the small symbol in the middle of the connecting arrow) to set the text identifier.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"containerLogin"])
self.vcLogin = (LoginVC *)segue.destinationViewController;
if ([segue.identifier isEqualToString:#"containerStartNew"])
self.vcStartNew = (StartNewVC *)segue.destinationViewController;
}
Here's just how to do it in Swift: (you have to be careful with the unwrappers)
override func prepareForSegue(segue:(UIStoryboardSegue!), sender:AnyObject!)
{
if (segue.identifier == "feedContainer")
{
feed = segue!.destinationViewController as! Feed
feed.someFunction()
}
}

prepareForSegue and delegates

I have an application with two segues. In one of the segues, the current view controller becomes a delegate and the other does not.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"MoreOptions"]) {
UINavigationController *navigationController = segue.destinationViewController;
MoreOptionsViewController *controller = (MoreOptionsViewController *)navigationController.topViewController;
controller.delegate = self;
} else if ([segue.identifier isEqualToString:#"FullStoryView"]) {
SingleStoryViewController *detailViewController = segue.destinationViewController;
detailViewController.urlObject = sender;
}
}
All of this is working fine, but I would like to try and understand the code better. What I don't understand is that I have to get a reference to the MoreOptionsViewController by grabbing it from navigationController.topViewController rather than simply getting it from segue.destinationViewController like I do in the second if condition. Is it because I'm setting the current view controller (self) as the delegate? Again, I'm not trying to solve a problem, just trying to get a better understanding of what's going on.
Take a look at your storyboard and it should be evident why this is the case. You have embedded MoreOptionsViewController in a UINavigationController and connected a segue to the navigation controller, thus making it the destinationViewController. This is fairly common.
The delegate is largely irrelevant in the context of your question.
Your first segue's destination is a navigation controller, which contains the view controller you are really interested in. Therefore to get to that view, you need to go through the navigation controller since that won't have any properties you are interested in setting.
Your second segue goes directly to a single view controller, so you can access it directly.

Resources