gpt4 book ai didi

ios - 更改带有时间延迟的 UITextField 文本

转载 作者:行者123 更新时间:2023-11-29 03:40:54 26 4
gpt4 key购买 nike

我试图以这种方式使 UItextFieldd 上的文字每 1.5 秒更改一次:

-(void)Welcome{

UITextField* WelcomeField = [[UITextField alloc] initWithFrame:CGRectMake(50,230,220,80)];
[self.view addSubview:WelcomeField];
WelcomeField.placeholder = @"Welcome";
WelcomeField.font = [UIFont fontWithName:@"Chalkduster" size:40];
[WelcomeField setBackgroundColor:[UIColor clearColor]];

NSTimer* TimeCounterWelcome = [NSTimer scheduledTimerWithTimeInterval: 1.5 target: self
selector: @selector(ChangeLanguage:) userInfo: nil repeats: YES];
}

- (IBAction)ChangeLanguage:(id)sender{

WelcomeField.placeholder = @"Goodby";


NSTimer* TimeCounterWelcome = [NSTimer scheduledTimerWithTimeInterval: 1.5 target: self
selector: @selector(ChangeLanguage2:) userInfo: nil repeats: YES];

}

- (IBAction)ChangeLanguage2:(id)sender{

WelcomeField.placeholder = @"Friend";

}

问题是我有 30 个单词,而且我不能重复代码 30 次。有没有更快的方法?

最佳答案

像这样的东西会起作用:

-(void)Welcome{

UITextField* WelcomeField = [[UITextField alloc]initWithFrame:CGRectMake(50,230,220,80)];
[self.view addSubview:WelcomeField];
WelcomeField.placeholder = @"Welcome";
WelcomeField.font = [UIFont fontWithName:@"Chalkduster" size:40];
[WelcomeField setBackgroundColor:[UIColor clearColor]];

// global integer 'wordIndex'
wordIndex = 0;

// global NSArray 'allTheWords'
allTheWords = [NSArray arrayWithObjects:word1, word2, word3, etc., nil];

NSTimer* TimeCounterWelcome = [NSTimer scheduledTimerWithTimeInterval: 1.5 target: self
selector: @selector(ChangeLanguage:) userInfo: nil repeats: YES];
}

-(void)ChangeLanguage:(id)sender{
NSString * nextWord = [allTheWords objectAtIndex:wordIndex];
WelcomeField.placeholder = nextWord;
wordIndex++;

// assuming to start from beginnning again
if(wordIndex >= 30) {
wordIndex = 0;
}
}

一个小小的风格评论:尝试以小写字母 i.s.o 开头方法和变量名。一个大写字母。如果您想遵循 Objective-C 编码指南,那就更好了。

关于ios - 更改带有时间延迟的 UITextField 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18467075/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com