【问题标题】:how to change the button text programmaticaly in iphone如何在iphone中以编程方式更改按钮文本
【发布时间】:2011-02-16 07:20:08
【问题描述】:

我是 iphone 开发的新手。在这里,我在 pickerview 中添加了一些名称列表。pickerview 的操作被赋予视图中的按钮。现在我想显示。当单击按钮pickerview列表显示时,我在pickerview中选择了一个名称,该名称显示在视图中的按钮上。我不知道如何更改 iPhone 中按钮的标题。

谁能给我关于我的问题的信息。

提前谢谢你。

【问题讨论】:

    标签: iphone


    【解决方案1】:

    您应该能够通过调用 setTitle 来更改 UIButton 的标题。请记住为 IUIButton 的所有状态设置它。这是一个例子:

    [button setTitle:@"Normal Title" forState:UIControlStateNormal];
    [button setTitle:@"Highlighted Title" forState:UIControlStateHighlighted];
    

    【讨论】:

    • 你不必为所有状态都设置它,如果你只设置UIControlStateNormal,它将被用于所有状态。
    【解决方案2】:

    在 .h 文件中声明您的按钮并为您的按钮设置属性

    UIButton *btnl;
    
    @property(nonatomic,retain) IBOutlet UIButton *btnl;
    

    并从 IB 建立连接。

    现在在 .m 文件中,

    使用你选择器的这个委托方法

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
       btn.text=[yourArrayOfPickerContent objectAtIndex:row];
    }
    

    【讨论】:

      【解决方案3】:

      要更改自定义按钮标题,请使用:

      UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
      button.frame = CGRectMake(82,203,129,45);
      
      [button setTitle:@"btnTitle" forState:UIControlStateNormal];
      [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
      
      [self.view addSubview:button];
      [self.view bringSubviewToFront:button];
      

      【讨论】:

        【解决方案4】:

        要更改按钮标题,

          button.titleLabel.text=@"String";
        

        你应该在pickerView委托中编码

        - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

        【讨论】:

          【解决方案5】:

          这样您就可以编写有关 UIButton 的代码并为其设置标题。

          UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];                               
          [btn setFrame:CGRectMake(0.0f, 4.0f, 90.0f, 20.0f)];
          [btn setBackgroundColor:[UIColor clearColor]];                              
          UIImage *backgroundView = [UIImage imageNamed:@"btn.png"];
          [btn setBackgroundImage:backgroundView forState:UIControlStateNormal];
          [btn setTitle:@"buttonName" forState:UIControlStateNormal];
          [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];          
          

          【讨论】:

            【解决方案6】:

            首先,您需要在视图控制器中创建UIButton 类型的指针作为IBOutlet,然后将该按钮连接到IB 中的那个。

            当你想更改标题的名称时,只需这样做

            button.titleLabel.text = [NSString stringWithString:yourString];

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2012-11-19
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-06-06
              相关资源
              最近更新 更多