在不知道其余代码是什么样子的情况下,您可以在 ViewController.m 文件中尝试此操作。它设置标签的文本,并在按下“确定”按钮时用警报视图中的文本替换可变数组中的对象。
#import "ViewController.h"
@interface ViewController () <UIAlertViewDelegate>
@property (strong,nonatomic) NSMutableArray *mutArray;
@property (weak,nonatomic) IBOutlet UILabel *label;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *array = @[@"item1",@"item2",@"item3"];
self.mutArray = [[NSMutableArray alloc] init];
self.mutArray = [array mutableCopy];
}
- (IBAction)showAlert:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"My Alert Example"
message:@"message here"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK",nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
int selected = 1;
if (buttonIndex != alertView.cancelButtonIndex) {
NSLog(@"ok button");
UITextField *textField = [alertView textFieldAtIndex:0];
self.label.text = textField.text;
[self.mutArray replaceObjectAtIndex:selected withObject:textField.text];
NSLog(@"mutArray is %@",self.mutArray);
} else {
NSLog(@"cancel button");
}
}
@end
由于看起来您正在使用 UITableView,因此在您的情况下,您将int selected = indexPath.row
使用int selected = 1
.