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 9128341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:29:31+00:00 2026-06-17T07:29:31+00:00

I am trying to implement a custom implementation of switching buttons so that only

  • 0

I am trying to implement a custom implementation of switching buttons so that only one can be selected at a time. I have run into a weird error where I init an object using [[ajdSwitchButton alloc] init]. In the init I set a class property as follows self.currentSelection = 2.

The issue is that between the init and the first call to an IBAction method, the value is changed to 0. I cannot figure out why. Here is the relevant code:

ajdSwitchButton.h

#import <UIKit/UIKit.h>

@interface ajdSwitchButton : UIView

@property (nonatomic) NSInteger currentSelection;

// Button Outlets
@property (nonatomic, strong) IBOutlet UIButton *buttonOne;
@property (nonatomic, strong) IBOutlet UIButton *buttonTwo;

// Button Actions
- (IBAction)buttonPress:(id)sender;
- (IBAction)buttonPressTwo:(id)sender;

// Instance Methods
- (void)switchButtonState:(UIButton *)button;

@end

ajdSwitchButton.m

#import "ajdSwitchButton.h"

@implementation ajdSwitchButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _currentSelection = 2;
    }
    return self;
}

// Handles button press actions for buttonOne
- (IBAction)buttonPress:(id)sender {
    NSLog(@"%@", self);
    // Y button pressed
    if (self.currentSelection == 2) {
        // No button is selected
        // Highlight and select buttonOne
        [self performSelector:@selector(switchButtonState:) withObject:self.buttonOne afterDelay:0.0];
    } else if (self.currentSelection == 1) {
        // No was previously selected
        // Unselect NO and select YES
        [self performSelector:@selector(switchButtonState:) withObject:self.buttonTwo afterDelay:0.0];
        [self performSelector:@selector(switchButtonState:) withObject:self.buttonOne afterDelay:0.0];
    } else {
    // Y button already pressed
    [self performSelector:@selector(switchButtonState:) withObject:self.buttonOne afterDelay:0.0];
    }
    self.currentSelection = 0;
}

- (IBAction)buttonPressTwo:(id)sender {
    // N button pressed
    NSLog(@"%i", self.currentSelection);
    if (self.currentSelection == 2) {
        // No button is selected
        // Highlight and select buttonOne
    [self performSelector:@selector(switchButtonState:) withObject:self.buttonTwo afterDelay:0.0];
} else if (self.currentSelection == 0) {
        // Yes was previously selected
        // Unselect YES and select NO
        [self performSelector:@selector(switchButtonState:) withObject:self.buttonTwo afterDelay:0.0];
        [self performSelector:@selector(switchButtonState:) withObject:self.buttonOne afterDelay:0.0];
    } else {
        // N button already pressed
        [self performSelector:@selector(switchButtonState:) withObject:self.buttonTwo afterDelay:0.0];
    }
    self.currentSelection = 1;
}

// Switches the look and state of the button
- (void)switchButtonState:(UIButton *)button {
    if (!button.selected) {
        button.highlighted = YES;
        button.selected = YES;
    } else {
        button.highlighted = NO;
        button.selected = NO;
    }
}
@end

I link an instance of ajdSwitchButton to an IBOutlet view within ViewController. Any help would be greatly appreciated.

EDIT: I pasted some NSLogs to check the memory value of the object directly after the init and as soon as the IBAction method is called. Here is the before and after:

<ajdSwitchButton: 0x746d2e0; frame = (0 0; 0 0); layer = <CALayer: 0x7472c40>>

<ajdSwitchButton: 0x7471b10; frame = (77 232; 180 83); autoresize = TM+BM; layer = <CALayer: 0x7471bf0>>

  • 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-17T07:29:32+00:00Added an answer on June 17, 2026 at 7:29 am

    IBOutlets init through initWithCoder:(NSCoder)aDecoder, so you need to implement this method.

    - (id)initWithCoder:(NSCoder *)aDecoder
    {
        self = [super initWithCoder:aDecoder];
        if (self) {
            // Initialization code
            _currentSelection = 2;
        }
        return self;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement some custom model metadata in ASP.NET MVC 3. I can't
I'm trying to implement a custom validation annotation in Seam. We have a list
I'm trying to implement a custom combobox options popup, so that near each option
I am trying to learn Objective-C and have run into a problem with pushing
I am trying to implement a custom profile class in C# that inherits System.Web.Profile.ProfileBase.
I'm trying to implement a custom TrackingParticipant for WF 4. I can write the
I am currently trying to implement a Custom SiteMap Provider. I have read several
It's the first time I'm trying to implement a custom STL compatible container with
I was trying to implement custom validation attribute by inheriting from ValidationAttribute and found
im trying to implement a custom error page, what i want to be able

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.