10

I have created a custom UIView and I would like to initialize it with custom parameters from the UIStoryboard. Basically I have added a UIView in the UIStoryboard and I have changed the class to my custom UIView.

In my custom UIView class I'm using the initWithCoder method to initialize it but I would like to use the provided NSCoder to get some configuration parameters from the UIStoryboard, is this possible or is there another way to pass parameters from the UIStoryboard to my custom UIView?

Thanks!!

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
rdiaz82
  • 996
  • 12
  • 31
  • Did you tried to make it using your own constructor. Like initWithCoder with parameters in it. Creating a new constructor in your custom class which over rides this method.Else if you provide some code into it it would be great to understand the problem. – Harjot Singh Sep 03 '13 at 09:33
  • According to my answer, that solution doesn't work because the initWithCoder method is responding to a specific protocol and the system will call that method and it doesn't expect any additional parameter. For that reason I would like to add my custom parameters in the NSCoder, because I could decode them inside the initwithCode without problems. This solution in theory is pretty simple but I have no idea if it's possible to add parameters to the NSCoder from the Storyboard. – rdiaz82 Sep 03 '13 at 09:36
  • Can't we go with a custom method for passing the parameters. Like [view myMethodCalled]. – Harjot Singh Sep 03 '13 at 09:39
  • Yes! I can do my custom init from my viewcontroller using an IBOutlet of my custom UIView class but I prefer do all the initialization from the storyboard. I suppose that I will have to proceed using this approach because I feel that is not possible to do what I want. – rdiaz82 Sep 03 '13 at 09:43
  • Else the other way you can do if there is one ID kind of parameter, then use the NSUSerDefaults and remove them after the view initialization... You can go for that way as well – Harjot Singh Sep 03 '13 at 09:47
  • I have found a workaround, take a look to my answer. What do you think about it? – rdiaz82 Sep 03 '13 at 10:03
  • crreate and add programatically, you will hav the refernece there and you can pass the value – Lithu T.V Sep 03 '13 at 10:26
  • @rdiaz82 Which xcode are you using? In latest xcode 5, I have added UIView by drag & drop to storyboard separately, but i did not see that view. How can i see that view? – Sunil Zalavadiya Sep 21 '13 at 10:48
  • @sunilz I have been using xCode 4, but I think that my solution is valid in the new xCode 5 as well. – rdiaz82 Sep 21 '13 at 23:44

1 Answers1

3

Apparently is not possible to add parameters to NSCoder using the initwithCoder:

Initializing a view with custom initWithCoder

I have found a workaround to solve the problem, simply using the User Defined Runtime Attributes doing the following:

1) Add the custom properties in the storyboard in the "User Defined Runtime Attributes".

2) Add the same properties to my custom class.

3) Use the properties to customize the UIView in the awakeFromNib method.

With this solution, I'm able to initialize my custom UIView from the storyboard and just use it from my controller.

Community
  • 1
  • 1
rdiaz82
  • 996
  • 12
  • 31
  • Could you show a code example with storyboard screenshots? And what if I want to set custom class object as the parameter? The storyboard only has Foundation data structures. – 7to4 Sep 26 '19 at 08:24