-3

我正在尝试制作一个应用程序,每次按下按钮时都会出现一个新的美国州,到目前为止我已经到了阿拉斯加并且不知道如何继续。这是我迄今为止生成的代码:

- (IBAction)button1:(id)sender {

if([hellolabel.text isEqualToString:@"Alabama"])
{

hellolabel.text = @"Alaska";

}

else

{

hellolabel.text = @"Alabama";

}

}
4

1 回答 1

1

将所有状态名称存储在一个数组中

  NSArray *states = @[@"Alabama",@"Alaska",...]

- (IBAction)button1:(id)sender 
 {
   NSString *state = [states objectAtIndex:arc4random_uniform(states.count)];
   hellolabel.text = state;
 }

这会给你随机的州名

如果你想按顺序

@interface ViewController : UIViewController
{
   NSArray *_states;
}

 - (void)viewDidLoad
 {
  [super viewDidLoad];
   _states = @[@"Albama",@"Alaska"];
 }
- (IBAction)button1:(id)sender 
 {
    static NSInteger position = 0;
    if(position == _states.count-1)
    {
     position = 0;
    }
    else
    {
      position++;
    }
   NSString *state = [_states objectAtIndex:position];
   hellolabel.text = state;
 }
于 2013-08-14T04:54:52.527 回答