pass data between view controller using storyboard xcode6 - ios

i have many view controller in my storyboard some how i am passing my login page data email id to my third view controller but i am getting the NULL value
here is my code for 1st view
//in first view i already import my second view below code is in 1st view
//.h
#property (weak, nonatomic) IBOutlet UITextField *emailid;
//.m
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"sendemailid"])
{
UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
UploadImageData *controller = (UploadImageData *)navController.topViewController;
controller.newemailid = self.emailid.text;
NSLog(#"emailid is %#",emailid.text);
}
}
// i already created segue by identifier name "sendemailid"
now in my second view
// in .h
#property (strong, nonatomic) IBOutlet UILabel *emailid;
#property (nonatomic) NSString *newemailid;
// .m in view didload method
self.emailid.text = self.newemailid;
NSLog( #"we get the emailid %#", emailid.text );
i am getting null value and one more thing i am using NSUSER DEFAULT to save emailid in 1st view. i don`t able to know why value is not passing is that i am missing something please suggest...

I have done with this
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"sendemailid"])
{
UploadImageData *controller=segue.destinationViewController;
controller.newemailid=self.emailid.text;
}
}

Related

Passing UITextField text to UILabel on different View Controller

I am trying to pass UITextField's text from View Controller 1 to the UILabel in View Controller 2.
I am using segue to pass the information, but I am not getting anything on Label. It seems like the text from the text field becomes NULL in when it is passed to view controller 2.
View Controller 1 (UITextField)
- (IBAction)sendtoVC2:(UIButton *)sender
{
[self performSegueWithIdentifier:#"toVC2" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"toVC2"])
{
ViewController2 *VC2 = (ViewController2 *)segue.destinationViewController;
VC2.label.text = self.textField.text;
}
}
View Controller 2.h (UILabel)
#import "ViewController1.h"
#interface ViewController2 : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *label;
#end
Thank you.
Most likely, it happens because the label doesn't exist yet. When UIViewController is created, its view is not loaded until it is actually required. This is called 'lazy loading', which means that a value is created only when somebody requires it for the first time.
Lazy loading is a design pattern commonly used in computer programming
to defer initialization of an object until the point at which it is
needed. It can contribute to efficiency in the program's operation if
properly and appropriately used.
Your UILabel property in second view controller gets a value only after second view controller's view is loaded, which happens only when somebody explicitly calls its 'view' property. So, to prove my theory just insert one more line:
if ([segue.identifier isEqualToString:#"toVC2"])
{
ViewController2 *VC2 = (ViewController2 *)segue.destinationViewController;
UIView *unusedReferenceToViewToLoadTheView = VC2.view
VC2.label.text = self.textField.text;
}
In viewDidLoad of View Controller 2, the text is lost, because the label was not created yet.
You have to save the text in a variable, and in viewDidLoad of ViewController2, set the text in label.
Like this:
View Controller 1:
- (IBAction)sendtoVC2:(UIButton *)sender
{
[self performSegueWithIdentifier:#"toVC2" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"toVC2"])
{
ViewController2 *VC2 = (ViewController2 *)segue.destinationViewController;
VC2.text = self.textField.text;
}
}
View Controller 2.h:
#import "ViewController1.h"
#interface ViewController2 : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *label;
#property (nonatomic, copy) NSString *text;
#end
View Controller 2.m:
- (void)viewDidLoad {
[super viewDidLoad];
label.text = text;
}

Xcode Segue push to Label

I'm having a small problem with my push segue on Xcode Im trying to push the content of one label to another one on a different view controller, this is my current setup no errors are present and but the text on the other view controller doesn't change to match the previous window.
The first example below is my detailviewcontroller which I'm sending the information from. I have created the push segue between the detail view controller and my new view controller which is CODEVIEW and given the segue the name ShowCode.
I was wondering if I was missing line of code which would automatically activate this segues information without the need of a button but instead works when loaded.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:#"ShowCode"]) {
CODEVIEW *CodeSender = [segue destinationViewController];
CodeSender.CODElabel.text = _Code.text;
}
}
Label outlet for CODEVIEW
#property (strong, nonatomic) IBOutlet UILabel *CODElabel;
Label outlet for detail view controller
#property (strong, nonatomic) IBOutlet UILabel *Code;
In your current VC:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:#"ShowCode"]) {
CODEVIEW *CodeSender = [segue destinationViewController];
CodeSender.codeLabeltext = _Code.text;
}
}
In your target/next VC:
a) inside CODEVIEW.h interface:
#property (strong, nonatomic) NSString *codeLabeltext;
#property (strong, nonatomic) IBOutlet UILabel *CODElabel;
b) in your CODEVIEW.m implementation:
- (void)viewDidLoad {
[super viewDidLoad];
//...
_CODElabel.text = codeLabeltext;
//...
}

Error passing data between table view controller to detail view controller

I am getting an error while passing data from a table view controller to a detail view
controller. I am using a navigation controller along with it.
The error message is as follows:
use of undeclared identifier _wordLabel
This is the prepare for segue implementation in my table view controller (.m file).
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"ShowWordDetails"]) {
//UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
wordDetailViewController *detailViewController = [segue destinationViewController];
NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
long row = [myIndexPath row];
detailViewController.wordDetailModel = #[_wordLabel[row], _meaningLabel[row]];
}
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
I have previously declared wordLabel and meaningLabel in the detail view controller as follows:
#property (strong, nonatomic) IBOutlet UILabel *wordLabel;
#property (strong, nonatomic) IBOutlet UILabel *meaningLabel;
#property (strong, nonatomic) NSArray *wordDetailModel;
I have also declared it in viewDidLoad method of detail view controller as follows:
-(void)viewDidLoad {
[super viewDidLoad];
_wordLabel.text = _wordDetailModel[0];
_meaningLabel.text = _wordDetailModel[1];
// Do any additional setup after loading the view.
}
_wordLabel is private member of your detailViewController. You can't access it like you're trying to do in the first code quote. You need to call detailViewController.wordLabel instead.
first of all you can't use the array operator with a UILabel
_wordLabel[row]
second of all you have to define the _wordLabel[row] as an array in the TVC, that is separate from the label in the detailViewController
I think you don't require wordDetailModel as you can access UILabel from TableViewController as described below
detailViewController.wordLabel.text = #"Word";
detailViewController.meaningLabel.text = #"Meaning";
_wordLabel is UILabel and it is private variable in DetailViewController which can not be used directly from TableViewController, you have to use it using property. Plus _wordLabel is not an array as already answered by #sha and #rockstarr

IOS Segue not able to set label directly

I am having two view controllers 'FirstViewController' and 'SecondViewController'. From first view controller it will take input from a text field ,process it and display accordingly in the second view. But I am having a problem while setting label value directly.
#interface SecondViewController : UIViewController
{
NSString *numPlate;
IBOutlet UILabel *output;
};
#property(strong,nonatomic) NSString *numPlate;
#property(strong,nonatomic) IBOutlet UILabel *output;
#end
The main file for FirstViewController.m with prepare for segue is as
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:#"Change"])
{
SecondViewController *svc = (SecondViewController *)[segue destinationViewController];
svc.numPlate = input.text;
NumberPlate *numPlate=[[NumberPlate alloc]init];
[numPlate setPlate:input.text];
NSInteger flag=[numPlate checkValidity];
if(flag==0)
{
svc.output.text =#"Invalid License";
}
else
if([numPlate getArea]==NULL||[numPlate getRegOffice]==NULL)
{
svc.output.text =#"Data not found";
}
else
{
svc.output.text =#"VALID License";
}
}
}
But when the action is performed its not working.The label is not changing.
When i used svc.numPlate instead of svc.output.text and in the SecondViewController viewDidLoad method and i used
- (void)viewDidLoad
{
[super viewDidLoad];
output.text=numPlate;
}
Everything is fine with this. Whats wrong in first method??
You will not be able to assign value directly to UILabel of second VC as view is not yet loaded into view hierarchy at this point.
So view cannot render the value assigned prior to it.
On the other hand, holding value in NSString and assigning same on viewDidLoad is working as now your view is in view hierarchy and loaded into memory.
At the time when you push SecondViewController, the SecondViewController's view hasn't been loaded yet, so you can't access its views. You need to create NSString properties in SecondViewController and pass a string to SecondViewController' NSString Object. Then in SecondViewController's viewDidLoad method, use those properties to populate the labels (which will have been loaded by the time viewDidLoad runs).
The initialisation of controller is different and presentation of its view is different process even if the viewController has been initialise its view will not because pushing the controller has not performed and controller dont know he need to load the view.. so we pass the data to controller and it load when view appears... like below code
#interface SecondViewController : UIViewController{
NSString *strMessage;
}
#property(nonatomic,retain) NSString *strMessage;
#end
SecondViewController.m
#synthesize strMessage;
- (void)viewDidLoad {
Nslog(#"%#",strMessage);
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:#"Change"])
{
SecondViewController *svc = (SecondViewController *)[segue destinationViewController];
NSString *message = [NSString stringWithFormat:#"Check out %#", nameLb.text];
svc.strMessage=message;
}

Connecting data in MVC

I have a Navigation Controller and 2 Views Controllers. The first View Controller is associated with a UIViewController named ViewController. The second is connected to a UIViewController named BookVC. BookVC has a UITextField and is connected via an outlet:
#property (strong, nonatomic) IBOutlet UITextField *textFieldContent;
The connection is made with a button that uses a segue. I want to pass some data between the two and am using the following code which fails:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
BookVC* nextPage = [[BookVC alloc] init];
nextPage = [segue destinationViewController];
nextPage.textFieldContent.text=#"Some content";
}
How should I pass data between the View Controllers?
I think the problem is that textFieldContent doesn't exist at that point. You'll need to add a property to BookVC that can hold the text you want to put into textFieldContent... we'll call it #property NSString *textFieldContentText. So then,
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
BookVC *nextPage = [segue destinationViewController];
[nextPage setTextFieldContentText:#"Some content"];
...
}
And then, in the -viewDidLoad method of BookVC:
- (void)viewDidLoad
{
[textFieldContent setText:[self textFieldContentText]];
}
You don't need this line:
BookVC* nextPage = [[BookVC alloc] init];
That creates a brand new BookVC which you then go and overwrite in the next line.
Since you are using [segue destinationViewController] you should be good to go. What's happening when you segue to nextPage?

Resources