每次单击按钮后,如何在 UILabel 上逐一显示文本?
How do I display text on UILabel one-by-one after each click of my button?
我有一个包含文本的数组,我想循环浏览,这样每次我单击我的按钮时,它都会一个接一个地显示文本;
然而,我的代码一按就遍历了整个数组:
- (IBAction)nextButtonOneClicked:(id)sender {
NSArray *titles = @[@"Label One", @"Label Two", @"Label Three",
@"Label Four", @"Label 5", @"Label 6"];
for (int i=0; i<[titles count]; i++) {
NSLog(@"%@", titles[i]);
}
}
我该如何做到每次单击我的按钮时都一个一个地显示文本?
在视图控制器 .m 文件的 class 扩展中添加一个变量以保持当前状态,然后在每次单击按钮时递增该变量:
@interface MyViewController() {
int _currentTitle;
NSArray *_titles;
}
@end
-(instancetype)initWithCoder:(NSCoder *)decoder {
if (self = [super initWithCoder:decoder]) {
_currentTitle = 0;
_titles = @[@"Label One", @"Label Two", @"Label Three",
@"Label Four", @"Label 5", @"Label 6"];
}
return self;
}
- (IBAction)nextButtonOneClicked:(id)sender {
NSString *str = _titles[_currentTitle++];
NSLog(@"%@", str);
myLabel.text = str;
if (_currentTitle == _titles.count) {
_currentTitle = 0;
}
}
我认为你可以使用 static
变量类型
代码在这里
- (IBAction)buttonPressed:(id)sender {
static int Index = 0;
NSArray *titles = @[@"Label One", @"Label Two", @"Label Three",
@"Label Four", @"Label 5", @"Label 6"];
NSLog(@"%@", titles[Index]);
Index++;
if( Index >= titles.count) {
Index = 0;
}
}
@interface myViewController ()
{
int i;
NSArray *titles;
}
- (void)viewDidLoad {
[super viewDidLoad];
i=0;
titles = @[@"Label One", @"Label Two", @"Label Three",
@"Label Four", @"Label 5", @"Label 6"];
}
- (IBAction)nextButtonOneClicked:(id)sender {
if(i<[titles count])
NSLog(@"%@", titles[i]);
i++
}else{
i=0;
}
}
在接口中声明一个整型变量。
int i=0;
执行以下代码:
-(IBAction)nextButtonClicked:(id)sender
{
NSArray *array=@[@"Label 1",@"Label 2",@"Label 3",@"Label 4",@"Label 5",@" Label 6"];
label.text=[array objectAtIndex:i];
NSLog(@"label %@",[array objectAtIndex: i]);
i=i+1;
}
您可以通过在 class 中创建一个成员变量来做到这一点,该变量跟踪用于访问和打印字符串的索引,如
// declare counter
int titleIndexCounter;
// initialize it
titleIndexCounter = 0;
// manipulate it in button's event handler as
- (IBAction)nextButtonOneClicked:(id)sender
{
NSArray *titles = @[@"Label One", @"Label Two", @"Label Three",
@"Label Four", @"Label 5", @"Label 6"];
NSLog(@"%@", [titles objectAtIndex:titleIndexCounter]);
titleIndexCounter++;
if (titleIndexCounter == titles.count)
titleIndexCounter = 0;
}
您也可以通过在按钮的事件处理程序中创建一个静态变量来实现相同的目的(但这是一种不太受欢迎的方法)-
// manipulate it in button's event handler as
- (IBAction)nextButtonOneClicked:(id)sender
{
static int titleIndexCounter = 0;
NSArray *titles = @[@"Label One", @"Label Two", @"Label Three",
@"Label Four", @"Label 5", @"Label 6"];
NSLog(@"%@", [titles objectAtIndex:titleIndexCounter]);
titleIndexCounter++;
if (titleIndexCounter == titles.count)
titleIndexCounter = 0;
}
我有一个包含文本的数组,我想循环浏览,这样每次我单击我的按钮时,它都会一个接一个地显示文本;
然而,我的代码一按就遍历了整个数组:
- (IBAction)nextButtonOneClicked:(id)sender {
NSArray *titles = @[@"Label One", @"Label Two", @"Label Three",
@"Label Four", @"Label 5", @"Label 6"];
for (int i=0; i<[titles count]; i++) {
NSLog(@"%@", titles[i]);
}
}
我该如何做到每次单击我的按钮时都一个一个地显示文本?
在视图控制器 .m 文件的 class 扩展中添加一个变量以保持当前状态,然后在每次单击按钮时递增该变量:
@interface MyViewController() {
int _currentTitle;
NSArray *_titles;
}
@end
-(instancetype)initWithCoder:(NSCoder *)decoder {
if (self = [super initWithCoder:decoder]) {
_currentTitle = 0;
_titles = @[@"Label One", @"Label Two", @"Label Three",
@"Label Four", @"Label 5", @"Label 6"];
}
return self;
}
- (IBAction)nextButtonOneClicked:(id)sender {
NSString *str = _titles[_currentTitle++];
NSLog(@"%@", str);
myLabel.text = str;
if (_currentTitle == _titles.count) {
_currentTitle = 0;
}
}
我认为你可以使用 static
变量类型
代码在这里
- (IBAction)buttonPressed:(id)sender {
static int Index = 0;
NSArray *titles = @[@"Label One", @"Label Two", @"Label Three",
@"Label Four", @"Label 5", @"Label 6"];
NSLog(@"%@", titles[Index]);
Index++;
if( Index >= titles.count) {
Index = 0;
}
}
@interface myViewController ()
{
int i;
NSArray *titles;
}
- (void)viewDidLoad {
[super viewDidLoad];
i=0;
titles = @[@"Label One", @"Label Two", @"Label Three",
@"Label Four", @"Label 5", @"Label 6"];
}
- (IBAction)nextButtonOneClicked:(id)sender {
if(i<[titles count])
NSLog(@"%@", titles[i]);
i++
}else{
i=0;
}
}
在接口中声明一个整型变量。 int i=0;
执行以下代码:
-(IBAction)nextButtonClicked:(id)sender
{
NSArray *array=@[@"Label 1",@"Label 2",@"Label 3",@"Label 4",@"Label 5",@" Label 6"];
label.text=[array objectAtIndex:i];
NSLog(@"label %@",[array objectAtIndex: i]);
i=i+1;
}
您可以通过在 class 中创建一个成员变量来做到这一点,该变量跟踪用于访问和打印字符串的索引,如
// declare counter
int titleIndexCounter;
// initialize it
titleIndexCounter = 0;
// manipulate it in button's event handler as
- (IBAction)nextButtonOneClicked:(id)sender
{
NSArray *titles = @[@"Label One", @"Label Two", @"Label Three",
@"Label Four", @"Label 5", @"Label 6"];
NSLog(@"%@", [titles objectAtIndex:titleIndexCounter]);
titleIndexCounter++;
if (titleIndexCounter == titles.count)
titleIndexCounter = 0;
}
您也可以通过在按钮的事件处理程序中创建一个静态变量来实现相同的目的(但这是一种不太受欢迎的方法)-
// manipulate it in button's event handler as
- (IBAction)nextButtonOneClicked:(id)sender
{
static int titleIndexCounter = 0;
NSArray *titles = @[@"Label One", @"Label Two", @"Label Three",
@"Label Four", @"Label 5", @"Label 6"];
NSLog(@"%@", [titles objectAtIndex:titleIndexCounter]);
titleIndexCounter++;
if (titleIndexCounter == titles.count)
titleIndexCounter = 0;
}