alertview弹出窗口不保存文本输入

时间:2014-08-17 10:30:33

标签: ios

如果我要使用@abbood的代码,并更改部分" //使用单元格"合并弹出的AlertView,用户可以输入文本来更改单元格的标签。我如何做到这一点,我尝试了下面的代码,但它只会弹出警报,并且不会对我作为警报文本输入的内容做任何事情。注意:单元格有一个名为UILabel的标签,我想更改其文本。看来我在下面输入的NSLog检查userEnterThisString的值是空的,代码已经在返回getTitle的文本之前执行了。我想我可能需要延迟cell.label.text = userEnteredThisString,直到完成AlertView,我该怎么做?或欢迎任何其他代码的想法。生成此代码的问题是@ 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];
    }
}

1 个答案:

答案 0 :(得分:1)

希望它有所帮助。试试这个

 -(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);
}
}
}