Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working with storyboard #1

Open
VasanthkumarVelusamy opened this issue Jun 2, 2018 · 2 comments
Open

Not working with storyboard #1

VasanthkumarVelusamy opened this issue Jun 2, 2018 · 2 comments

Comments

@VasanthkumarVelusamy
Copy link

VasanthkumarVelusamy commented Jun 2, 2018

Badge properties are not added when using with segmented control in storyboard. Only the badge text appears without any circle background.

@johncpang
Copy link

johncpang commented Jan 6, 2019

When used with Storyboard, M2DSegmentedControl:initWithItems: is never called. So, the badgeViews array is nil. To resolve, we must recall those missing codes (as following):

self.badgeViews = [NSMutableDictionary new];
[self addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
[self setBadgeBackgroundColor:[UIColor redColor]];
[self setBadgeTextColor:[UIColor whiteColor]];

The last two is straight forward as they are public functions. But the first two are tricky because they are private property/method. So, we need to redeclare them as public (Credit: http://jerrymarino.com/2014/01/31/objective-c-private-instance-variable-access.html).

@interface M2DSegmentedControl()

@property (nonatomic, strong) NSMutableDictionary<NSNumber *, M2DSegmentedControlBadgeView *> *badgeViews;
- (void)valueChanged:(id)sender;

@end

// declare a short cut to the `valueChanged:` as this:
- (void)M2DSegmentedControlValueChanged:(id)sender {
	[(M2DSegmentedControl *)sender valueChanged:sender];
}

Now, redo the missing initialization and call this function from viewDidLoad:

- (void)reinitM2DSegmentedControl:(M2DSegmentedControl *)segmentedControl {
	segmentedControl.badgeViews = [NSMutableDictionary new];
	[segmentedControl addTarget:self action:@selector(M2DSegmentedControlValueChanged:) forControlEvents:UIControlEventValueChanged];
	[segmentedControl setBadgeBackgroundColor:[UIColor redColor]];
	[segmentedControl setBadgeTextColor:[UIColor whiteColor]];
}

@johncpang
Copy link

After using it for a while, I found that this library is quite buggy. For example, if I set a badge with nil text (no value), it will be hidden forever. I have to access the badgeView (via the above hack to the supposed private array) to unhidden it before updating the text.

When the viewController is disappears (for example, a navigation controller event), on return, the badges are gone/hidden/invisible. Can't find a way to show them programatically. But user tapping any other tab on the segment control (any except current one), the badges will come back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants