【问题标题】:alertview pop up doesn't save text inputalertview 弹出不保存文本输入
【发布时间】:2014-08-17 10:30:32
【问题描述】:

如果我要使用@abbood 的代码,并更改“//do stuff with the cell”部分以合并弹出的AlertView,用户可以输入文本来更改单元格的标签。我将如何做到这一点,我尝试了下面的代码,但它只会弹出警报,并且不会对我作为警报文本输入的内容做任何事情。注意:单元格有一个名为 label 的 UILabel,我想更改它的文本。我在下面输入的用于检查 userEnterThisString 值的 NSLog 似乎是空的,代码在 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];
    }
}

【问题讨论】:

  • 使用UIAlertViewDelegate 方法怎么样?检查用户单击按钮时输入的文本?
  • 我尝试将 UIAlertViewDelegate 方法添加到 .h 文件 @interface CustomViewController : UICollectionViewController 是你的意思吗?或者你需要额外的东西吗?我在这里有点新手,所以非常感谢您的帮助!

标签: ios


【解决方案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);
}
}
}

【讨论】:

  • 我知道你在这里得到了什么,但它对我不起作用,我是 iOS 编码的新手,但我认为 -(void)alertView 得到了一个标志,因为它在另一个函数中,附加函数代码:
  • 如果您在任何方法中声明警报视图功能,则在关闭该方法(您正在声明警报视图)后声明 void 方法。并将标签设置为 alertview 并在 void 方法中写入标签
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-19
  • 1970-01-01
相关资源
最近更新 更多