How to implement a pop-up dialog box in iOS?
Yup, a UIAlertView
is probably what you're looking for. Here's an example:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No network connection"
message:@"You must be connected to the internet to use this app."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
If you want to do something more fancy, say display a custom UI in your UIAlertView
, you can subclass UIAlertView
and put in custom UI components in the init
method. If you want to respond to a button press after a UIAlertView
appears, you can set the delegate
above and implement the - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
method.
You might also want to look at the UIActionSheet
.
What's a simple way to get a text input popup dialog box on an iPhone
In iOS 5 there is a new and easy way to this. I'm not sure if the implementation is fully complete yet as it's not a gracious as, say, a UITableViewCell
, but it should definitly do the trick as it is now standard supported in the iOS API. You will not need a private API for this.
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"This is an example alert!" delegate:self cancelButtonTitle:@"Hide" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
[alert release];
This renders an alertView like this (screenshot taken from the iPhone 5.0 simulator in XCode 4.2):
When pressing any buttons, the regular delegate methods will be called and you can extract the textInput there like so:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@"Entered: %@",[[alertView textFieldAtIndex:0] text]);
}
Here I just NSLog the results that were entered. In production code, you should probably keep a pointer to your alertView as a global variable or use the alertView tag to check if the delegate function was called by the appropriate UIAlertView
but for this example this should be okay.
You should check out the UIAlertView API and you'll see there are some more styles defined.
Hope this helped!
-- EDIT --
I was playing around with the alertView a little and I suppose it needs no announcement that it's perfectly possible to edit the textField as desired: you can create a reference to the UITextField
and edit it as normal (programmatically).
Doing this I constructed an alertView as you specified in your original question. Better late than never, right :-)?
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Hello!" message:@"Please enter your name:" delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeNumberPad;
alertTextField.placeholder = @"Enter your name";
[alert show];
[alert release];
This produces this alert:
You can use the same delegate method as I poster earlier to process the result from the input. I'm not sure if you can prevent the UIAlertView
from dismissing though (there is no shouldDismiss
delegate function AFAIK) so I suppose if the user input is invalid, you have to put up a new alert (or just reshow
this one) until correct input was entered.
Have fun!
How to implement a navigation bar for popup dialog in IOS?
I would say, it depends on your dialog box requirement
You mentioned, that you need back and next button in navigation bar, as I understand, you simply want to manipulate the data in dialog box or do some operation using it.
If you would want series of view controllers inside dialog box, pushing after each other, then only navigation controller would be good to use in your dialog box
Otherwise, using it simply for navigation bar would be overkill.
UINavigationControlker is a Container Controller and is optimized for keeping view controllers in stack and pop them up.
I suggest, go for custom UIView from xib or if dialog box have much code to handle than, implement your dialog box in view controller, and add it as child view controller.
How to create a small popup in iOS?
UIPopoverController does this, but it's limited to iPad only. If you need this on iPhone, just implement a custom UIView subclass. Then when you want to show your popover, instantiate the subclass and add it to your view, maybe with some animation.
Swift: How to pass information to a pop up view?
Just declare a new variable on PopUpViewController
called data
with type Array
.
After this, when you are creating your viewController, you can just pass it to the controller. After that it is just a simple tableView
implementation in PopUpViewController
to display the data.
Extend PopUpViewController
with data
parameter.
import UIKit
class PopUpViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var tableView: UITableView!
// Data variable
public var data: [String] = []
}
Add the data upon calling showCustomDialog()
function
// Create a custom view controller
let PopUpVC = PopUpViewController(nibName: "PopUpViewController", bundle: nil)
// Assign the data
PopUpVC.data = popUpArray
How can I get following pop up text field in Swift?
It's going to look something like this:
// create the actual alert controller view that will be the pop-up
let alertController = UIAlertController(title: "New Folder", message: "name this folder", preferredStyle: .alert)
alertController.addTextField { (textField) in
// configure the properties of the text field
textField.placeholder = "Name"
}
// add the buttons/actions to the view controller
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
let saveAction = UIAlertAction(title: "Save", style: .default) { _ in
// this code runs when the user hits the "save" button
let inputName = alertController.textFields![0].text
print(inputName)
}
alertController.addAction(cancelAction)
alertController.addAction(saveAction)
present(alertController, animated: true, completion: nil)
Let me know if this helps.
How to create a custom pop up view with swift?
you'd create an custom UIView
with all respected object needed, from your Controller's viewDidLoad() you'll hide it.
customView.hidden = true
Whenever your user wants to perform some action or task, you unhide it and once the user finished then hide it again or remove from the superView.
customView.hidden = false
Below there is some code to help you start
private var customView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
customView.hidden = true
}
private func loadCustomViewIntoController() {
let customViewFrame = CGRect(x: 0, y: 0, witdh: view.frame.width, height: view.frame.height - 200)
customView = UIView(frame: customViewFrame)
view.addSubview(customView)
customView.hidden = false
// any other objects should be tied to this view as superView
// for example adding this okayButton
let okayButtonFrame = CGRect(x: 40, y: 100, width: 50, height: 50)
let okayButton = UIButton(frame: okayButtonFrame )
// here we are adding the button its superView
customView.addSubview(okayButton)
okayButton.addTarget(self, action: #selector(self.didPressButtonFromCustomView:), forControlEvents:.TouchUpInside)
}
func didPressButtonFromCustomView(sender:UIButton) {
// do whatever you want
// make view disappears again, or remove from its superview
}
@IBAction func rateButton(sender:UIBarButtonItem) {
// this barButton is located at the top of your tableview navigation bar
// when it pressed make sure you remove any other activities that were on the screen, for example dismiss a keyboard
loadCustomViewIntoController()
}
Check it out this github project, It's closed to be production ready, it gives you a better way to deal (present and dismiss) with UIViews
If you only want the player's name then use a
UIAlertController
containing a textfield
Related Topics
Finish All Asynchronous Requests Before Loading Data
Prevent Segue in Prepareforsegue Method
In Swift, How to Avoid Both Optionals and Nil Object References
Change Tab Bar Item Selected Color in a Storyboard
Loading an Image into Uiimage Asynchronously
iOS 8 Enabled Device Not Receiving Push Notifications After Code Update
Uitableview - Change Section Header Color
How to Check If Uilabel Is Truncated
Custom Uitableview Section Index
Custom Annotation View for Userlocation Not Moving the Mapview
Change Language in the App Programmatically in iOS
Where's the Difference Between Setobject:Forkey: and Setvalue:Forkey: in Nsmutabledictionary
Insert CSS into Loaded HTML in Uiwebview/Wkwebview
How to Get the Console Logs from the iOS Simulator
How to Rotate Text for Uibutton and Uilabel in Swift
How to Create an Umbrella Framework in iOS Sdk