2

Similar to Refactoring 3 different UI components into one UIView into a CocoaPod but I think maybe I'm doing the core refactoring wrong.

So, I have a 3 UIView elements that I am consistently reusing:

  • label
  • input
  • check mark

There is some validation and other logic that I want to refactor as well (as in stay with the new class so the logic is also reused).

So, do I want to start off with a UIViewController sub class and add that everywhere I used these element or a UIView?

I don't see how UIVew should/could have the validation logic required. So, then I would need to have a UIViewController. But this doesn't make a lot of sense to me, since you have UIView objects on in your views, not UIVIewController objects.

Thoughts?

EDIT:

  • What validations?
  • basically there is a label, input and check box.
  • when the user enters text the label changes colour and the check mark changes state depending on validation (I was actually going to do validation delegate)
  • when the user is done editing and all good, check mark is green & label changes colour again.
Community
  • 1
  • 1
cbrulak
  • 15,436
  • 20
  • 61
  • 101

1 Answers1

0

I touched on this in my latest blog post.

There is very little information in your question to provide a detailed answer but you could do it several ways.

You need to create a UIView subclass and add subviews to it for the label, input and check mark and provide these as properties so you can do something like...

myReusableView.label.text = @"Hello world";

or

[myReusableView.input becomeFirstResponder];

Then you need to actually add these subviews. This can be done in code...

- (instancetype)initWithFrame:(CGRect)frame
{
    ...
    self.label = [UILabel new];
    self.label.frame = //blah
    [self addSubview:self.label];
    ...
}

Or you can do this in a nib and attach the outlets etc...

There is nothing wrong with putting a small amount of validation in there. This happens a lot of the time. For instance if you try to set a progressView value to greater than 1 then it will catch it and set it to 1 and so on.

It also depends on what sort of validation you are doing. You could always put it in a viewController and use it as a child view controller (I personally wouldn't do this though).

You could provide delegate methods to "outsource" the validation to somewhere else.

What is the validation that you are doing?

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • @cbrulak for that validation put the "logic" into the view. The validation relates to the view and affects the view so it's perfectly acceptable to put that logic into the UIView subclass – Fogmeister Mar 17 '15 at 14:50
  • @cbrulak what you could do is use a delegate to change the validation. Have a standard validation that runs if there is no delegate and change it by supplying a delegate. If you want to make it more flexible. – Fogmeister Mar 17 '15 at 14:51