alertview 弹出不保存文本输入
Posted
技术标签:
【中文标题】alertview 弹出不保存文本输入【英文标题】:alertview pop up doesn't save text input 【发布时间】: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 希望对您有所帮助。试试这个
-(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 方法中写入标签以上是关于alertview 弹出不保存文本输入的主要内容,如果未能解决你的问题,请参考以下文章