How to pass UILabel with NSUserdefualts? - objective-c

I'm trying to pass the value of UILabel with NSUserDefaults .
At the first viewController I have a picker and the picked item is a UILabel string.
In the second viewController I want the item that has been chosen from the picker to be simply displayed as a UILabel string .
How can I do this with NSUserDefaults, I know how do it with UITextField but it does not work for me with UILabel
Why does this happen?

Solution
To store the value of your UILabel to NSUserDefaults you can use
[[NSUserDefaults standardUserDefaults] setObject:label.text forKey:#"myLabelText"];
To get the value stored on NSUserDefaults and pass it to label you can use
label.text = [[NSUserDefaults standardUserDefaults] objectForKey:#"myLabelText"];
BUT
NSUserDefaults is not recommended to store dynamic properties. The easiest solution for you should be to pass data between different viewControllers:
Passing Data between View Controllers

Related

Sending multiple UITextField fields to a UITextView on a second ViewController (using a segue)

Firstly, Im new to Objective-C and i'm currently following several tutorials. Apologies if this is an obvious solution. I did use the search for several hours and couldn't find a solution.
I have no problem understanding how to send a data from a single UITextField to a UITextView in a new view controller.
MY QUESTION IS: I want to populate the UITextView on the second view controller with all the users information they entered into the UITextField when they press the button.
IMAGE OF LAYOUT
The code I used to populate the UITextView with 'Name' is:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier]isEqualToString:#"mySegue"]) //will return true so will start segue
{
ViewControllerTwo *ViewControllerTwo = [segue destinationViewController];
ViewControllerTwo.Name = sendName.text;
}
}
I'm sure to send the rest of the form over is simple but right now I just can't think of a solution.
Create more properties for the information in ViewControllerTwo.h. For example if you wanted to add the users age.
#property (nonatomic, strong) int age;
Set the value of the age in your prepareForSegue Method from the text the user enters into the field.
ViewControllerTwo.age = [ageTextField.text intValue];
In ViewControllerTwo.m build a string with all these properties and set the UITextView text equal to entire string.
NSString *userInformation = [[NSString stringWithFormat:#"%# \n %d", name, age];
Set your UITextView.text property equal to userInformation

passing data from uiview to tabview

I have a variable 'userID' that is getting populated after validating through a parser. Now when I will press the login button, the next view will be a tabView (Consisting of 5 screens). Now I want this userID to be fixed for UILabel in all tabViews. How to do this ?
Thanks in advance.
You can store it into NSUserDefaults using:
[[NSUserDefaults standardUserDefaults]setObject:userID forKey:#"USERID"];
and you can get its value anywhere using:
[[NSUserDefaults standardUserDefaults]objectForKey:#"USERID"]]

Passing text from UITextView to string in different ViewController

I need to pass the text of a UITextView in viewController1 to viewController2 and set the text to a string in viewController2. I thought I had the code figured out, but it turns out that nothing is being passed.
In viewController2.h I added
viewController1*v1
and assigned a property to it.
in the viewController2.m
-(void)method{
v1=[[viewController1 alloc] initWithNibName:#"viewController1" bundle:nil];
NSString *text=[[NSString alloc]init];
[v1.tweetText setText:text];
}
This is probably the wrong way to go about it, so does anyone have other solutions to this problem?
Thats the correct way to send data to another vc, but you are not setting the variable text to anything. Try setting it to hello and see if you get hello as the tweetText in the other vc.

Getting value of a button label from a different ViewController

This seems to be quite trivial but somehow I can't get it to work. I have 2 Popover Controllers with a tableView as contents, and I have a UIViewController with some buttons.
I'm setting one of the button's title from the PopoverController's didSelectRoxAtIndexPath function. This works nicely. Now I would like to pass this button title to the second PopoverController but can't seem to get this to work; the string returns a NULL. Currently I'm using:
h file:
NSString *myString;
#property(nonatomic,retain)NSString *myString;
m file:
#synthesize myString;
in viewDidLoad:
MyAppViewController *appV = [[MyAppViewController alloc]initWithNibName:#"MyAppViewController" bundle:nil];
self.myString = appV.buttonOne.currenTitle;
Try setting setting this property before displaying the second UIPopoverController.

Pass data to UITextField between ViewControllers

I am stuck on passing data from one ViewController to another. The scenario is as:
I have 2 ViewControllers named : SearchDomainController and LoginViewController. What i am trying to do is pass the string value from SearchDomainController to the UITExtfield in LoginViewController.
In LoginViewController i have declared IBOutlet UITextField *domainField; and also a property #property(nonatomic,retain) UITextField *domainField.
The problem is when i create a new instance of LoginViewController in SearchDomainController and try _loginViewController.domainField.text = #"Some text";
the text never changes in UItextField on LoginViewController.
What did i miss ? And what are the best solution for this kind of problem? Thanx
My guess would be that _loginViewController.domainField is nil at that time, which is probably because the view hasn't loaded yet, and the label is created when the view loads (via a nib) and not as soon as the view controller object is created.
In order to not depend on having the view fully loaded when passing the value, I would have used a separate property for passing along the title, i.e. _loginViewController.domainFieldText = #"Some text";. Then in viewDidLoad of the _loginViewController, assign the value of domaonFieldText to the actual label.
Alternatively, make sure the UILabel instance is created and not nil when you set its text from the other view controller.
Set the string as a property of the AppDelegate in one view controller, and you can retrieve it from another, and set it as textField.text