How to Pass Data Back [duplicate] - ios

This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 8 years ago.
This is the scene of my storyboard.
UINavigationController -> UITableViewController -> push segue -> UIViewController
I know how to pass the data of UITableViewController to UIViewController.
But, how can I pass the data back to the UITableViewController?(UIViewController has a navigation bar with a back button)

You can do it through the use of delegation.
When you pass data of UITableViewController to UIViewController, you can set a delegate on UIViewController. Whenever you want to pass data back to UITableViewController, you can call the delegate and pass in the data
Here's a more concrete example in code
- (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
UIViewController *vc = [segue destinationViewController];
// Pass the UITableViewController to the UIViewController
[vc setDelegate:self];
}
}
Create a method on UITableViewController that takes data from UIViewController (example: -(void)processDataFromUIViewController:(NSString *)string)
Now when you want to pass data back to UITableViewController, call [delegate sendDataFromUIViewController:#"data"]

Related

Passing Bool Value from one viewcontroller to another in ios [duplicate]

This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 6 years ago.
I am trying to send a BOOL value from
ViewController A to ViewController B.
On ViewController A, dashboard.skip shows YES, but on
ViewController B, self.skip shows NO.
- (IBAction)skipToDashboard:(UIButton *)sender {
ViewController B *vc=[[ViewController B alloc]init];
vc.presentButtonTag=sender.tag;
self.fromSkip=YES;
vc.skip=self.fromSkip;
}
In ViewController B:
#property (nonatomic,assign) BOOL skip;
There are 2 ways to pass your data from First screen to 2nd screen.
1st Way:
You can pass your data by creating VC object and assign value in it and push to next screen like below example.
Code :
//Initilize view controller from storyboard id.
ViewController *objViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewController"];
//Assign value
objViewController.presentButtonTag=sender.tag;
self.fromSkip=YES;
objViewController.skip=self.fromSkip;
//Push view
[self.navigationController pushViewController:objViewController animated:YES];
2nd Way:
By Storyboard you can set segue action on button and in prepare segue method you need to pass your data like this.
Code:
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (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:#"ViewController"]) {
ViewController *objViewController = (ViewController*)segue.destinationViewController;
objViewController.selectedTag = ((UIButton*) sender).tag;
//Assign value
objViewController.presentButtonTag=sender.tag;
self.fromSkip=YES;
objViewController.skip=self.fromSkip;
}
}
Now you can check your scenario in both condition I think you are pushing to next screen by storyboard so use 2nd Way.
Let me know if you need more help.
Try to define BOOL property and set value either true or false.
For Ex: Appdelegate declare property and then use
#property (nonatomic) BOOL fromSkip;

How to connect to a UITableViewController

I have 2 views in my storyboard.
One is the defualt view connected to ViewController. I'll refer to it as VC.
The other view is one I created by dragging to the storyboard a UITalbeViewController. It's connected (through the custom class) to a new cocoa touch class, MyTVC. I'll call it TVC.
I connected a button on VC to TVC through a segue.
The problem is, now I have an array in ViewController, and I can't pass it to the MyTVC instance that populates the TVC view.
If I just create an instance of MyTVC in my ViewController.m, it's not connected to TVC. How could I access the MyTVC instance that's already connected to the TVC view?
In the VC class, you need to implement the prepareForSegue method. That method can obtain a pointer to TVC, which you can use to pass information from VC to TVC.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ( [segue.identifier isEqualToString:#"ShowTable"] )
{
MyTVC *tvc = segue.destinationViewController;
tvc.arrayForTable = self.arrayForTable;
}
}
Note that the segue identifier is a string that you assign to the segue in the storyboard. You don't really need to check the segue identifier if you only have one segue.

Using pushViewController is very slow on iPad

I'm trying to move from one UIViewController to another using -pushViewController:animated using the below snipped :
SomeController *tabBar = [self.storyboard instantiateViewControllerWithIdentifier:#"mycus"];
[self.navigationController pushViewController:tabBar animated:YES];
Moving from one UIViewController (which has no UIViews or images) has a 3 second delay before moving. What is causing this issue and how would I solve it?
For storyboard, click on the segue you have created, give it an identifier like "someSegue".
Next you will want to trigger the prepareForSegue delegate in your viewcontroller doing the following...
[self performSegueWithIdentifier:#"SomeSegue" sender:nil];
..Then you will want to include the delegate method in your view controller and do any preparing you might want before the next view controller is initialised.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
SomeController *vc = [segue destinationViewController];
// Here you will want to do your prep e.g. pass iVars etc from one controller to the other.
}

Perform Segue from Root View Controller of a UIPageViewController

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?

How to pass arguments between view controllers [duplicate]

This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 8 years ago.
I'm looking for a way to pass arguments from view controller to another. For example:
I've designed one view controller that receives username and password, and has a submit button which leads to another view controller, which I've designed in main.storyboard. The connection between the controllers is by dragging the button to the second view controller and choosing 'modal' in the connection type.
How can I transfer the user details to the second view controller?
You'll need to use prepareForSegue:sender: method. As taken from https://stackoverflow.com/a/7865100/657676:
- (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];
// or
vc.property = value;
}
}

Resources