How to Insert UIlabel After UItextfield in UIalertcontroller

How to insert UILabel after UITextField in UIAlertController

A UIAlertController sub-classes from UIViewController. Because of this, a UIAlertController contains a view which can be modified. So, to add a label to the alert, you could do this:

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
label.text = "Text"
alert.view.addSubview(label)

However, you should keep in mind what is said in the docs:

The UIAlertController class is intended to be used as-is and does not
support subclassing. The view hierarchy for this class is private and
must not be modified.

UILabel in UIAlertController or UITextField alike UILabel

I believe you want to add a custom Label in UIAlert for good UI Look.

The best way to do this either programming a custom UIView such that it feels and behaves like an UIAlertView or use one of the following libraries from github.

https://github.com/nealyoung/NYAlertViewController

https://github.com/sberrevoets/SDCAlertView

How to add TextField to UIAlertController in Swift

Swift 5.1

alert.addTextField { (textField) in
textField.placeholder = "Enter First Name"
}

Use this code, I am running this code in my app successfully.

@IBAction func addButtonClicked(sender : AnyObject){
let alertController = UIAlertController(title: "Add New Name", message: "", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addTextFieldWithConfigurationHandler { (textField : UITextField!) -> Void in
textField.placeholder = "Enter Second Name"
}
let saveAction = UIAlertAction(title: "Save", style: UIAlertActionStyle.Default, handler: { alert -> Void in
let firstTextField = alertController.textFields![0] as UITextField
let secondTextField = alertController.textFields![1] as UITextField
})
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: {
(action : UIAlertAction!) -> Void in })
alertController.addTextFieldWithConfigurationHandler { (textField : UITextField!) -> Void in
textField.placeholder = "Enter First Name"
}

alertController.addAction(saveAction)
alertController.addAction(cancelAction)

self.presentViewController(alertController, animated: true, completion: nil)
}

Edited: Swift 3.0 version

@IBAction func addButtonClicked(_ sender: UIButton){
let alertController = UIAlertController(title: "Add New Name", message: "", preferredStyle: .alert)
alertController.addTextField { (textField : UITextField!) -> Void in
textField.placeholder = "Enter Second Name"
}
let saveAction = UIAlertAction(title: "Save", style: .default, handler: { alert -> Void in
let firstTextField = alertController.textFields![0] as UITextField
let secondTextField = alertController.textFields![1] as UITextField
print("firstName \(firstTextField.text), secondName \(secondTextField.text)")
})
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: { (action : UIAlertAction!) -> Void in })
alertController.addTextField { (textField : UITextField!) -> Void in
textField.placeholder = "Enter First Name"
}

alertController.addAction(saveAction)
alertController.addAction(cancelAction)

self.present(alertController, animated: true, completion: nil)
}

show the textField in the alertController in swift

Code for Adding textField in UIALertController :

let alertController = UIAlertController(title: "PlainTextStyle", message: "PlainTextStyle AlertView.", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addTextFieldWithConfigurationHandler { (textField : UITextField) -> Void in
textField.placeholder = "Login"
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (result : UIAlertAction) -> Void in
print("Cancel")
}
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (result : UIAlertAction) -> Void in
print(alertController.textFields?.first?.text)
}
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.presentViewController(alertController, animated: true, completion: nil)

Update Swift 3.0

 let alertController = UIAlertController(title: "PlainTextStyle", message: "PlainTextStyle AlertView.", preferredStyle: UIAlertControllerStyle.alert)
alertController.addTextField { (textField : UITextField) -> Void in
textField.placeholder = "Login"
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) { (result : UIAlertAction) -> Void in
}
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
}
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)

TextField in UIALertController :

Sample Image

Multiline editable text UITextview inside UIAlertController?

This is good appraoch ...

func popUpController()
{

let alertController = UIAlertController(title: "\n\n\n\n\n\n", message: nil, preferredStyle: UIAlertController.Style.actionSheet)

let margin:CGFloat = 8.0
let rect = CGRect(x: margin, y: margin, width: alertController.view.bounds.size.width - margin * 4.0, height: 100.0)
let customView = UITextView(frame: rect)

customView.backgroundColor = UIColor.clear
customView.font = UIFont(name: "Helvetica", size: 15)

// customView.backgroundColor = UIColor.greenColor()
alertController.view.addSubview(customView)

let somethingAction = UIAlertAction(title: "Something", style: UIAlertAction.Style.default, handler: {(alert: UIAlertAction!) in print("something")

print(customView.text)

})

let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: {(alert: UIAlertAction!) in print("cancel")})

alertController.addAction(somethingAction)
alertController.addAction(cancelAction)

self.present(alertController, animated: true, completion:{})

}

How to pass an input from a UITextField to a UILabel in different controllers?

