Mixpanel iOS - 如何 MPTweakBind UIButton 文本?

Mixpanel iOS - How to MPTweakBind a UIButton text?

使用 MixPanel iOS SDK 我想在 UIButton 上使用 MPTweakBind,但我发现很难将它应用到 UIButton,因为它的标题属性是 read-only。

我尝试了以下但没有成功;

这从不兼容的 void

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

这从不兼容的 void

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

我很遗憾地卡在了这一点上,我该如何将 MPTweakBind 应用到 UIButton 上?

非常感谢

您可以尝试使用一些 属性 从 UIButton 创建您自己的子类,例如 NSString *textForTweakBind。在 setter 你应该写:

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

//.h文件

//  MyButton.h

#import <UIKit/UIKit.h>

@interface MyButton : UIButton

@property (nonatomic, strong) NSString *textForTweakBind;

@end

//实现文件

@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

你可以尝试通过这个按钮使用 MPTweakBind:

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

希望对你有所帮助)