Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8284237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:02:01+00:00 2026-06-08T11:02:01+00:00

Here is the situation. I have a view controller titled MyViewController. Within this view

  • 0

Here is the situation. I have a view controller titled “MyViewController.” Within this view controller I have a text editing feature that uses subclassed buttons. The name of the UIButton Subclass is “ColorSwatch”

I have setup delegate/protocol methods in the “ColorSwatch.h” subclass as follow.

//  ColorSwatch.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>

@protocol ColorSwatchDelegate <NSObject>
- (void)fontColor:(UIColor *)color;
@end

@interface ColorSwatch : UIButton {
id <ColorSwatchDelegate> colorSwatchDelegate;
    CAGradientLayer *gradient;
    UIView *currentView;
    UIColor *fontColor;
}

@property (nonatomic, retain) id <ColorSwatchDelegate> colorSwatchDelegate;
@property (nonatomic, retain) CAGradientLayer *gradient; 
@property (nonatomic, retain) UIView *currentView;
@property (nonatomic, retain) UIColor *fontColor;

@end

Now in my “ColorSwatch.m” I have:

//  ColorSwatch.m

#import "ColorSwatch.h"
#import <QuartzCore/QuartzCore.h>
#import "MyViewController.h"

@implementation ColorSwatch

@synthesize gradient; 
@synthesize currentView;
@synthesize colorSwatchDelegate;
@synthesize fontColor;

-(void)setupView{
    "Makes the subclassed buttons pretty"
}

-(id)initWithFrame:(CGRect)frame{
    if((self = [super initWithFrame:frame])){

    }

    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder{
    if((self = [super initWithCoder:aDecoder])){
        [self setupView];
        MyViewController *mvc = [[MyViewController alloc] initWithNibName:
                                           @"MyViewController" bundle:nil];
        self.colorSwatchDelegate = mvc;
    }

    return self;
}


-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [self magnify:view];
    fontColor = view.backgroundColor;
    [self.colorSwatchDelegate fontColor:fontColor];
}

- (void)magnify:(UIView *)view
{

}

- (void)dealloc
{
    [currentView release];
    [gradient release];
    [fontColor release];

    [super dealloc];
}

@end

In the “MyViewController.h” I have:

//  MyViewController.h

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "ColorSwatch.h"


@interface MyViewController : UIViewController <ColorSwatchDelegate> {    

UITextField *photoLabelTextField;

}

@property (nonatomic, retain) IBOutlet UITextField *photoLabelTextField;

@end

In the “MyViewController.m” I have:

- (void)fontColor:(UIColor *)color
{
    NSLog(@"Selected Font Color");
    [self.photoLabelTextField setTextColor:color];
}

Now the delegate method sort of works, meaning when I tap on a color button the

NSLog(@"Selected Font Color");

message gets fired. But the problem is that I cannot change the

[self.photoLabelTextField setTextColor:color];

property. I have tried numerous ways of changing the property, the only thing that I can do is send NSLogs, anything I try to change a property in the “MyViewController” Class nothing happens.

If anyone could please help me out, I would appreciate it.

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:02:03+00:00Added an answer on June 8, 2026 at 11:02 am

    The problem is that the ColorSwatch is sending delegate messages to a dangling instance of MyViewController that it incorrectly allocated in it’s initWithCoder: method.

    UIControls shouldn’t allocate ViewControllers to be their delegates… it goes the other way around.

    Delete these lines…

    // in ColorSwatch.m initWithCoder:
    MyViewController *mvc = [[MyViewController alloc] initWithNibName:
                                               @"MyViewController" bundle:nil];
    self.colorSwatchDelegate = mvc;
    

    Then, in MyViewController.m …

    - (void)viewDidLoad {
    
        ColorSwatch *colorSwatchButton = [[ColorSwatch alloc] buttonWithType:UIButtonTypeCustom];
    
        // or place a ColorSwatch in the xib, on MyViewController's view... But not before you
        // you delete lines from initWithCoder, otherwise it's infinite circular allocation
    
        colorSwatchButton.frame = CGRectMake(/* ... */);
        colorSwatchButton addTarget:self action:@selector(csButtonPressed:) forControlEvent: UIControlEventTouchUpInside];
        // and so on...
        // now the important part:
        colorSwatchButton.colorSwatchDelegate = self;
    
        // see - the ViewController is in charge of allocation, sets itself up as the delegate
        [self.view addSubview:colorSwatchButton];
    }
    

    Instead of building the button in code, you can use IB.

    Step 1: make the delegate an outlet…

    @property (nonatomic, retain) IBOutlet id <ColorSwatchDelegate> colorSwatchDelegate;
    

    Step 2: draw the buttons in IB, and set their class to ColorSwatch.
    Then you can skip the code I wrote in viewDidLoad.

    Step 3: The newly placed button should now present an outlet in IB. You can drag from that to the MyViewController as you normally do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, so here's the situation. I currently have a view controller called MainViewController which
I have a strange situation in one view controller where this line crashes in
Here is my situation: I have one table that contains a list of drugs
Here's the situation: I have a two images that are over 1024 in width
I have an situation here that looks very like a SELECT N+1 from the
I have a problem here with cakePHP. I have 4 .ctp view pages that
Here is my current situation: I have a user class that has an attribute
Here is my situation: I have a controller action called OrderFromCategory which fills a
I have a view controller class called PresidentsViewController that sets up data in a
Here is my situation - I have two nested view models: <%=Html.EditorFor(x => x.DisplayEntitiesWithRadioboxesViewModel)%><br

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.