Help with Weird Text Validation Issue on iPHone App - iphone

I am using two forms of text validation (one when they click Next, another when they manually select the next text field to enter) and it's been working perfect. However, the last text field isn't getting error checked and I can't figure it out.
You can see the issue in this video on the last text field (http://screencast.com/t/ODJiOTAwMzA). The previous four work fine as you can see, but the bottom not so much.
Here is my code:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == txtUserName)
{
NSString *userNameOne = txtUserName.text;
double numOne = [userNameOne doubleValue];
if(numOne < 30 || numOne > 80)
{
//foo
[txtUserName becomeFirstResponder];
txtUserName.text = nil;
}
else
{
[txtUserName2 becomeFirstResponder];
}
}
else if (textField == txtUserName2)
{
NSString *userNameThree = txtUserName2.text;
float numTwo = [userNameThree doubleValue];
if (numTwo < 20 || numTwo > 32)
{
//foo
[txtUserName2 becomeFirstResponder];
txtUserName2.text = nil;
}
else
{
[txtUserName3 becomeFirstResponder];
}
}
else if (textField == txtUserName3)
{
NSString *userNameThree = txtUserName3.text;
float numThree = [userNameThree doubleValue];
if (numThree < 475 || numThree > 650)
{
//foo
[txtUserName3 becomeFirstResponder];
txtUserName3.text = nil;
}
else
{
[txtUserName4 becomeFirstResponder];
}
}
else if (textField == txtUserName4)
{
NSString *userNameFour = txtUserName4.text;
double numFour = [userNameFour doubleValue];
if (numFour < 0.5 || numFour > 3.00)
{
//foo
[txtUserName4 becomeFirstResponder];
txtUserName4.text = nil;
}
else
{
[txtUserName5 becomeFirstResponder];
}
}
else if (textField == txtUserName5)
{
NSString *userNameFive = txtUserName5.text;
double numFive = [userNameFive doubleValue];
if (numFive > 1)
{
//foo
}
}
return NO;
}
and here
if (textField == txtUserName)
{
NSString *userNameOne = txtUserName.text;
double numOne = [userNameOne doubleValue];
if(numOne < 30 || numOne > 80)
{
//foo
[txtUserName becomeFirstResponder];
txtUserName.text = nil;
}
else
{
[txtUserName2 becomeFirstResponder];
}
}
else if (textField == txtUserName2)
{
NSString *userNameThree = txtUserName2.text;
float numTwo = [userNameThree doubleValue];
if (numTwo < 20 || numTwo > 32)
{
//foo
[txtUserName2 becomeFirstResponder];
txtUserName2.text = nil;
}
else
{
[txtUserName3 becomeFirstResponder];
}
}
else if (textField == txtUserName3)
{
NSString *userNameThree = txtUserName3.text;
float numThree = [userNameThree doubleValue];
if (numThree < 475 || numThree > 650)
{
//fo
[txtUserName3 becomeFirstResponder];
txtUserName3.text = nil;
}
else
{
[txtUserName4 becomeFirstResponder];
}
}
else if (textField == txtUserName4)
{
NSString *userNameFour = txtUserName4.text;
double numFour = [userNameFour doubleValue];
if (numFour < 0.5 || numFour > 3.00)
{
//foo
[txtUserName4 becomeFirstResponder];
txtUserName4.text = nil;
}
else
{
[txtUserName5 becomeFirstResponder];
}
}
else if (textField == txtUserName5)
{
NSString *userNameFive = txtUserName5.text;
double numFive = [userNameFive doubleValue];
if (numFive > 1)
{
//foo
}
else
{
[txtUserName5 becomeFirstResponder];
}
}

If your interface is in Interface Builder, is the outlet for txtUserName5 hooked up?
For your fifth text field, why don't you have these lines after "foo" like you do for the other text fields?
if (numFive > 1)
{
//foo
[txtUserName5 becomeFirstResponder];
txtUserName5.text = nil;
}
Also, for your else clause, rather than become first responder again, you should resign first responder (to dismiss the keyboard):
else
{
//[txtUserName5 becomeFirstResponder];
[txtUserName5 resignFirstResponder];
return YES;
}

Related

How to Append a Special Character after every 3 characters in UITextField Ex: (123-12346) like '-' i did it but issue while Clear

