0

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?

E-Riddie
  • 14,660
  • 7
  • 52
  • 74
  • 1
    Do you want to pass the `UILabel` itself, or the text shown *in* the label? – Undo Jul 07 '14 at 14:32
  • Both I think? I want to pass the Uilabel – user3344457 Jul 07 '14 at 14:34
  • I don't see difference between UITextField and UILabel, at least provide some code to see where you have this issue – E-Riddie Jul 07 '14 at 14:34
  • So you're using `NSUserDefaults` to pass values between view controllers? – trojanfoe Jul 07 '14 at 14:34
  • why do you want to pass a `UILabel` to the `NSUserDefault`? please post some code about what you are doing. – holex Jul 07 '14 at 14:35
  • You can't pass a `UILabel`, but you can store the content of the `label.text` property. But maybe it is better to not use `NSUserDefaults` for data passing bewteen controllers... – mxb Jul 07 '14 at 14:37
  • I'm not home right know I'm with my phone when I get home I will post my code. – user3344457 Jul 07 '14 at 14:48
  • You should tell us how you instantiate each view controller, what the sequence of events in the program is, and how your storyboard is setup. You actually need advice as to how to structure a typical iOS GUI app. Speaking from experience, you want to try to do things as close as you can to the way that Apple intended, or you end up doing a lot more work in the long run. – Colin Jul 07 '14 at 15:31

1 Answers1

1

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:

Community
  • 1
  • 1
E-Riddie
  • 14,660
  • 7
  • 52
  • 74