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

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.

Related

How To Pass Data Between View Controllers On Different Storyboards?

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]];
}
}

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];

How to use custom Navigationcontroller in AppDelegate by using a Storyboard

I've got a problem concerning Navigationcontroller in AppDelegate. I'm using a storyboard, which looks like this:
As a result of using Push notifications, i've got the following function in my AppDelegate File:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
//...
}
When the notification arrives I want to initialize the "Detail View" - Controller which needs an ID as a parameter. This ID is part of my payload so it is present in didReceiveRemoteNotification.
I'd like to to the follwing:
DetailView *detail = [storyboard instantiateViewControllerWithIdentifier:#"detail"];
detail.conversationID = theID;
[self.navigationController pushViewController:detail animated:YES];
My question at this point is: how can I get the navigation controller? I've searched for a function like "getNavigationControllerByIdentifier" or something like this, but found nothing. I can't instantiate the Detail View Controller directly because the navigationbar is missing there.
I hope you understand what I mean - if you think my approach is completly wrong please correct me ;o)
Just another small information: It's not important for me that the back button in the Detail View Controller goes back to the Table View - it's enough when it links to the controller with the button "Load Table View".
Thank you for help!
UINavigationController is a UIViewController subclass and can also be assigned an identifier in the storyboard.
Use -instantiateViewControllerWithIdentifier: to create the UINavigationController and it's root view controller. You may need to instantiate all of the intermediate controllers in code and modify the navigation controller's viewControllers property to set up the appropriate navigation stack. This way when the app launches into the detail view, they can find their way back as if they had manually pushed all the way through via the interface.
You can use rootViewController on your window object.
UIViewController *rootViewController = self.window.rootViewController;
// You now have in rootViewController the view with your "Hello world" label and go button.
// Get the navigation controller of this view controller with:
UINavigationController *navigationController = rootViewController.navigationController;
// You can now use it to push your next view controller:
DetailViewController *detail = [navigationController.storyboard instantiateViewControllerWithIdentifier:#"detail"];
detail.conversationID = theID;
[navigationController pushViewController:detailViewController animated:YES];

Resources