0

I have two controller A and B

In B, I have a UISwitch IBOutlet.

when I use Segue from A,

switch.on = YES does not work.

If I declare another property in B , say switchvalue,

@property (strong, nonatomic) NSNumber* switchvalue;

I can pass the value to switchvalue.

Anyone has some clue? It is tedious that I cannot set property directly on controls.

Adam Lee
  • 24,710
  • 51
  • 156
  • 236
  • IBOutlets are not set in time the segue is performed. So if you want to pass some information, you need to use an interim property and then set the outlet in viewDidLoad or viewWillAppear methods. – Martin Koles May 25 '14 at 12:50
  • http://stackoverflow.com/a/23403979/294884 – Fattie May 05 '16 at 16:27

1 Answers1

1

It is impossible to set the value of an IBOutlet in prepareForSegue: because the outlets have not been created when this method is invoked. If you debug your code, you will see that the UISwitch value is nil when prepareForSegue is called.

As an alternative to setting an NSNumber property, you can try this

Community
  • 1
  • 1
gsach
  • 5,715
  • 7
  • 27
  • 42
  • I add the normal property; when I debug, I also see that normal property is nil as well; but the value is passed to that normal property. – Adam Lee May 25 '14 at 13:00
  • The string is nil because it does not have a value. You can set a value though. In the case of the IBOutlet you are not trying to set a value to the outlet but to a property of it. E.g you can do controller.string = @"a" but not controller.label.text = @"a" because label is nil. So the text property cannot be changed. Thats the difference – gsach May 25 '14 at 13:02