【问题标题】:change the text to uibutton in iOS在iOS中将文本更改为uibutton
【发布时间】:2013-07-18 05:50:22
【问题描述】:

我在 iOS 中将文本更改为 uibutton 时出错

@interface ViewControllerSonidos : UIViewController{
    __weak IBOutlet UIButton *initgame;
}
@property (weak, nonatomic) IBOutlet UIButton *initgame;
- (IBAction)Reproducir:(id)sender;

我的实现文件是这样的:

@implementation ViewControllerSonidos
@synthesize initgame

- (IBAction)Reproducir:(id)sender {
    [initgame setTitle:@"Continue" forState:UIControlStateNormal];
}

但问题是文本永远不会改变,有人看有什么问题吗? 提前致谢!

【问题讨论】:

  • 您确定 UIButton 已连接到 IBOutlet?
  • 你检查过这个方法是否真的被调用了吗?
  • 它为我工作。你能发布整个代码
  • 是的,UIButton是连接的,被tape的时候方法执行其他代码
  • 你能同时显示xib和.h吗

标签: ios objective-c uibutton uilabel


【解决方案1】:

你有很多方法可以做到这一点。

  • 您只需将其与 IBOutlet 连接即可,
  • 您可以将其与 id sender 一起使用
  • 或者您可以只使用简单的 IBAction 签名

例子:

 // using the id sender method
 - (IBAction) buttonTitleChange:(id)sender {
    [sender setTitle: @"Title" forState:UIControlStateNormal];
 }

 // using the connection one
 - (IBAction) buttonTitleChange: (id)sender {
    //AFTER CONNECTING THE BUTTON IN .XIB
    [buttonName setTitle:@"Title" forState:UIControlStateNormal];
 }

 // and the regular void method....also requiring the linkage of the .xib
 - (IBAction) buttonTitleChange {
    [buttonName setTitle:@"Title" forState:UIControlStateNormal];
 }

只要记住为他们都连接 .xib 中的按钮

使用 id sender,只是说您可以将它用于任何类型的对象。带connection的id sender只是一种写法,正则法只是一种实践方式。

这完全取决于您的编程方式。

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    试试这个代码:

    - (IBAction)Reproducir:(id)sender {
        [sender setTitle:@"Continue" forState:UIControlStateNormal];
    }
    

    【讨论】:

    • 谢谢,但我尝试了这段代码,但结果是一样的,文本没有改变
    • 你在xib中连接Reproducir的方法吗?
    【解决方案3】:

    IBOutlet 可能有问题,你忘记在 XIB 中连接它,否则它可以正常工作。

    【讨论】:

      【解决方案4】:
      -(void)btnChanged
      {
          [m_btnSample setTitle:@"Continue" forState:UIControlStateNormal];
      
      }
      

      【讨论】:

      • 我想你需要检查插座是否连接正确。如果没有,请尝试以编程方式创建一个按钮,然后检查是否调用了 btnAction。
      【解决方案5】:

      是的,请检查 IBOutlet 连接,因为相同的代码对我有用。

      【讨论】:

        【解决方案6】:

        在 ViewController.h 中

        #import <UIKit/UIKit.h>
        
        @interface ViewController : UIViewController
        - (IBAction)initgame:(id)sender;
        @property (retain, nonatomic) IBOutlet UIButton *initgame;
        @end
        

        在 ViewController.m 中

        - (IBAction)initgame:(id)sender {
        [self.initgame setTitle:@"Continue" forState:UIControlStateNormal];   
        }
        

        不要忘记将 .xib 中的按钮附加到属性和 IBAction。

        如果它现在不起作用,只需删除 Button 和 IBAction/Property 之间的连接,为其创建新的 IBAction 和属性,它将适用于上述代码。

        【讨论】:

          猜你喜欢
          • 2012-07-10
          • 2017-10-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-10-01
          • 1970-01-01
          相关资源
          最近更新 更多