1

In my app I have a gradient as a background. This gradient is made programmatically. The way that I now use this is like this:

I have a UIViewController which needs to display the gradient and in that class I do this :

- (void) viewWillAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [Gradient gradientInViewController:self];
}

This ain't so bad but this needs to be done in all the classes which isn't very good programming. What I want is instead of making a class a UIViewController, I want it to be a GradientViewController which is a subclass of UIViewController and in this class I will handle everything.

So my question is how do I do this? I think this has to be done through categories? But I can't figure out how to get the image on the screen. Should this be done in viewWillAppear or something?

2
  • simply change [super viewDidAppear:animated]; to [super viewDidAppear:animated]; Commented Oct 31, 2013 at 8:27
  • @NANNAV not sure what the difference is between them did you mean to say [super viewWillAppea:animated]; Commented Oct 31, 2013 at 8:31

4 Answers 4

2

Make a GradientViewController which handles the gradient drawing

@interface GradientViewController : UIViewController

@end

@implementation GradientViewController

- (void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [Gradient gradientInViewController:self];
}

@end

Then inherit all your other controllers from that

@interface YourViewController : GradientViewController

@end

@implementation YourViewController

- (void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    // no need to do anything
}

@end

It's more flexible to do it through helper classes or categories though, even if there's a bit of repetition.

Note as pointed out by Nguyen Duc, that you are calling [super viewDidAppear:] for viewWillAppear: which is wrong, I edited the answer.

Sign up to request clarification or add additional context in comments.

1 Comment

I will give you the best answer since it gives an entire explanation! Thank you
1

Why not use Interface Builder and have a xib that knows how to load your image for you automatically?

Comments

0

You can get this behaviour "for free" throughout your app simply by subclassing UIViewController:-

@interface GradientViewController : UIViewController 
@end

and implementing your gradient code in viewDidLoad: as such:-

- (void)viewDidLoad
{
    [super viewDidLoad];
    [Gradient gradientInViewController:self];
}

Then just use GradientViewController instead of UIViewController everywhere you need your gradient.

Alternatively you can use a category. This can be useful because you can use categories for "themes" for your app, configuring various UI elements of your UIViewControllers. But I'd set up a category on UIViewController specifically to set your gradient, and then call the category method in every view controller that needs it - do not attempt to do it by overriding viewDidLoad: or viewWillAppear: in a category.

Comments

0

Just create a new view controller (let's call it GradientViewController for example), put the same code that draws the gradient into its view viewWillAppear, then make all your view controllers a subclass of the view controller by replacing

@interface SomeViewController : UIViewController

with

@interface SomeViewController : GradientViewController

1 Comment

Thank you! Just realized I completely overthought this!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.