【问题标题】:Tapping Dynamically created UIButtons Crash App点击动态创建的 UIButtons Crash App
【发布时间】:2013-09-13 13:14:22
【问题描述】:

我有一种制作按钮的方法。参数是文本颜色位置 x 位置 y 按钮类型和方法(注意:此方法在一个类中)

-(UIButton *)makeButton :(NSString *)string :(UIColor*)textColor  :(CGFloat)posx :(CGFloat)posy :(UIButtonType)type :(SEL)run
{
    UIButton *button =[UIButton buttonWithType:type];
    [button addTarget:self action:run forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:string forState:UIControlStateNormal];
    button.titleLabel.font =[UIFont systemFontOfSize:11];
    [button setTitleColor:textColor forState:UIControlStateNormal];
    CGSize stringSize =[string sizeWithFont:[UIFont systemFontOfSize:11]];
    [button setFrame:CGRectMake(posx, posy, stringSize.width+10, stringSize.height+5)];

    return button;
}

我正在调用这样的方法。

 UIButton *btnGidenler=  [methods makeButton:anlik :[methods RGBR:0 G:135 B:222 A:1.0] :10 :5:(UIButtonType)UIButtonTypeCustom :@selector(gidenKisiler)];
            [viewGidenler addSubview:btnGidenler];

和@selector(btnGidenler)

基本方法-(void)gidenKisiler{ //just alert }

当我点击按钮时,我的应用程序崩溃了。为什么选择器不起作用?

粉碎错误

@autoreleasepool { 返回 UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); }

【问题讨论】:

    标签: ios uibutton


    【解决方案1】:

    我找到了.. 我在我的方法中添加了目标选择器,我认为选择器和方法必须在同一个地方,所以我将 addTarget 部分移到返回按钮下我的意思是对我的英语感到抱歉

    UIButton *btnGidenler=  [methods makeButton:anlik :[methods RGBR:0 G:135 B:222 A:1.0] :10 :5:(UIButtonType)UIButtonTypeCustom :@selector(gidenKisiler)];
    [button addTarget:self action:run forControlEvents:UIControlEventTouchUpInside];
                [viewGidenler addSubview:btnGidenler];
    

    它有效!

    【讨论】:

    • 我的立场是正确的,你的功能是有效的。就个人而言,这只是糟糕的语法。但如果它有效,它就有效。
    猜你喜欢
    • 1970-01-01
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-26
    • 2021-11-30
    • 2016-11-27
    相关资源
    最近更新 更多