How To Pass Data Between View Controllers On Different Storyboards? - xcode

I use multiple storyboards in an XCode project. There are multiple view controllers on each of the storyboards.
Using prepareForSegue to transition between view controllers on different storyboards works fine but what is the best way to pass data between the view controllers on different storyboards.
For example, how can I pass parameters/data from ViewController1 on storyboard1 to ViewController2 on storyboard2?

In your segue, you get access the destinationViewController which is the view controller are about to load. You can use this reference to set any data you desire.
This is an example of passing selected cell data from a UITableView to the destinationViewController.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"showNoticeDetails"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
[segue.destinationViewController setNotice:[notices objectAtIndex:indexPath.row]];
}
}

Related

how to push data from one viewController to another without using Segues

i have two view controllers TestViewController and SecondViewController.
i also have corresponding views TestView and SecondView corresponding to them.
please guide me with the steps to push data from one view Controller to another without using Segues.
you can use Segues it is very easy to work with. You can use method inside your objective-c code:
for 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];
}
}
In the link above you have all the explanation how to it... :) hope it helps.

Passing data from detail to master in ios6

I'm making an application with xcode, and I have a trouble with passing data between views.
I want to set in detail view, a date of a calendar. Then when I go back to the master view I want to see the events in the selected date, but I don`t know how I make it.
Can you help me?
this is how you can communicate between 2 class
ViewController *dvController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:[NSBundle mainBundle]];
// ViewController is controler where you want to send date from particular controler we are making object of ViewController where you want to send data
dvController.selectedBirthdate = selectedbirthdate;
dvController.selectedName = selectedname;
//you are sending data in above two lines just imagine you are sending string
[self.navigationController pushViewController:dvController animated:YES];
//then pushviewcontroller and there you go
[dvController release];
simple as that
there is another way to comunicate between 2 classes is that app delegate make object of you app delegate and then assing what you want to particular varible of app delegate and then you can use that variable anywhere in project
create app delegate object like this
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
//and then access the variable by appDelegate.variable
if you are using storyboard then you can use prepareForSegue like below
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:...]) {
MyViewController *controller = (MyViewController *)segue.destinationViewController;
controller.myProperty1 = ...;
controller.myProperty2 = ...;
}
}
There are basically two most common approaches:
1) use unwind segue if you're using storyboard in the project. The approach is perfectly discussed here:
http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/SecondiOSAppTutorial/CreatingAddView/CreatingAddView.html
2) use delegate pattern. I found the below tutorial quite useful when I had started to learn delegation:
http://www.roostersoftstudios.com/2011/04/12/simple-delegate-tutorial-for-ios-development/

How can I code my view controller to perform a push segue to another view?

How can I code my view controller to perform a push segue to another view? I know how to do it with the storyboard, but, I can't figure out how to do it with some coding.
If your view controllers are connected with a segue and if you just want to perform that segue just do:
[self performSegueWithIdentifier:#"identifier" sender:self];
If they're not connected you can set the Storyboard ID of your view controller in the Identity Inspector (⌥⌘3) and use this code to initiate the push segue:
UIViewController *aViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"Your Storyboard ID"];
[self.navigationController pushViewController:aViewController animated:YES];
Also, these methods are really useful to manipulate segues:
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

How perform an animated custom segue in storyboard w/o NavigationController?

I have the necessity to set some parameters before execute a segue, so I must use CUSTOM SEGUE, right?
In this case, I would make the transition, from a screen to another, animated but I don't have a NavigationController (and I don't want to insert one!)
Is it possible to not use a NavigationController and change views with a custom segue?
In alternative, there is the possibility to set an action for a button and execute some rows of code befor performing a segue?
I found this solution :
(IBAction)showDetailView:(id)sender {
//code
.....
[self performSegueWithIdentifier:#"ShowDetail" sender:sender];
}
but it need navigtion controller...
thank you to all and sorry for my bad englis!
When you create you segue in Storyboard, select "modal" instead of "push" (custom refers to a third type which I don't think you need). Select the segue and use the attributes inspector to give it a name. In my code example I use the name "editTitleBlock".
To set properties on the destination view controller (which will be the modal view controller) put a prepareForSegueMethod in your first view controller like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"editTitleBlock"]) {
[[segue destinationViewController] setTitleFieldString: #""];
[[segue destinationViewController] setAltitudeFieldString:currentLocation.localizedAltitudeString];
[[segue destinationViewController] setLocationFieldString:currentLocation.localizedCoordinateString];
[[segue destinationViewController] setAuthorString:userName];
if ([[segue identifier] isEqualToString:#"cancel"]) {
// do nothing special
}
}
to get back to the first view controller use:
[self dismissModalViewController animated:YES];

Cocoa: Adding a subview to a view from a different class and nib

If i have two nibs with several views, is there a way for me to use the addSubview: method between them? What I would like to do is take a view from one of the nibs and tell it to add a subview that would be a view in the other nib file.
The reason I have them in separate nibs is because the subview from the second nib will be added several times, using the same template but different parameters.
Yes, you can add a view in one nib as a subview to the view in another nib.
You need to create a NSViewController object which will own the child nib. So that as soon as you initialize the view controller the nib associated with it is loaded. Now you can use the view property of the controller and add it as a subview to any other view.
The code below will help you understand better:
YourViewController.m
-(id)init
{
self = [super init];
if(nil != self)
{
[NSBundle loadNibNamed:#"myNibName" owner:self];
}
return self;
}
YourOtherClass.m
-(void)addYourViewControllerViewAsSubview
{
YourViewController *yvc = [[YourViewController alloc] init];
[yourOtherViewOutlet addSubview:yvc.view];
}

Resources