我有一个警报视图,显示用户必须输入的文本字段作为必填项。问题是如果用户点击"授权"没有进入它,alertview被解雇。我无法找到一种方法,向用户显示它是强制性的,而不会忽略警报视图。
代码:
func tableView(tableView:UITableView!,didSelectRowAtIndexPath indexPath:NSIndexPath!) {
print("You selected cell #\(self.empNameArr[indexPath.row])!")
let alertController = UIAlertController(title: "OD Authorise", message: "", preferredStyle: UIAlertControllerStyle.Alert)
let AUTHORISE = UIAlertAction(title: "AUTHORISE", style: UIAlertActionStyle.Default, handler: {
alert -> Void in
let firstTextField = alertController.textFields![3] as UITextField
print("<><><><><><>",firstTextField.text)
})
let DENY = UIAlertAction(title: "DENY", style: UIAlertActionStyle.Default, handler: {
(action : UIAlertAction!) -> Void in
})
let CANCEL = UIAlertAction(title: "CANCEL", style: UIAlertActionStyle.Default, handler: {
(action : UIAlertAction!) -> Void in
})
alertController.addTextFieldWithConfigurationHandler { (txtRemarks : UITextField!) -> Void in
txtRemarks.font = UIFont(name: (txtRemarks.font?.fontName)!, size: 11)
txtRemarks.text = " Employee Name :\(self.empNameArr[indexPath.row]) "
txtRemarks.userInteractionEnabled=false
txtRemarks.borderStyle = UITextBorderStyle.None
}
alertController.addTextFieldWithConfigurationHandler { (txtRemarks : UITextField!) -> Void in
txtRemarks.font = UIFont(name: (txtRemarks.font?.fontName)!, size: 11)
txtRemarks.text = " From Date :\(self.leavDateArr[indexPath.row]) "
txtRemarks.userInteractionEnabled=false
txtRemarks.borderStyle = UITextBorderStyle.None
}
alertController.addTextFieldWithConfigurationHandler { (txtRemarks : UITextField!) -> Void in
txtRemarks.font = UIFont(name: (txtRemarks.font?.fontName)!, size: 11)
txtRemarks.text = " To Date :\(self.ToDate[indexPath.row]) "
txtRemarks.userInteractionEnabled=false
txtRemarks.borderStyle = UITextBorderStyle.None
}
alertController.addTextFieldWithConfigurationHandler { (txtRemarks : UITextField!) -> Void in
txtRemarks.font = UIFont(name: (txtRemarks.font?.fontName)!, size: 11)
txtRemarks.text = " Leave reason :\(self.Reason[indexPath.row]) "
txtRemarks.userInteractionEnabled=false
txtRemarks.borderStyle = UITextBorderStyle.None
}
alertController.addTextFieldWithConfigurationHandler { (txtRemarks : UITextField!) -> Void in
txtRemarks.placeholder = "Enter Your Remarks"
txtRemarks.font = UIFont(name: (txtRemarks.font?.fontName)!, size: 15)
txtRemarks.userInteractionEnabled=true
txtRemarks.borderStyle = UITextBorderStyle.Line
txtRemarks.textAlignment = NSTextAlignment.Center
}
alertController.addAction(AUTHORISE)
alertController.addAction(DENY)
alertController.addAction(CANCEL)
self.presentViewController(alertController, animated: true, completion: nil)
}
答案 0 :(得分:1)
将您的班级设置为UIAlertViewDelegate,并将您的alrtView委托设置为您的班级。
然后这样做
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
NSString *name = [alertView textFieldAtIndex:0].text;
// Here check for validation. If the text is empty disable button or however you would like to handle it
}
}
答案 1 :(得分:1)
class ViewController: UIViewController,UITextFieldDelegate
{
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
var authorizeaction:UIAlertAction?
@IBAction func tapBtnaction(sender: AnyObject)
{
let titleStr = "title"
let messageStr = "message"
let alert = UIAlertController(title: titleStr, message: messageStr, preferredStyle: UIAlertControllerStyle.Alert)
let placeholderStr = "Enter your Remarks"
alert.addTextFieldWithConfigurationHandler({(textField: UITextField) in
textField.placeholder = placeholderStr
textField.addTarget(self, action: #selector(self.textChanged(_:)), forControlEvents: .EditingChanged)
})
let authorize = UIAlertAction(title: "Authorize", style: UIAlertActionStyle.Default, handler: { (_) -> Void in
})
let deny = UIAlertAction(title: "Deny", style: UIAlertActionStyle.Default, handler: { (_) -> Void in
let textfield = alert.textFields!.first!
//Do what you want with the textfield!
})
let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: { (_) -> Void in
let textfield = alert.textFields!.first!
//Do what you want with the textfield!
})
alert.addAction(cancel)
alert.addAction(authorize)
alert.addAction(deny)
//self.actionToEnable = action
authorizeaction = authorize
authorizeaction!.enabled = false
self.presentViewController(alert, animated: true, completion: nil)
}
func textChanged(sender:UITextField)
{
if sender.text?.characters.count > 0
{
authorizeaction?.enabled = true
}
else
{
authorizeaction?.enabled = false
}
}
}
Output:
修改强>
如果您不想启用或停用授权操作,则可以使用以下代码。
let authorize = UIAlertAction(title: "Authorize", style: UIAlertActionStyle.Default, handler: { (_) -> Void in
if let textfield : UITextField = alert.textFields![0]
{
if textfield.text?.characters.count == 0 {
//empty
self.presentViewController(alert, animated: true, completion: {
let tempAlert = UIAlertController(title: "Error", message: "Please Enter remarks", preferredStyle: UIAlertControllerStyle.Alert)
tempAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: { (action) in
}))
alert.presentViewController(tempAlert, animated: true, completion: nil)
})
} else {
//authorize
}
}
})
仅显示消息的替代选项是Toast。 https://github.com/scalessec/Toast
答案 2 :(得分:0)
您需要将手势识别器添加到在解雇之前触发的UIAlertController 为此,您可以参考以下答案: Prevent UIAlertController to dismiss
否则,您可以使用以下代码:
import ilog.concert.*;
import ilog.cplex.*;
import java.util.*;
public class SimpleLP {
public static void main(String[] args) {
Solve();
}
public static void Solve() {
int n = 4;
int m = 3;
double[] p = { 310.0, 380.0, 350.0, 285.0 };
double[] v = { 480.0, 650.0, 580.0, 390.0 };
double[] a = { 18.0, 15.0, 23.0, 12.0 };
double[] c = { 10.0, 16.0, 8.0 };
double[] V = { 6800.0, 8700.0, 5300.0 };
try {
IloCplex model = new IloCplex();
IloNumVar[][] x = new IloNumVar[n][];
for (int i = 0; i < n; i++) {
x[i] = model.numVarArray(m, 0, Double.MAX_VALUE);
}
IloNumVar y = model.numVar(0, Double.MAX_VALUE);
IloLinearNumExpr obj = model.linearNumExpr();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
obj.addTerm(p[i], x[i][j]);
}
}
model.addMaximize(obj);
for (int i = 0; i < n; i++) {
model.addLe(model.sum(x[i]), a[i]);
}
List<IloRange> Constraints = new ArrayList<IloRange>();
IloLinearNumExpr [] usedWeightCap = new IloLinearNumExpr[m];
IloLinearNumExpr [] usedVolumeCap = new IloLinearNumExpr[m];
IloLinearNumExpr [] proportionalWeight = new IloLinearNumExpr[m];
for (int j = 0; j < m; j++) {
usedWeightCap[j] = model.linearNumExpr();
usedVolumeCap[j] = model.linearNumExpr();
proportionalWeight[j] = model.linearNumExpr();
for (int i = 0; i < n; i++) {
usedWeightCap[j].addTerm(1.0, x[i][j]);
usedVolumeCap[j].addTerm(v[i], x[i][j]);
proportionalWeight[j].addTerm(1/c[j], x[i][j]);
}
}
for (int j = 0; j < m; j++) {
Constraints.add(model.addLe(usedWeightCap[j], c[j]));
Constraints.add(model.addLe(usedVolumeCap[j], V[j]));
//model.addEq(model.prod(1/c[j], usedWeightCap[j]), y);
//Constraints.add(model.addEq(proportionalWeight[j], y)); /*This is my problem that I cannot add this constraint into the list*/
model.addEq(proportionalWeight[j], y);
}
if (model.solve()) {
System.out.println("Objective Value: "+ model.getObjValue());
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.println("x*["+(i+1)+"]"+"["+(j+1)+"] : "+model.getValue(x[i][j]));
}
}
System.out.println("y* : "+ model.getValue(y));
} else {
System.out.println("Problem not solved :(");
}
model.end();
} catch (IloException ex) {
ex.printStackTrace();
}
}
}