アクションが実行される文字に応じて、文字列を配列内の文字に分割しています。テキストフィールドが空白であるか、テキストフィールドに1文字が含まれている場合、アプリはNSRangeExceptionでクラッシュしました。2つ以上の文字がある場合は、問題なく機能します。コードの一部は次のようになります。
-(IBAction) text: (id) sender{
//labelText.text = [[NSString alloc] initWithFormat:@"%@",textField1];
labelText.text = textField1.text;
NSString *myString = textField1.text;
NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[myString length]];
for (int i=0; i < [myString length]; i++) {
NSString *ichar = [NSString stringWithFormat:@"%c", [myString characterAtIndex:i]];
[characters addObject:ichar];
}
//NSLog (@"%@", [characters objectAtIndex:1]);
//NSLog (@"%@", [characters objectAtIndex:2]);
one1.text = (@"%@", [characters objectAtIndex:1]);
if ([textField1.text length] == 0) {
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Please enter number in the box above." message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[myAlert show];
[myAlert release];
}
else if ([textField1.text length] == 1){
NSString *char1 = [characters objectAtIndex:0];
Charr1 = char1;
}
else if ([textField1.text length] == 2){
NSString *char1 = [characters objectAtIndex:0];
NSString *char2 = [characters objectAtIndex:1];
Charr1 = char1;
Charr2 = char2;
}else if ([textField1.text length] == 3){
NSString *char1 = [characters objectAtIndex:0];
NSString *char2 = [characters objectAtIndex:1];
NSString *char3 = [characters objectAtIndex:2];
Charr1 = char1;
Charr2 = char2;
Charr3 = char3;
}else if ([textField1.text length] == 4){
NSString *char1 = [characters objectAtIndex:0];
NSString *char2 = [characters objectAtIndex:1];
NSString *char3 = [characters objectAtIndex:2];
NSString *char4 = [characters objectAtIndex:3];
Charr1 = char1;
Charr2 = char2;
Charr3 = char3;
Charr4 = char4;
}else if ([textField1.text length] == 5){
NSString *char1 = [characters objectAtIndex:0];
NSString *char2 = [characters objectAtIndex:1];
NSString *char3 = [characters objectAtIndex:2];
NSString *char4 = [characters objectAtIndex:3];
NSString *char5 = [characters objectAtIndex:4];
Charr1 = char1;
Charr2 = char2;
Charr3 = char3;
Charr4 = char4;
Charr5 = char5;