-3

If I were to use the code by @abbood, and change the section "//do stuff with the cell" to incorporate an AlertView that pops up and the user can enter text to change the label of the cell. How would I do that, I tried the code below, but it only pops up the alert and doesn't do anything with what I entered in as the text to the alert. Note: cell has a UILabel named label that I want to change the text of. It appears that the NSLog I entered below to check the value of userEnterThisString is empty, the code has already executed before the text from getTitle is returned. I think I may need to delay the cell.label.text = userEnteredThisString until the AlertView is done, how would I do this? or any other ideas for the code are welcome. the question that generated this code is @ Long press gesture on UICollectionViewCell

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
    return;
}
CGPoint p = [gestureRecognizer locationInView:self.collectionView];

NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:p];
if (indexPath == nil){
    NSLog(@"couldn't find index path");
} else {
    // get the cell at indexPath (the one you long pressed)
    Cell *cell =
    [self.collectionView cellForItemAtIndexPath:indexPath];

    // do stuff with the cell
    UIAlertView *getTitle = [[UIAlertView alloc] initWithTitle:@"Add Search Keyword" message:nil delegate:self cancelButtonTitle:@"Add" otherButtonTitles:nil];
    getTitle.alertViewStyle = UIAlertViewStylePlainTextInput;
    NSString *  userEnterThisString = [[getTitle textFieldAtIndex:0] text];

// this part doesn't execute (wondering why it doesn't work?)
    cell.label.text = userEnterThisString;
    NSLog(userEnterThisString);
    [getTitle show];
    }
}
Community
  • 1
  • 1
  • 1
    What about using the `UIAlertViewDelegate` methods? Checking the text entered when the user click on a button? – Larme Aug 17 '14 at 10:33
  • I tried adding the UIAlertViewDelegate method to the .h file @interface CustomViewController : UICollectionViewController is that what you meant? or do you need something extra? I'm a bit of a novice here, so I appreciate your help! – user3768250 Aug 17 '14 at 15:29

1 Answers1

1

Hope it helps. Try this

 -(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
  {
if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
  return;
}
CGPoint p = [gestureRecognizer locationInView:self.collectionView];

NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:p];
if (indexPath == nil){
NSLog(@"couldn't find index path");
 } else {
// get the cell at indexPath (the one you long pressed)
Cell *cell =
[self.collectionView cellForItemAtIndexPath:indexPath];

UIAlertView *getTitle = [[UIAlertView alloc] initWithTitle:@"Add Search Keyword"
   message:nil delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil];
 getTitle.alertViewStyle = UIAlertViewStylePlainTextInput;
 [alert addButtonWithTitle:@"Add"];
[getTitle setTag : 99];
[getTitle show];
 }
    }
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex

{  
  if(alertView.Tag == 99)
    {
 if (buttonIndex == 0)
{
    //cancel
}
else if (buttonIndex == 1)

{
 NSString *  userEnterThisString = [[getTitle textFieldAtIndex:0] text];
 cell.label.text = userEnterThisString;
 NSLog(userEnterThisString);
}
}
}
Avis
  • 495
  • 4
  • 13
  • I see what you are getting at here, but it didn't work for me, I'm a novice with iOS coding, but the -(void)alertView is getting a flag I think because its within another function, code of function attached: – user3768250 Aug 17 '14 at 15:24
  • If you are declaring alert view function in any method,then declare void method after closing that method (which you are declaring alertview). and set tag to alertview and write tag in void method – Avis Aug 17 '14 at 17:38