ASCII按钮标题中字符串的值

时间:2012-08-13 12:25:44

标签: iphone uibutton

我有三个按钮,除了标题以外,所有属性都相同。标题分别是'A','B'和'C'。我写了一个方法,我已经编写了这三个按钮的所有常见属性,并调用此方法,我想在视图上添加这些按钮。我做了所有的论文,但按钮标题结构。我用循环来添加这些按钮来查看。 这是我的代码:

这是具有三个按钮的所有常见属性的方法。

-(UIButton *)setMiddlePlayButton{
    UIButton * middleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [middleButton setBackgroundImage:[UIImage imageNamed:@"Middle"] forState:UIControlStateNormal];
    [middleButton addTarget:self action:@selector(playButtonPressed:)
           forControlEvents:UIControlEventTouchDown];
    middleButton.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    return middleButton;
}

以这种方式调用方法在视图上添加按钮。

for(int i= 0; i<3; i++){
        UIButton * middleButtons = [self setMiddlePlayButton];
        middleButtons.tag= 2+i;
        //Here I have to add button Title which is 'A' for first button with tag 2, 'B' for second button with tag 3 and 'C' for third button. 
        [self.view addSubView:middleButtons];

    }

我没有找到获得标题的方法,是否有可能使用ASCII值?或者还有其他方法吗?请帮忙。

1 个答案:

答案 0 :(得分:0)

试试此代码

for(int i= 0; i<3; i++){
    UIButton * middleButtons = [self setMiddlePlayButton];
    middleButtons.tag= 2+i;
    [middleButtons setTitle:[NSString stringWithFormat:@"%c", 65+i] forState:UIControlStateNormal];
    [self.view addSubview:middleButtons];   
}