UIAlertController *alertController = [UIAlertController alertControllerWithTitle:Localkey(@"Add the nickname") message:Localkey(@"Add the sensor's nickname") preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = Localkey(@"nickname");
}];
__weak typeof(self)weakSelf = self;
__weak typeof(alertController)weakAlert = alertController;
[alertController addAction:[UIAlertAction actionWithTitle:Localkey(@"Confirm") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UITextField *textField = weakAlert.textFields.firstObject;
if (textField.text.length < 1) {
[MBProgressHUD showError:Localkey(@"Nickname is empty, add failure")];
return ;
}else{
[[TcpSocketManger manager] sendCmd:0xa1 withDeviceID:weakSelf.deviceId andData:[PacketFactory change28BytesDataOfString:textField.text]];
}
NSLog(@"%@",textField.text);
}]];
//添加一个取消按钮
[alertController addAction:[UIAlertAction actionWithTitle:Localkey(@"Cancel") style:UIAlertActionStyleCancel handler:nil]];
//present出AlertView
[self presentViewController:alertController animated:true completion:nil];
注意红色背景,不这样写会造成页面不释放**************************