I am getting Phone card number form user in UI text field. The format of number is like
123-4567-890
I want that as user types 123 automatically - is inserted in UITextField same after 4567 - and so on.
I Did it using following code in UITextField delegate method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
{
NSLog(#"***** %d",textField.text.length);
if(textField.text.length == 3)
{
textField.text = [textField.text stringByAppendingString:#"-"];
}
return YES;
}
But the Problem raised while clear the text, When we start clearing.
Last 3 digits 890 clears and then - addded, we cleared it and again added and soooo on so clearing stop at
We clear all the text at a time using
textField.clearButtonMode = UITextFieldViewModeWhileEditing; //To clear all text at a time
But our requirement is user must delete one character at a time.
How to achieve it?
During clearing replacementString should be empty #"". So replacement string should be checked also in addition to length check. Like this:
if (textField.text.length == 3 && ![string isEqualToString:#""]) {
// append -
}
USE: I have seen this somewhere in this forum, It worked for me
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *filter = #"###-####-###";
if(!filter) return YES;
NSString *changedString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if(range.length == 1 && string.length < range.length && [[textField.text substringWithRange:range] rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:#"0123456789"]].location == NSNotFound)
{
NSInteger location = changedString.length-1;
if(location > 0)
{
for(; location > 0; location--)
{
if(isdigit([changedString characterAtIndex:location]))
break;
}
changedString = [changedString substringToIndex:location];
}
}
textField.text = filteredStringFromStringWithFilter(changedString, filter);
return NO;
}
NSString *filteredStringFromStringWithFilter(NSString *string, NSString *filter)
{
NSUInteger onOriginal = 0, onFilter = 0, onOutput = 0;
char outputString[([filter length])];
BOOL done = NO;
while(onFilter < [filter length] && !done)
{
char filterChar = [filter characterAtIndex:onFilter];
char originalChar = onOriginal >= string.length ? '\0' : [string characterAtIndex:onOriginal];
switch (filterChar) {
case '#':
if(originalChar=='\0')
{
done = YES;
break;
}
if(isdigit(originalChar))
{
outputString[onOutput] = originalChar;
onOriginal++;
onFilter++;
onOutput++;
}
else
{
onOriginal++;
}
break;
default:
outputString[onOutput] = filterChar;
onOutput++;
onFilter++;
if(originalChar == filterChar)
onOriginal++;
break;
}
}
outputString[onOutput] = '\0';
return [NSString stringWithUTF8String:outputString];
}

Parsing xml content tag

my problem is that i cant parse the tag from a xml file.
It returns a null value, im testing it by using an NSLog with %#.
If someone could point me a solution i would be very thankful.
Here is the code:
BOOL processed = NO;
if (currentText) {
// Remove newlines and whitespace from currentText
NSString *processedText = [currentText stringByRemovingNewLinesAndWhitespace];
// Process
switch (feedType) {
case FeedTypeRSS: {
// Item
if (!processed) {
if ([currentPath isEqualToString:#"/rss/channel/item/title"]) { if (processedText.length > 0) item.title = processedText; processed = YES; }
else if([currentPath isEqualToString:#"/rss/channel/item/url"]) { if (processedText.length > 0) item.image = processedText; processed = YES; }
else if([currentPath isEqualToString:#"/rss/channel/item/category"]) { if (processedText.length > 0) item.category = processedText; processed = YES; }
else if ([currentPath isEqualToString:#"/rss/channel/item/link"]) { if (processedText.length > 0) item.link = processedText; processed = YES; }
else if ([currentPath isEqualToString:#"/rss/channel/item/guid"]) { if (processedText.length > 0) item.identifier = processedText; processed = YES; }
else if ([currentPath isEqualToString:#"/rss/channel/item/description"]) { if (processedText.length > 0) item.summary = processedText; processed = YES; }
else if ([currentPath isEqualToString:#"/rss/channel/item/content:encoded"]) { if (processedText.length > 0) item.content = processedText; processed = YES; }
else if ([currentPath isEqualToString:#"/rss/channel/item/pubDate"]) { if (processedText.length > 0) item.date = [NSDate dateFromInternetDateTimeString:processedText formatHint:DateFormatHintRFC822]; processed = YES; }
else if ([currentPath isEqualToString:#"/rss/channel/item/enclosure"]) { [self createEnclosureFromAttributes:currentElementAttributes andAddToItem:item]; processed = YES; }
else if ([currentPath isEqualToString:#"/rss/channel/item/dc:date"]) { if (processedText.length > 0) item.date = [NSDate dateFromInternetDateTimeString:processedText formatHint:DateFormatHintRFC3339]; processed = YES; }
}
// Info
if (!processed && feedParseType != ParseTypeItemsOnly) {
if ([currentPath isEqualToString:#"/rss/channel/title"]) { if (processedText.length > 0) info.title = processedText; processed = YES; }
else if([currentPath isEqualToString:#"/rss/channel/item/url"]) { if (processedText.length > 0) item.image = processedText; processed = YES; }
else if([currentPath isEqualToString:#"/rss/channel/item/category"]) { if (processedText.length > 0) item.category = processedText; processed = YES; }
else if ([currentPath isEqualToString:#"/rss/channel/description"]) { if (processedText.length > 0) info.summary = processedText; processed = YES; }
else if ([currentPath isEqualToString:#"/rss/channel/link"]) { if (processedText.length > 0) info.link = processedText; processed = YES; }
}
break;
}
case FeedTypeRSS1: {
// Item
if (!processed) {
if ([currentPath isEqualToString:#"/rdf:RDF/item/title"]) { if (processedText.length > 0) item.title = processedText; processed = YES; }
else if([currentPath isEqualToString:#"/rss/channel/item/url"]) { if (processedText.length > 0) item.image = processedText; processed = YES; }
else if([currentPath isEqualToString:#"/rss/channel/item/category"]) { if (processedText.length > 0) item.category = processedText; processed = YES; }
else if ([currentPath isEqualToString:#"/rdf:RDF/item/link"]) { if (processedText.length > 0) item.link = processedText; processed = YES; }
else if ([currentPath isEqualToString:#"/rdf:RDF/item/dc:identifier"]) { if (processedText.length > 0) item.identifier = processedText; processed = YES; }
else if ([currentPath isEqualToString:#"/rdf:RDF/item/description"]) { if (processedText.length > 0) item.summary = processedText; processed = YES; }
else if ([currentPath isEqualToString:#"/rdf:RDF/item/content:encoded"]) { if (processedText.length > 0) item.content = processedText; processed = YES; }
else if ([currentPath isEqualToString:#"/rdf:RDF/item/dc:date"]) { if (processedText.length > 0) item.date = [NSDate dateFromInternetDateTimeString:processedText formatHint:DateFormatHintRFC3339]; processed = YES; }
else if ([currentPath isEqualToString:#"/rdf:RDF/item/enc:enclosure"]) { [self createEnclosureFromAttributes:currentElementAttributes andAddToItem:item]; processed = YES; }
}
call method:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// Configure the cell.
MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row];
if (item) {
NSLog(#"content = %#",item.content);//<-------------------------------------------Returns a NULL
// Process
NSString *itemTitle = item.title ? [item.title stringByConvertingHTMLToPlainText] : #"[No Title]";
NSString *itemSummary = item.summary ? [item.summary stringByConvertingHTMLToPlainText] : #"[No Summary]";
// Set
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
cell.textLabel.text = itemTitle;
NSMutableString *subtitle = [NSMutableString string];
if (item.date) [subtitle appendFormat:#"%#: ", [formatter stringFromDate:item.date]];
[subtitle appendString:itemSummary];
cell.detailTextLabel.text = subtitle;
}
return cell;
}
Best Regards
While parsing every element the parser will return a whitespace character as a response because of new line / tabbing / carriage returns. If you have implemented a call back methods (delegates) of parser then check for null values, and i donno where you have implemented(in the sense,in which parser delegate method has this) the very first snippet.

ObjC - Using NSRange, Allow User to Enter Only One decimal point -

building a simple calculator here to get my feet wet with iOS dev. I want to prohibit the entry of more than one decimal point. Put my logic in the comment.
It builds and runs with no problem, but it's not blocking the entry of more than one "." any help is appreciated. Thanks.
- (IBAction)digitPressed:(UIButton *)sender
{
NSString *digit = [[sender titleLabel] text];
if (userIsInTheMiddleofTypingANumber) {
[display setText:[[display text] stringByAppendingString:digit]];
// if the user enters a decimal point, check if the existing display text contains a "." if not, then allow it, but if it does, do not allow it.
NSString *decimal = #".";
NSRange range = [digit rangeOfString:decimal];
if (range.location == NSNotFound) {
[display setText:digit];
}
} else {
[display setText:digit];
userIsInTheMiddleofTypingANumber = YES;
}
}
- (IBAction)digitPressed:(UIButton *)sender
{
NSString *digit = [[sender titleLabel] text];
NSString *decimal = #".";
BOOL decimalAlreadyEntered = [display.text rangeOfString:decimal].location == NSNotFound ? NO : YES;
if (userIsInTheMiddleofTypingANumber) {
if (([digit isEqual:decimal] && !decimalAlreadyEntered) || !([digit isEqual:decimal])) {
[display setText:[[display text] stringByAppendingString:digit]];
}
}
else if (display.text isEqual:#"0" && digit == decimal){
[display setText:[[display text] stringByAppendingString:digit]];
userIsInTheMiddleofTypingANumber = YES;
}
else {
[display setText:digit];
userIsInTheMiddleofTypingANumber = YES;
}
}
I think this should work. It looks like you are appending the digit first thing in your loop, even if a decimal has been entered. I haven't tried this code in the compiler but it checks to see if a decimal has been entered first.
Don't frustrate your user. Give them feedback about what you can and cannot accept for input:
- (IBAction)digitPressed:(UIButton *)sender
{
NSString *char = [[sender titleLabel] text];
if( [char isEqualTo:#"."] ){
[sender setEnabled:NO];
}
[display setText:[[display text] stringByAppendingString:char]];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)replacementRange replacementString:(NSString *)replacementString {
// Re-enable the button if the decimal point is deleted
NSRange rangeForPoint = [[display.text substringWithRange:replacementRange]
rangeOfString:#"."];
if( NSNotFound != rangeForPoint.location ){
// You may also want to check that replacementString does not contain a new
// decimal point, in the case of pasted text, e.g.
[decimalPointButton setEnabled:YES];
}
}
This delegate method, textField:shouldChangeCharactersInRange:replacementString: is really the key to handling restricted input. You should also look at NSFormatter, which can take a format and validate input as the user types, using its isPartialStringValid... methods.
You might want to check an earlier post by me, which deals with sort of the same issue. I tried creating a textfield that accepts limited input (for example only numbers in the following format: 0.00). It never worked completely great (it had some bugs with currency I believe), but it might be good enough for your purposes:
Re-Apply currency formatting to a UITextField on a change event
This worked for me:
- (IBAction)digitPressed:(UIButton *)sender
{
NSString *digit = [[sender titleLabel] text];
if (userIsInTheMiddleOfTypingANumber)
{
if (decimalEntered)
{
NSRange range = [digit rangeOfString:#"."];
if (range.location == NSNotFound)
{
[display setText:[[display text] stringByAppendingString:digit]];
}
else
{
NSLog(#"You can't enter more than one decimal");
}
}
else if (!decimalEntered)
{
NSRange range = [digit rangeOfString:#"."];
if (range.location == NSNotFound)
{
[display setText:[[display text] stringByAppendingString:digit]];
}
else
{
[display setText:[[display text] stringByAppendingString:digit]];
decimalEntered = YES;
}
}
}
else
{
decimalEntered = NO;
[display setText:digit];
userIsInTheMiddleOfTypingANumber = YES;
}
}
Try below updated code of yours
- (IBAction)digitPressed:(UIButton *)sender
{
NSString *digit = [[sender titleLabel] text];
if (userIsInTheMiddleofTypingANumber)
{
// if the user enters a decimal point, check if the existing display text contains a "." if not, then allow it, but if it does, do not allow it.
NSString *decimal = #".";
NSRange range = [digit rangeOfString:decimal];
if (range.location == NSNotFound)
{
[display setText:[[display text] stringByAppendingString:digit]];
[display setText:digit];
}
}
else
{
[display setText:digit];
userIsInTheMiddleofTypingANumber = YES;
}
}

wait for a method to finish before starting another

assume that i have two sequential methods, methodA and methodB (methodB is called at the end of methodA) and i need to finish completely with the process of methodA before starting methodB :
-(void)methodA {
//do some very heavy computation here
[self methodB];
}
-(void)methodB {
//this method performs some animation
}
methodA requires alot of processing power and takes time to finish while methodB has animations. The animations are starting laggy at first and im guessing this has to do with the overhead of method A. So how can i make the second method start only after the first has finished completely?
this is the actual code:
the code is very messy and unorganized. there is alot of code that has been commented out so please ingore that. I test some things sometimes and then comment them out incase i need them later.
here is what is happening: it starts with (void)checkMoves method. This method calls either one of two similar methods which are
-(void)getMovesForJourney:(int)journey withRolls:(NSArray *)rolls or
-(void) getEntryMovesForJourney:(int)journey withRolls:(NSArray *)rolls
these methods are wrappers for a long recursive process (which is most probably where the multithreading is happening).
at the end of (void)checkMoves another method -(void)analyseMoves is called. This is also a heavy process.
and finally, at the end of -(void)analyseMoves the animation method -(void)move: is called. this last method is starting with a lag.
finally at the end of -(void)move: the process will start again until no more 'moves' are available.
the methods below are not in order so please refer to the description above to know whats going on.
-(void) checkMoves {
GameScene *game = [GameScene sharedGameScene];
CCLOG(#"start");
for (Stone *stone in stones) {
if (!stone.onBoard) {
if ([game EntryAvailable]) {
[self getEntryMovesForJourney:stone.journey withRolls:[game rollsAsIntegers]];
}
}
else {
[self getMovesForJourney:stone.journey withRolls:[game rollsAsIntegers]];
}
}
CCLOG(#"finish");
[self analyseMoves];
}
-(NSMutableArray *) otherPlayerLocations {
GameScene *game = [GameScene sharedGameScene];
Board *board = [game board];
Player *otherPlayer = [game playerOne];
NSMutableArray *locations = [[[NSMutableArray alloc] init] autorelease];
for (Stone *stone in otherPlayer.stones) {
if (stone.journey < 77) {
int location;
if (stone.onBoard)
location = [[board tileForATag:[self convertJourneyToTileTag:stone.journey]] bTag];
else
location = 0;
[locations addObject:[NSNumber numberWithInt:location]];
}
}
return locations;
}
-(void) analyseMoves {
if (moves.count > 0) {
NSMutableArray *killMoves = [[[NSMutableArray alloc] init] autorelease];
NSMutableArray *safeMoves = [[[NSMutableArray alloc] init] autorelease];
int mostThreatened;
float highestThreat = -1.0;
float lowestThreat = 100.0;
AIMove *safestMove;
//get kill and safe moves in seperate arrays
for (AIMove *move in moves) {
if (move.moveType == killMoveType)
[killMoves addObject:move];
else if (move.moveType == safeMoveType)
[safeMoves addObject:move];
// get highest threatened stone
//if (move.potThreat > highestThreat) {
// mostThreatened = move.journey;
// highestThreat = move.potThreat;
if (move.initThreat < lowestThreat) {
lowestThreat = move.initThreat;
safestMove = [move copy];
CCLOG(#"safest move assigned");
}
//}
}
//MOVE SELECTION:
//___________________________________
//choose best kill move
int killJourney;
if (killMoves.count > 0) {
//leave one move in killMoves with highest journey
killJourney = [[killMoves objectAtIndex:0] tileTag];
for (AIMove *killMove in killMoves) {
if (killMove.tileTag < killJourney)
[killMoves removeObject:killMove];
else
killJourney = killMove.tileTag;
}
}
//select correct move
if (killMoves.count > 0) {
[self move:[killMoves objectAtIndex:0]];
CCLOG(#"Kill move chosen");
}
else {
CCLOG(#"this is called!!!!");
CCLOG(#"safest move with initThreat: %i, potThreat: %i",safestMove.journey, safestMove.tileTag);
[self move:safestMove];
CCLOG(#"Success");
}
/*
else if (safeMoves.count > 0) {
[self move:[safeMoves objectAtIndex:0]];
CCLOG(#"safe move chosen");
}
else {
//temp random move generation
randomMove = CCRANDOM_0_1()*[moves count];
AIMove *move = [moves objectAtIndex:randomMove];
[self move:move];
CCLOG(#"Random move chosen");
}
*/
}
}
-(void) assignThreatLevel:(AIMove *)move {
NSArray *otherPlayerStones = [self otherPlayerLocations];
NSMutableArray *initThreats = [[NSMutableArray alloc] init];
NSMutableArray *potThreats = [[NSMutableArray alloc] initWithCapacity:4];
for (NSNumber *location in otherPlayerStones) {
//get threat levels for potential moves
if (move.tileTag > [location intValue]) {
int dif = move.tileTag - [location intValue];
CCLOG(#"dif: %i",dif);
//threat level conditions:
// 1 >> 5 = 70% of journey
// 6 >> 9 = 40% of journey
// 10 & 11 = 50% of journey
// 12 >> 24 = 20% of journey
// 25 && 26 = 50% of journey
// 27+ = 20% of journey
if (move.tileTag < 9) {
[initThreats addObject:[NSNumber numberWithFloat:0.0f]];
}
else if (dif >= 1 && dif <= 5) {
[initThreats addObject:[NSNumber numberWithFloat:k1to5]];
}
else if (dif >= 6 && dif <= 9) {
[initThreats addObject:[NSNumber numberWithFloat:k6to9]];
}
else if (dif == 10 || dif == 11) {
[initThreats addObject:[NSNumber numberWithFloat:k10to11]];
}
else if (dif >= 12 && dif <= 24) {
[initThreats addObject:[NSNumber numberWithFloat:k12to24]];
}
else if (dif == 25 || dif == 26) {
[initThreats addObject:[NSNumber numberWithFloat:k25to26]];
}
else if (dif > 26) {
[initThreats addObject:[NSNumber numberWithFloat:k27plus]];
}
//-------------------------------------
}
//get Threat levels for current positions
if (move.tileTag > [location intValue]) {
int dif = move.tileTag - [location intValue];
//threat level conditions:
// 1 >> 5 = 70% of journey
// 6 >> 9 = 40% of journey
// 10 & 11 = 50% of journey
// 12 >> 24 = 20% of journey
// 25 && 26 = 50% of journey
// 27+ = 20% of journey
if (move.journey < 8 || move.journey > 75)
[potThreats addObject:[NSNumber numberWithFloat:0.0f]];
else if (dif >= 1 && dif <= 5)
[potThreats addObject:[NSNumber numberWithFloat:k1to5]];
else if (dif >= 6 && dif <= 9)
[potThreats addObject:[NSNumber numberWithFloat:k6to9]];
else if (dif == 10 || dif == 11)
[potThreats addObject:[NSNumber numberWithFloat:k10to11]];
else if (dif >= 12 && dif <= 24)
[potThreats addObject:[NSNumber numberWithFloat:k12to24]];
else if (dif == 25 || dif == 26)
[potThreats addObject:[NSNumber numberWithFloat:k25to26]];
else if (dif > 26)
[potThreats addObject:[NSNumber numberWithFloat:k27plus]];
//-------------------------------------
}
}
float initThreat = 0.0f;
float potThreat = 0.0f;
for (NSNumber *number in initThreats) {
if ([number floatValue] > initThreat)
initThreat = [number floatValue];
}
for (NSNumber *number in potThreats) {
if ([number floatValue] > potThreat)
potThreat = [number floatValue];
}
move.initThreat = initThreat;
move.potThreat = potThreat;
[initThreats release];
}
-(void) move:(AIMove *)move {
CCLOG(#"Moves count: %i",[moves count]);
if ([moves count] > 0) {
BOOL isOtherStoneOnPreviousTile = NO;
int total;
if (move.tileTag > 8)
total = move.tileTag - move.journey;
else if (move.tileTag < 9 && move.journey > 8)
total = ((9-move.tileTag)+75) - move.journey;
else if (move.tileTag < 9)
total = move.tileTag - move.journey;
CCLOG(#"Total: %i",total);
GameScene *game = [GameScene sharedGameScene];
Board *board = [game board];
BoardTile *tile = [[game board] tileForBTag:move.tileTag];
CCSequence *sequence;
NSMutableArray *actions = [[[NSMutableArray alloc] init] autorelease];
id delay = [CCDelayTime actionWithDuration:0.5];
[actions addObject:delay];
if (move.journey > 0) {
for (int i = 1; i < total+1; i++) {
if (move.journey+i < 76)
tile = [board tileForBTag:move.journey+i];
else
tile = [board tileForBTag:9-((move.journey+i)-75)];
id moveAction = [CCMoveTo actionWithDuration:0.2 position:tile.position];
[actions addObject:moveAction];
}
}
else {
id moveAction = [CCMoveTo actionWithDuration:0.2 position:tile.position];
[actions addObject:moveAction];
}
// id moveFunc = [CCCallFunc actionWithTarget:self selector:#selector(moveMotion)];
//id moveAction = [CCMoveTo actionWithDuration:0.3 position:tile.position];
id killFunc = [CCCallFuncND actionWithTarget:self selector:#selector(killStonesForTile:data:) data:tile];
//id callfunc = [CCCallFunc actionWithTarget:self selector:#selector(move)];
[actions addObject:killFunc];
//[actions addObject:callfunc];
sequence = [CCSequence actionMutableArray:actions];
[actions removeAllObjects];
CGPoint exitPos = ccp(exitPosition.x-(completeStones*30),exitPosition.y-(completeStones*30));
id move2 = [CCMoveTo actionWithDuration:0.2f position:exitPos];
id sequence2 = [CCSequence actions:move2, nil];
Stone *stone = [self getStoneForJourney:move.journey];
//-
//------------------------------------------
//clean tracks
for (Stone *stone in stones) {
if (stone.journey == (move.tileTag - move.journey))
isOtherStoneOnPreviousTile = YES;
}
if (!isOtherStoneOnPreviousTile) {
BoardTile *prevTile = [board tileForBTag:[self convertJourneyToTileTag:move.journey]];
prevTile.occupiedBy = 0;
}
//===========================================
//-------------------------------------------
//set stone status
if (move.tileTag < 9 && stone.crossedEntry)
stone.journey = (9 - move.tileTag) + 75;
else
stone.journey = move.tileTag;
stone.onBoard = YES;
tile.occupiedBy = player2;
if (stone.journey > 8 && !stone.crossedEntry)
stone.crossedEntry = YES;
//============================================
if (stone.journey < 84)
[stone runAction:sequence];
else {
[stone runAction:sequence2];
completeStones++;
stone.isJourneyComplete = YES;
}
CCLOG(#"Stone Journey:%i",stone.journey);
NSArray *rollTypesArray = [move rollTypes];
[self removeRollTypes:rollTypesArray];
[moves removeAllObjects];
[game updateRollResults];
[self updateMoveAvailability];
}
else {
GameScene *game = [GameScene sharedGameScene];
[moves removeAllObjects];
[game nextTurn];
[game updateRollResults];
}
}
-(Stone *)getStoneForJourney:(int)journey {
Stone *theStone;
for (Stone *stone in stones) {
if (stone.journey == journey)
theStone = stone;
}
return theStone;
}
-(void)dealloc {
[moves release];
[rollTypes release];
[results release];
[super dealloc];
}
-(void)killStonesForTile:(id)sender data:(BoardTile *)tile {
GameScene *game = [GameScene sharedGameScene];
int tileTag;
Player *otherPlayer;
if (playerNumber == player1) {
tileTag = tile.aTag;
otherPlayer = [game playerTwo];
}
else {
tileTag = tile.bTag;
otherPlayer = [game playerOne];
}
CCArray *currentStones = [otherPlayer stones];
for (Stone *stone in currentStones) {
if (!stone.isJourneyComplete) {
int journey = stone.journey;
if (tileTag == tile.aTag) {
if (journey > 0 && [self convertJourneyToTileTag:journey] == tile.bTag) {
CCLOG(#"blue stone killed");
[self returnStoneToOrigin:stone];
}
}
if (tileTag == tile.bTag) {
if (journey > 0 && [self convertJourneyToTileTag:journey] == tile.aTag) {
CCLOG(#"gold stone killed");
[self returnStoneToOrigin:stone];
}
}
}
}
}
-(void)removeRollTypes:(NSArray *)theRollTypes {
GameScene *game = [GameScene sharedGameScene];
NSMutableArray *rolls = [game rolls];
for (NSNumber *roll in theRollTypes) {
NSUInteger index = [rolls indexOfObject:[game convertIntToRoll:roll]];
CCLOG(#"rolltypes count: %i",[theRollTypes count]);
CCLOG(#"roll integer: %i",[roll intValue]);
[rolls removeObjectAtIndex:index];
}
}
#pragma mark -
#pragma mark enumerations
- (NSArray*)getSums:(NSArray*)numbers {
NSMutableArray *result = [self getSumsHelper:numbers startingFrom:0];
[result removeObjectAtIndex:0];
return result;
}
- (NSMutableArray*)getSumsHelper:(NSArray*)numbers startingFrom:(NSUInteger)index {
/* (1) */
if (index >= [numbers count])
return [NSMutableArray arrayWithObject:[NSNumber numberWithFloat:0]];
/* (2) Generate all the subsets where the `index`th element is not included */
NSMutableArray* result = [self getSumsHelper:numbers startingFrom:index+1];
// NSSortDescriptor *mySorter = [[NSSortDescriptor alloc] initWithKey:#"floatValue" ascending:YES];
// [result sortUsingDescriptors:[NSArray arrayWithObject:mySorter]];
/* (3) Add all the cases where the `index`th element is included */
NSUInteger i, n = [result count];
float element = [[numbers objectAtIndex:index] floatValue];
for (i = 0; i < n; i++) {
float element2 = [[result objectAtIndex:i] floatValue];
float sum = element+element2;
BOOL numberPresent = NO;
for (NSNumber *number in result) {
if ([number floatValue] == sum)
numberPresent = YES;
}
if (!numberPresent)
[result addObject:[NSNumber numberWithFloat:sum]];
}
return result;
}
-(NSArray *) getCombsforNumbers:(NSArray *)numbers withTarget:(int)target{
NSMutableArray *partial = [[NSMutableArray alloc] init];
[partial addObject:[NSNumber numberWithInt:0]];
[results removeAllObjects];
NSArray *result = [self getCombsHelper:numbers target:target partial:partial];
NSUInteger minCount = [[result objectAtIndex:0] count];
NSUInteger index = 0;
NSMutableArray *combArray = [result objectAtIndex:0];
for (NSMutableArray *array in result) {
if ([array count] < minCount) {
minCount = [array count];
index = [result indexOfObject:array];
combArray = array;
}
}
//remove number 0 from array
[combArray removeObject:[NSNumber numberWithInt:0]];
return combArray;
}
-(NSMutableArray *) getCombsHelper:(NSArray *)numbers target:(int)target partial:(NSMutableArray *)partial {
int s = 0;
for (NSNumber *number in partial) {
s += [number intValue];
}
if (s == target) {
[results addObject:partial];
}
if (s >= target) {
return results;
}
for (int i = 0; i < [numbers count]; i++) {
NSMutableArray *remaining = [[[NSMutableArray alloc] init] autorelease];
int n = [[numbers objectAtIndex:i] intValue];
for (int j = i+1; j<[numbers count]; j++) {
[remaining addObject:[numbers objectAtIndex:j]];
}
NSMutableArray *partialRec = [[[NSMutableArray alloc] init] autorelease];
[partialRec addObjectsFromArray:partial];
[partialRec addObject:[NSNumber numberWithInt:n]];
[self getCombsHelper:remaining target:target partial:partialRec];
}
return results;
}
-(void) getMovesForJourney:(int)journey withRolls:(NSArray *)rolls {
GameScene *game = [GameScene sharedGameScene];
Board *board = [game board];
NSArray *sums = [self getSums:rolls];
for (NSNumber *number in sums) {
if ([number intValue]+journey <= 84) {
BoardTile *tile = [board tileForBTag:[self convertJourneyToTileTag:journey+[number intValue]]];
if (tile.isSafeTile) {
if (tile.occupiedBy != player1) {
AIMove *move = [AIMove moveWithJourney:journey tileTag:tile.bTag];
NSArray *theRollTypes = [[self getCombsforNumbers:rolls withTarget:[number intValue]] copy];
//Checking rolltypes, remove later
NSLog(#"%i = ",[number intValue]);
for (NSNumber *comb in theRollTypes) {
NSLog(#"%i",[comb intValue]);
}
NSLog(#"-----------");
//----------------------------------
move.moveType = safeMoveType;
move.initThreat = 0.0;
CCLOG(#"move initThreat: %f",move.initThreat);
[move setRollTypes:theRollTypes];
[moves addObject:move];
}
}
else {
AIMove *move = [AIMove moveWithJourney:journey tileTag:tile.bTag];
NSArray *theRollTypes = [[self getCombsforNumbers:rolls withTarget:[number intValue]] copy];
//Checking rolltypes, remove later
NSLog(#"%i = ",[number intValue]);
for (NSNumber *comb in theRollTypes) {
NSLog(#"%i",[comb intValue]);
}
NSLog(#"-----------");
//-----------------------------------
[move setRollTypes:theRollTypes];
//assing threat level
[self assignThreatLevel:move];
CCLOG(#"move initThreat: %f",move.initThreat);
//check for kill move
NSArray *otherPlayerPositions = (NSArray *)[self otherPlayerLocations];
for (NSNumber *location in otherPlayerPositions) {
if (move.tileTag == [location intValue])
move.moveType = killMoveType;
}
[moves addObject:move];
}
}
//int i = [number intValue];
//NSArray *combs = [self getCombsforNumbers:numbers withTarget:i];
}
}
-(void) getEntryMovesForJourney:(int)journey withRolls:(NSArray *)rolls {
GameScene *game = [GameScene sharedGameScene];
Board *board = [game board];
NSArray *sums = [self getSums:rolls];
for (NSNumber *number in sums) {
if ([number intValue]+journey <= 84) {
BoardTile *tile = [board tileForBTag:[self convertJourneyToTileTag:journey+[number intValue]]];
if (tile.isSafeTile) {
if (tile.occupiedBy != player1) {
NSArray *theRollTypes = [[self getCombsforNumbers:rolls withTarget:[number intValue]] copy];
BOOL containsEntry = NO;
for (NSNumber *rollType in theRollTypes) {
if ([rollType intValue] == 1) {
containsEntry = YES;
}
}
if (containsEntry) {
AIMove *move = [AIMove moveWithJourney:journey tileTag:tile.bTag];
//Checking rolltypes, remove later
NSLog(#"%i = ",[number intValue]);
for (NSNumber *comb in theRollTypes) {
NSLog(#"%i",[comb intValue]);
}
NSLog(#"-----------");
move.moveType = safeMoveType;
move.initThreat = 0.0;
CCLOG(#"move initThreat: %f",move.initThreat);
[move setRollTypes:theRollTypes];
[moves addObject:move];
//----------------------------------
}
}
}
else {
NSArray *theRollTypes = [[self getCombsforNumbers:rolls withTarget:[number intValue]] copy];
BOOL containsEntry = NO;
for (NSNumber *rollType in theRollTypes) {
if ([rollType intValue] == 1) {
containsEntry = YES;
}
}
if (containsEntry) {
AIMove *move = [AIMove moveWithJourney:journey tileTag:tile.bTag];
[move setRollTypes:theRollTypes];
//check for kill move
NSArray *otherPlayerPositions = (NSArray *)[self otherPlayerLocations];
for (NSNumber *location in otherPlayerPositions) {
if (move.tileTag == [location intValue])
move.moveType = killMoveType;
}
//assing threat level
[self assignThreatLevel:move];
[moves addObject:move];
//Checking rolltypes, remove later
NSLog(#"%i = ",[number intValue]);
for (NSNumber *comb in theRollTypes) {
NSLog(#"%i",[comb intValue]);
}
NSLog(#"-----------");
CCLOG(#"move initThreat: %f",move.initThreat);
[move setRollTypes:theRollTypes];
[moves addObject:move];
//----------------------------------
}
}
}
}
}
Technically, methodB is called after methodA is finished. In methodA you are apparently doing things that are backgrounded, meaning that the method finishes while some tasks are performed on another thread (or scheduled on the runloop). There's nothing we can say about this unless you do share the inner workings of methodA.

UIPicker - Initial value problem

I have a UIPicker with 3 components containing numeric values, this allows the user to set the time in hh:mm:ss format. If I select the hour from the first component my UITextField looks like this: 10:(null):(null). The (null) can be removed when the user selects the appropriate mm and ss. But if the user just wants to enter 10 hours, I want the other values to be 00 instead of (null), without the user having to physically move the picker components.
Anyone have any ideas on this ?
Some code below, this question is related to the timePicker.
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if([pickerView isEqual: routePicker])
{
route.text = [[routeArray objectAtIndex:0] valueForKey:#"route_name"];
return [[routeArray objectAtIndex:row] valueForKey:#"route_name"];
}
else if([pickerView isEqual: activityPicker])
{
activity.text = [activityArray objectAtIndex:0];
return [activityArray objectAtIndex:row];
}
else if([pickerView isEqual: intensityPicker])
{
if ([intensityArray objectAtIndex:row]==#"Low")
numberIntensity=2;
else if ([intensityArray objectAtIndex:row]==#"Low-Medium")
numberIntensity=3.5;
else if ([intensityArray objectAtIndex:row]==#"Medium")
numberIntensity=5;
else if ([intensityArray objectAtIndex:row]==#"Medium-High")
numberIntensity=6.5;
else if ([intensityArray objectAtIndex:row]==#"High")
numberIntensity=8;
else numberIntensity=0;
intensity.text = [intensityArray objectAtIndex:0];
return [intensityArray objectAtIndex:row];
}
else if([pickerView isEqual: timePicker])
{
switch (component)
{
case 0:
return [hourArray objectAtIndex:row];
break;
case 1:
return [minuteArray objectAtIndex:row];
break;
case 2:
return [secondArray objectAtIndex:row];
break;
default:
return 0;
break;
}
}
else if([pickerView isEqual: distancePicker])
{
switch (component)
{
case 0:
return [distance1Array objectAtIndex:row];
break;
case 1:
return [distance2Array objectAtIndex:row];
break;
default:
return 0;
break;
}
}
else
{
return 0;
}
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if([pickerView isEqual: routePicker])
{
route.text = [[routeArray objectAtIndex:row] valueForKey:#"route_name"];
}
else if([pickerView isEqual: timePicker])
{
if (component == 0)
{
selectedHour = [hourArray objectAtIndex:row];
}
else if (component == 1)
{
selectedMinute = [minuteArray objectAtIndex:row];
}
else if (component == 2)
{
selectedSecond = [secondArray objectAtIndex:row];
}
time.text = [NSString stringWithFormat:#"%#:%#:%#", selectedHour, selectedMinute, selectedSecond];
}
else if([pickerView isEqual: distancePicker])
{
if (component == 0)
{
selectedDistance1 = [distance1Array objectAtIndex:row];
}
else if (component == 1)
{
selectedDistance2 = [minuteArray objectAtIndex:row];
}
distance.text = [NSString stringWithFormat:#"%#.%#", selectedDistance1, selectedDistance2];
}
else if([pickerView isEqual: activityPicker])
{
activity.text = [activityArray objectAtIndex:row];
}
else if([pickerView isEqual: intensityPicker])
{
intensity.text = [intensityArray objectAtIndex:row];
}
}
I think this line:
time.text = [NSString stringWithFormat:#"%#:%#:%#", selectedHour, selectedMinute, selectedSecond];
could be changed to:
time.text = [NSString stringWithFormat:#"%#:%#:%#",
(selectedHour ? selectedHour : #"00"),
(selectedMinute ? selectedMinute : #"00"),
(selectedSecond ? selectedSecond : #"00")];
This assumes that selectedHour, selectedMinute and selectedSecond are all initially nil outside of this code. I am away from my mac, so I haven't tested this.
Alternatively, you could initialize selectedHour, selectedMinute and selectedSecond to #"00".