请帮忙。我究竟做错了什么? textfield.text进入一个数组,并假设更改属于另一个数组的标签。
我有多个文本字段。我试图将每个字段的文本保存到一个数组,然后将阿尔法设置为0.然后我有相同数量的标签,我想要更改为文本字段的文本。我尝试过使用可变和不可变的数组。我一直都有错误。
我已经尝试了多种方式,它只是一种愚蠢的东西,我很想念。我已经大大减少了这些数组只是为了发布在这里。
_namesText = [NSMutableArray arrayWithObjects:_nameLabel1.text, _nameLabel2.text, nil];
_names = [NSMutableArray arrayWithObjects:_nameLabel1, _nameLabel2, nil];
_nameInputs = [NSMutableArray arrayWithObjects:_p1NameTextField, _p2NameTextField, nil];
_playerNameText = [NSArray arrayWithObjects:_p1NameTextField.text, _p2NameTextField.text, nil];
enter code here
- (IBAction)enterNamesButton:(id)sender {
//These don't work.
for (int i = 0; i < _numberOfPlayers; i++) {
[_names[i] setAlpha:1];
NSString *tempString = [[NSString alloc]initWithString:[_nameInputs[i] text]];
[_lastTry addObject:tempString];
}
//Then tried this. This is after trying for 2.5 hours and different coding.
for (int i = 0; i < _numberOfPlayers; i++) {
_namesText[i] = _lastTry[i];
UILabel *cell = [[UILabel alloc] init];
cell.text = _lastTry[i];
//[[_namesText[i] textLabel] text] = @"Idiot";
_namesText[i] = cell;
NSLog(@"%@", _namesText[i]);
// This works (but bad practice) and I want to loop through arrays that each UI element belongs to instead of typing it all out.
// _nameLabel1.text = _p1NameTextField.text;
// _nameLabel2.text = _p2NameTextField.text;
我希望这可行,但是NOPE !!!!
答案 0 :(得分:0)
我会这样做:
for (int i = 0; i < _numberOfPlayers; i++) {
_namesText[i] = _lastTry[i];
}
我不明白为什么这不起作用......是吗?
答案 1 :(得分:0)
假设如下:
Array _fromTextField是您要复制的文本字段,并将Alpha设置为0 Array _toLabelField是您希望文本显示的标签 数组_toStoreStrings是你保留所有字符串的地方,以后可能会吗?
NSMutableArray *_fromTextField = [[NSMutableArray alloc] init]; // Put the Text Fields in Here
NSMutableArray *_toLabelField = [[NSMutableArray alloc] init]; // Put the Labels in Here
NSMutableArray *_toStoreStrings = [[NSMutableArray alloc] init];
int count = 0;
int overRun = [_toLabelField count];
for (UITextField *tf in _fromTextField)
{
tf.alpha = 0;
NSString *tempString = [[NSString alloc]initWithString:tf.text];
[_toStoreStrings addObject:tempString];
if (count < overRun) // Check just in case Array sizes not same
{
UILabel *lb = [_toLabelField ObjectAtIndex:count];
lb.text = tempString;
}
else
break;
count++;
}
您的代码面临的问题是您有一个NSObject数组。编译器不知道它们是UITextField还是UILabel,除非你使用(type_cast *)转换它们或者为它们指向一个新的类型变量。因此你看到了崩溃。