Atilla Jax Atilla Jax - 4 months ago 20
Objective-C Question

Mixpanel iOS - How to MPTweakBind a UIButton text?

Using the MixPanel iOS SDK I want to use the

MPTweakBind
on a UIButton but am finding it hard to apply it on a UIButton because its title properties are read-only.

I've tried the following without success;

This complains of NSString from incompatible void

UIButton *submitBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 30, 30)];
MPTweakBind(submitBtn, titleLabel.text, @"Button Text");


This complains of NSString from incompatible void

MPTweakBind(submitBtn.titleLabel, text, @"Button Text");


I'm sadly stuck at this point, how do I apply the MPTweakBind onto a UIButton?

Many thanks

Answer

You can try create you own subclass from UIButton with some property for example NSString *textForTweakBind. In setter you should write:

[self setTitle: @"myTitle" forState: UIControlStateNormal];

//.h file

//  MyButton.h

#import <UIKit/UIKit.h>

@interface MyButton : UIButton

@property (nonatomic, strong) NSString *textForTweakBind;

@end

//implentation file

@implementation MyButton

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)setTextForTweakBind:(NSString *)textForTweakBind {
    [self setTitle:textForTweakBind forState:UIControlStateNormal];
}

@end

Than you can try to use MPTweakBind with this button:

MyButton *submitBtn = [[MyButton alloc] initWithFrame:CGRectMake(20, 20, 30, 30)];
MPTweakBind(submitBtn, textForTweakBind, @"Button Text", @"Button Text 2");

I hope it will helps)