【问题标题】:UIButton title replacement with each press每次按下 UIButton 标题替换
【发布时间】:2013-06-13 18:36:38
【问题描述】:

我想在每次按下按钮时向用户展示来自 ABC 的不同字母。

所以当它第一次点击应用程序时,用户会看到字母 A,然后他们需要按下按钮,然后他们会看到字母 B,依此类推。

我的挑战是如何在每次单击按钮时替换标题文本。 我写了一些代码,两个函数,如果你仔细看看你会发现-(void)displayABC:(id)sender 每次都被调用所以每次我得到数组的第一个成员,所以用户第一次看到字母 A然后它按下按钮,她/他看到字母 B,它是数组的第一个成员,但是每个相同的字母都出现了,因为每次,我们加载 plist 文件,因此我们一直调用数组的第一个成员数组。

有什么办法解决吗?

-(void)createLoginBioButton
{
    authButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [authButton setBounds:CGRectMake(300,300, 150, 150)];
    [authButton setCenter:CGPointMake(150, 240)];
    [self.view addSubview:authButton];
    [authButton setEnabled:true];
    [authButton setTitle:@"A" forState:UIControlStateNormal];
    [authButton setFont:[UIFont systemFontOfSize:70]];
    [authButton addTarget:self
                   action:@selector(displayABC:) 
         forControlEvents:UIControlEventAllTouchEvents];
 }

-(void)displayABC:(id)sender
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"ABC" ofType:@"plist"];
    NSArray *ABCArray = [NSArray arrayWithContentsOfFile:path];
    for (NSString *ABCValues in ABCArray){
        [authButton setTitle:ABCValues forState:UIControlStateNormal];
    }
}

【问题讨论】:

    标签: objective-c cocoa-touch uibutton


    【解决方案1】:

    为什么不保留对当前字母索引的引用作为实例变量并在每次触摸时递增?

    伪代码

    @property NSArray *abcs;
    @property int currentIndex;
    
    - (void)viewDidLoad {
      [super viewDidLoad];
      self.abcs = //load it from the plist
      [self updateButtonText];
    }
    
    - (void)buttonTouched
    {
      self.currentIndex++;
    
      if ( self.currentIndex >= self.abcs.count ) {
        self.currentIndex = 0;
      }
      [self updateButtonText];
    }
    - (void)updateButtonText
    {
      [self.button setTitle:self.abcs[self.currentIndex] forControlState:UIControlStateNormal];
    }
    

    【讨论】:

      【解决方案2】:

      最简单的解决方案是放置一个保持跟踪索引的静态变量

      -(void)createLoginBioButton
        {
      
          authButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
          [authButton setBounds:CGRectMake(300,300, 150, 150)];
          [authButton setCenter:CGPointMake(150, 240)];
          [self.view addSubview:authButton];
          [authButton setEnabled:true];
          [authButton setTitle:@"A" forState:UIControlStateNormal];
          [authButton setFont:[UIFont systemFontOfSize:70]];
          [authButton addTarget:self action:@selector(displayABC:) forControlEvents:UIControlEventAllTouchEvents];
       }
      
      -(void)displayABC:(id)sender
      {
      
          static int index = 0;
          NSString *path = [[NSBundle mainBundle] pathForResource:@"ABC" ofType:@"plist"];
          NSArray *ABCArray = [NSArray arrayWithContentsOfFile:path];
          if(index < [ABCArray count])
              [authButton setTitle:[ABCArray objectAtIndex:index] forState:UIControlStateNormal];
          index++;
      
      
      }
      

      【讨论】:

      • Mihir 我也一样,它先是字母 A,然后是字母,但没有前进。
      • 您的索引计数正在增加?或者不是……你试过用上面的代码吗?
      猜你喜欢
      • 2012-02-16
      • 1970-01-01
      • 2017-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-19
      相关资源
      最近更新 更多