IOS Segue not able to set label directly - ios

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

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

Can't set the properties for the next view using prepareForSegue: sender:

I'm using Objective-C. I set a segue from a table view cell to a new view controller, and I want to set the properties for the new view controller using this method:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString: #"detail"]){
if([segue.destinationViewController isKindOfClass:[detailViewController class]]){
detailViewController *detail = (detailViewController *)segue.destinationViewController;
detail.label.text = #"QQQQQ";
}
}
I've set the segue's identifier to #"detail" already, and also connected the label code with the view controller in storyboard. But when I run this in my simulator, the label would never change.
at the time when prepareForSegue is called the destinationviewcontroller's outlets are not set yet.
what you have to do is create a property in your detailViewController class like
#property (copy, nonatomic) NSString *detailText;
then in prepareForSegue:
detailViewController *detail = (detailViewController *)segue.destinationViewController;
detail.detailText = #"QQQQQ";
and in your detailViewControllers viewDidLoad:
self.label.text = self.detailText;

Loading another view in the same viewController clears non UI properties

I have a UIViewController controlling several different views in Xcode. I want to set a label on the incoming view using information from the current view. I know labels and other UI elements get reset when the view is loaded, but my non UI #property is being set to nil as well.
Here is my property:
#property (strong, nonatomic) NSString* username;
Here is the code that sets it:
//NSLog(#"%#",dict);
if ([dict[#"login"] intValue]){
[self performSegueWithIdentifier:#"JoinSuccess" sender:sender];
self.username = dict[#"user"];
Here is the code that tries to use it:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"%#", self.username);
self.welcomeLabel.text = [NSString stringWithFormat:#"Welcome, %#",self.username];
}
I've tried setting username both before and after performSegueWithIdentifier, but in either case when I try to reference it in viewDidLoad the value is nil. Any idea as to why it's reseting my #property? My guess is maybe it's creating a new instance of my viewController. If that's the case, how can I pass the needed variable?
As you said the viewController will be created again when ever you call performSegueWithIdentifier. You could set the value in the new instance. You can do this in
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
YourViewController *vc = [segue destinationViewController];
vc.username = dict[#"user"];
}
you have to add another method to pass value from one view controller to another view controller .
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
nextViewcontroller *nextViewcontrollerObjt = [segue
destinationViewController];
// apply setter to set value of next view
nextViewcontrollerobject.title = nextViewControllerTitleString;
nextViewcontrollerobject.variable_1= currentView_variable1;
nextViewcontrollerobject.variable_2 =currentView_variable2;
}

Get ViewController from Storyboard

I have two ViewControllers, which aren`t (directly) connected with a Segue. But I want to change a String in the second VC, which i get in the first one. My try was:
#import "secondViewController.h"
#interface firstViewController ()
#property NSString* originalString;
#end
#implementation firstViewController
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
secondViewController* svc = [secondViewController new];
svc.anotherString = self.originalString;
}
But it dosent work, because I've only created a instance of the second VC, so the value was not saved. Also I can`t use the Storyboard ID, because I use Xcode 5.
I have a menuVC from which you can get to the firstVC and the secondVC. And from the firstVC I can go back (with the navigationbackbarbutton) to the menu. so: menu->firstVC->menu. menu->secondVC->...->menu
My try with StoryboardID:
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
secondViewController* svc =[[UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:#"secondVCSrorybradID"];
svc.anotherString = self.originalString;
}
you can pass the string to second view controller with this code.
secondViewController* svc =[self.storyboard instantiateViewControllerWithIdentifier:#"Your Second VC's Storyboad ID"];
svc.anotherString = self.originalString;
[self presentViewController:svc animated:YES completion:nil];
//you have to create anotherString property in second View Controller's .h File.
now you can get the string of originalString to second VC. Now you can get this value back second VC to first VC.
hope this helps you.
You should pass your data successively through your UIViewControllers navigation. If, for example, you have a navigation like FirstVC > SecondVC > ThirdVC :
In your FirstVC.m, use :
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
((SecondVCClass*) segue.destinationViewController).secondVCString = _firstVCString;
}
With secondVCString being a #property in your second ViewController.
In your SecondVC.m, use :
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
((ThirdVCClass*) segue.destinationViewController).thirdVCString = _secondVCString;
}
With of course thirdVCString being a #property in your third ViewController.
Edit:
As you updated your question, here is what I suggest :
In your MenuVC, add this :
#property (nonatomic, weak) NSString *importantString;
In your FirstVC and SecondVC, add this :
#property (nonatomic, weak) MenuVCClass *menu;
When you push to FirstVC or SecondVC, use prepareForSegue to set the destination's view controller menu property to your menu :
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"FirstSegue"])
((FirstVC*) segue.destinationViewController).menu = self;
else if ([segue.identifier isEqualToString:#"SecondSegue"])
((SecondVC*) segue.destinationViewController).menu = self;
}
In FirstVC or SecondVC, you can change the NSString value from your menu using _menu.importantString = #"";
You should never use new to create a view controller.
If you're using storyboards but not using segues, you can still create a view controller from the storyboard and invoke it.
Use the method instantiateViewControllerWithIdentifier: to create an instance of the target view controller. The set the properties you want to set, and finally make a call to display it (present it modally, push it onto the navigation stack, or whatever is appropriate for your program.)

Passing Data from a View Controller to a First Detail Controller and then to a Second Detail Controller

Posting my first question here on stackoverflow! Hope my question is clear, here it is...
I have a Navigation Controller embedded in a First View Controller that has a table view. From the table view I have a segue that pushes to a First Detail Controller. Everything works fine, the segue passes the data to the First Detail Controller, however I would like to push again from a button to a Second Detail Controller.
The layout in the storyboard looks like this Image of storyboard
I set-up a Second Detail Controller and pushed to that from a button on the First Detail Controller.
However I can not figure out how to pass the data I have in an array along to the Second Detail Controller. I added an NSLog line to see what was being passed along and the debug panel is outputing "(null)"
Here is my viewdidload and segue code from the .m First View Controller named ColorBookViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
Color *color1 = [Color new];
color1.name = #"Red";
color1.hexnumber = #"FF0000";
color1.spanishname =#"Rojo";
Color *color2 = [Color new];
color2.name = #"Green";
color2.hexnumber = #"00FF00";
color2.spanishname =#"Verde";
Color *color3 = [Color new];
color3.name = #"Blue";
color3.hexnumber = #"0000FF";
color3.spanishname = #"Azul";
colors = [NSArray arrayWithObjects: color1,color2,color3, nil];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"showColorDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
ColorDetailViewController *destViewController = segue.destinationViewController;
destViewController.color = [colors objectAtIndex:indexPath.row];
}
}
Here is the view did load from the .m of the First Detail Controller named ColorDetailController:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"This is...%#", color.name);
self.nameLabel.text = color.name;
self.hexnumberLabel.text = color.hexnumber;
}
My .h for the SecondDetailController:
#import <UIKit/UIKit.h>
#import "Color.h"
#interface SecondDetailViewController : UIViewController
#property (nonatomic,weak) IBOutlet UILabel *spanishnameLabel;
#property (nonatomic, strong) Color *color;
#end
Here is the view did load from the .m of the Second Detail Controller named SecondDetailController:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = color.name;
NSLog(#"This is...%#", color.spanishname);
self.spanishnameLabel.text = color.spanishname;
}
The app runs fine in the simulator, but I get a blank screen for the Second Detail Controller and I get the "null" result from NSLog(#"This is...%#", color.spanishname);
Any suggestions as to why the segue passes and holds the instance for the first Detail View but not the Second Detail View? Or am I missing something more fundamental here? I tried setting up a second segue on the First Detail Controller and the NSLog produces the correct result (the color name in Spanish), but I am not sure how I would pass this onto the Second Detail Controller:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showMoreDetail"]) {
NSLog(#"This is... %#",color.spanishname); //works fine
self.spanishnameLabel.text = color.spanishname; //throws error
}
}
Thanks for any help or insight!
Download the complete project here
To pass the data to the second ViewController, just do exactly the same thing as you did in the first ViewController: pass the reference of the Color object in the segue.
In the SecondDetailViewController, you already have a color property. You just have to set this property with the same Color object that was selected in your ColorBookViewController. As you already have this reference in the ColorDetailViewController, just pass it directly to the SecondDetailViewController, via segue.
Inside your ColorDetailViewController:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"showMoreDetail"]) {
SecondDetailViewController *destViewController = segue.destinationViewController;
destViewController.color = self.color;
}
}
Now, in the viewDidLoad of the SecondDetailViewController, you would be able to set the labels correctly, like in self.spanishnameLabel.text = color.spanishname;

Resources