In your prepareForSegue method, store a weak reference to the sender (which will be your UITableViewCell) on your modal view controller. Assuming you have the UILabel exposed on your UITableViewCell you'll be able to set it directly. For example:

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
UITableViewCell *cell = (UITableViewCell *) sender;
UIViewController* controller = segue.destinationViewController;
controller.callingCell = cell;
}

Assuming on your modal view you have the following property:

@property(nonatomic, weak) UITableViewCell* callingCell;

You can then say:

self.callingCell.desiredTextField.text = @"Text";

Instead of keeping a reference to the UITableViewCell itself, you could also create a protocol that only exposes the ability to set the text field you desire. Then your UITableViewCell would conform to that. For example:

@protocol SettableLabel <NSObject>

-(void) setText:(NSString*) text;

@end

Then your UITableViewCell would conform to that protocol and have the method implemented:

-(void) setText:(NSString*) text {
self.desiredTextField.text = text;
}

Then on your modal view you'd have the property:

@property(nonatomic, weak) id<SettableLabel>* callingCell;

and would just call setText when you need to. This may be a better design but I leave that decision to you.

Change UILabel Text via UIAlertAction

First of all, this code makes no sense:

if statusDVR.text !=  "STOP" {
self.present(recordalert, animated:true, completion: nil)
if statusDVR.text != "STOP" {
self.present(statechangealert, animated:true, completion: nil)

That code tries to present two alerts at the same time. That is illegal.

I think you think that when you say

self.present(recordalert, animated:true, completion: nil)

...your code magically comes to a stop while the user interacts with the alert and then proceeds after the user dismisses the alert. That’s not the case. Your code never magically stops; it just keeps right on going.


As for the actual question you asked about, the problem is simply that what you're doing is illegal:

    playalert.addAction(stopAction)
playalert.addAction(returnAction)
recordalert.addAction(stopAction)
recordalert.addAction(returnAction)

No! You cannot take one UIAlertAction and somehow magically "share" it between two different UIAlertControllers. Every UIAlertController needs UIActions that belong to it alone. In other words, do not call addAction on the same UIAlertAction twice.

Just to demonstrate more simply, try running this code:

let action1 = UIAlertAction(title: "Test", style: .default) {
_ in print("test")
}
let alert1 = UIAlertController(title: "Hello", message: nil, preferredStyle: .alert)
let alert2 = UIAlertController(title: "Hello2", message: nil, preferredStyle: .alert)
alert1.addAction(action1)
alert2.addAction(action1)
self.present(alert1, animated: true, completion: nil)

The alert appears, you tap the Test button, the alert disappears — but nothing prints in the console. Now comment out the next to last line:

// alert2.addAction(action1)

... and run the code again. This time, when you tap Test, "test" appears in the console.

Is it possible to insert two text fields in one row inside a UIAlertController

So this is my piece of Swift code. It works for me. I hope it will work for somebody else too.

class NumbersViewController: UIViewController, UITextFieldDelegate {

var codeTextField: UITextField?;
var numberTextField: UITextField?;

override func viewDidLoad() {
super.viewDidLoad();
}

override func viewDidAppear(animated: Bool) {
self.askForNumber();
}

func askForNumber(prefix: String = ""){
var title = "";
var message = "Enter your mobile number\n\n\n";
var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert);
alert.modalInPopover = true;

func handleOk(alertView: UIAlertAction!){

var code: String = self.codeTextField!.text;
var number: String = self.numberTextField!.text;

}

var inputFrame = CGRectMake(0, 70, 270, 25);
var inputView: UIView = UIView(frame: inputFrame);

var prefixFrame = CGRectMake(7, 5, 10, 10);
var prefixLabel: UILabel = UILabel(frame: prefixFrame);
prefixLabel.text = "+";

var codeFrame = CGRectMake(20, 0, 65, 25);
var countryCodeTextField: UITextField = UITextField(frame: codeFrame);
countryCodeTextField.placeholder = "Code";
countryCodeTextField.borderStyle = UITextBorderStyle.RoundedRect;
countryCodeTextField.keyboardType = UIKeyboardType.DecimalPad;

var numberFrame = CGRectMake(90, 0, 170, 25);
var myNumberTextField: UITextField = UITextField(frame: numberFrame);
myNumberTextField.placeholder = "Number - digits only";
myNumberTextField.borderStyle = UITextBorderStyle.RoundedRect;
myNumberTextField.keyboardType = UIKeyboardType.DecimalPad;

self.codeTextField = countryCodeTextField;
self.numberTextField = myNumberTextField;

inputView.addSubview(prefixLabel);
inputView.addSubview(self.codeTextField!);
inputView.addSubview(self.numberTextField!);

alert.view.addSubview(inputView);

alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler:handleOk));

self.presentViewController(alert, animated: true, completion: nil);

} }


Related Topics



Leave a reply



Submit