I'm fairly new to Objective-C, programming. The one thing, I'm currently putting up with is passing a value from the first ViewController
to the next one.
I've read this entry here and it didn't help me. Which is funny, since his answer has nearly 500 votes, so I must be the problem. What I did was the following.
I opened my PreviewViewController.m
and added the following line #import "MainViewController.h"
since I wanted to pass a value from the PreviewViewController
to the `MainViewController. Then, when I switch the layouts ( which successfully works ) I want to pass a value.
MainViewController *mainViewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
mainViewController.userId = @"539897197";
As you can see, I want to pass the userId. For that, I also created a property in the MainViewController.h
@property ( nonatomic, strong ) NSString *userId;
Now, In my MainViewController.m
I want to access the userId. But when I log it, the console tells me it is null
. However, when I set the variable right before the NSLog
it works, so it seems like the passing is the problem.
Additionally in the MainViewController.m
I have the following line
@synthesize userId = _userId;
but even when I removed that line and changed the NSLog
to NSLog(@"%@",self.userId);
the same problem occurred.
How can I successfully pass the variables? Which step am I doing wrong?
EDIT This is how I switch the layouts
UIViewController *viewController = [[MainViewController alloc]init];
MainViewController *mainViewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
mainViewController.userId = @"539897197";
[self presentViewController:viewController animated:YES completion:NULL];
Why not create a custom initializer and pass it in that way? Something like
Then you can just do:
I tried and got the solution for passing the value between viewcontroller.
or using this way . You don`t use the storyboard use the following its working fine
use mainViewController rather than viewController when presenting the view controller
If you are using
storyboard
then you should useprepareForSegue:
method to pass data between view controllers. First Create the segue from yourPreviewViewController
toMainViewController
, just control drag from your view controller to next viewcontroller to create segue. UseUINavigationController
if you are using push segue. Use this method to segue and pass dataHere we have two ways for passing data.
First is Custom Delegate
Second is NSNotification
Here we have two view controllers.
ViewController and SecondViewController
in SecondViewController
.h
.m
in ViewController
.h
.m
For your understanding the code I create a simple passing data from one second view controller to first view controller.
First we navigate the view from first view controller to second view controller.
After that we send the data from second view controller to first view controller.
NOTE : You can either use NSNotification or Custom Delegate method for sending data from One View Controller to Other View Controller
If you use NSNotification, you need to set the postNotificationName for getting data in button action method.
Next you need to write addObserver in (sending data to your required View Controller) ViewController and call the addObserver method in same View Controller.
If you use custom delegate, Usually we go with Custom Protocol Delegate and also we need to Assign the delegate here.
Very importantly we have to set the Custom Delegate Method in the Second View Controller.Because where we send the data to first view controller once we click the done button in second view controller.
Finally we must call the Custom Delegate Method in First View Controller, where we get the data and assign that data to label.Now you can see the passed data using custom delegate.
Likewise you can send the data to other view controller using Custom Delegate Methods