我怎样才能继续if和else语句?

时间:2013-08-14 04:30:58

标签: objective-c if-statement

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

- (IBAction)button1:(id)sender {

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

hellolabel.text = @"Alaska";

}

else

{

hellolabel.text = @"Alabama";

}

}

1 个答案:

答案 0 :(得分: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;
 }