UIActionsheet not displaying all buttons on iPads running iOS7 - ios

Instances of UIActionSheet don't display correctly on iPads running iOS7. For some reason, they display the cancel button and leave off the last button. The same code works fine on iOS8.
You would expect the cancel button to be ignored, given that tapping elsewhere on the screen will close the action sheet. Does anyone know why this is the case?
Exactly the same code is used in both cases:
// Create UIActionSheet
let mapOptions = UIActionSheet(title: "Select map type", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: nil, otherButtonTitles: "Standard", "Hybrid", "Satellite")
// Display from bar button
mapOptions.showFromBarButtonItem(self.mapTypeButton, animated: true)

I was able to solve the problem by avoiding the default UIActionSheet constructor and adding buttons individually. This - and making sure the cancel button is added last - resolves the issue.
// Create the UIActionSheet
var mapOptions = UIActionSheet()
mapOptions.title = "Select map type"
mapOptions.addButtonWithTitle("Standard")
mapOptions.addButtonWithTitle("Hybrid")
mapOptions.addButtonWithTitle("Satellite")
// Add a cancel button, and set the button index at the same time
mapOptions.cancelButtonIndex = mapOptions.addButtonWithTitle("Cancel")
mapOptions.delegate = self
// Display the action sheet
mapOptions.showFromBarButtonItem(self.mapTypeButton, animated: true)

I'm facing the same problem with Swift. Your answer saved me.
I found you can still use this init method (with cancel button nil) to make your code more succinct.
let mapOptions = UIActionSheet(title: "Select map type", delegate: self, cancelButtonTitle: nil, destructiveButtonTitle: nil, otherButtonTitles: "Standard", "Hybrid", "Satellite")
and then with the line you provided
mapOptions.cancelButtonIndex = mapOptions.addButtonWithTitle("Cancel")
(I don't have enough reputation to add a comment so write here instead.)

Related

How to disable touch outside screen dismiss view using MDCAlertController Material Design - swift

I am new iOS programming and now am fascinated in using MaterialComponents which provide by google. Now i facing one problem in component named Dialog.
When the view has been pop up on screen when i touch outside that pop up view and then that view has been dismiss. I don't want that to happen in my app.
I don't want user to click outside popup view to dismiss that popup view. What i want i just want user to click on action button that i provide for user's choice then the view should be dismiss when click on that action button only.
Really glade that you help.
MDCAlertController is inherited from UIViewController.
So, in order to restrict user to click outside MDCAlertController you have to access its property named view and then superview?.subviews[0].isUserInteractionEnabled = false
I have completed one example using MDCAlertController
let alert = MDCAlertController(title: title, message: message)
alert.buttonTitleColor = UIColor(red:0.03, green:0.62, blue:0.09, alpha:1.0)
//MDCAlertControllerThemer.applyScheme(alertScheme, to: alert)
let okayAction = MDCAlertAction(title: "Okay") { (action) in
print("User click okay")
}
let cancelAction = MDCAlertAction(title: "Cancel", handler: nil)
alert.addAction(okayAction)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: {
// When the Dialog view has pop up on screen then just put this line of code when Dialog view has completed pop up.
alert.view.superview?.subviews[0].isUserInteractionEnabled = false
})
use this.
let alert = MDCAlertController(title: title, message: message)
alert.mdc_dialogPresentationController.dismissOnBackgroundTap = false
https://material.io/develop/ios/components/dialogs/api-docs/Categories/UIViewController_28MaterialDialogs_29.html
https://material.io/develop/ios/components/dialogs/api-docs/Classes/MDCDialogPresentationController.html#/c:objc(cs)MDCDialogPresentationController(py)dismissOnBackgroundTap

How to make message of UIAlertController copyable in swift2, iOS9?

I've a simple question: How can I make the message of an UIAlertController be selectable and copyable by the user?
The controller is initiated like so:
let alertController = UIAlertController(title: "Hello World", message: "Copy Me!", preferredStyle: .Alert)
and displayed like so:
presentViewController(alertController, animated: true, completion: nil)
Adam's correct that UIAlertController doesn't provide text selection functionality, so a traditional copy/paste solution isn't going to work. You could alternatively provide a button on your UIAlertController that copies a string to the pasteboard.
UIPasteboard.general.string = "Copy Me!"
It is not possible. UIAlertController has no such functionality. It was implemented with UILabel components, which don't support copying text. You are not allowed to subclass UIAlertController either. The only option is to implement your own controller instead.

Add an animation image per image inside a UIAlertController SWIFT

I have a function to display an UIAlertController. Below the title and the text, I would like to display an animation image per image. I have 13 images to loop.
func alertDownloadInProgress(text:String, sender:UIViewController) -> UIAlertController {
var alert = UIAlertController(title: "Alert", message: text, preferredStyle: UIAlertControllerStyle.Alert)
sender.presentViewController(alert, animated: true, completion: nil)
return alert
}
Is it possible to use an UIAlertController or I have to custom completely my pop up?
I don't know of a way to customize a UIAlertController like that; they only have three properties--title, message, preferredStyle, and textFields--none of which you can add a UIImageView to.
I would recommend creating a custom modal presentation controller. I actually just wrote an answer on how to do that. Of course you'll center your custom view instead of pinning it to the right, but that answer could at least help you get started with the wacky world of UIPresentationControllers.

how to make a slider appear when a button is pressed in Swift?

i'm making a draw application, and i want a slider to appear when the user taps on a button, for example to set the width of a line .
Can I just add an alert and put a slider in it ?
Using Swift 3.0, you can add a slider (or other object) to a UIAlertController subview. The following example is triggered by an #IBAction button to use and update the UserDefaults: Example of a UISider inside a UIAlertController
#IBAction func sliderButton(_ sender: AnyObject) {
//get the Slider values from UserDefaults
let defaultSliderValue = UserDefaults.standard.float(forKey: "sliderValue")
//create the Alert message with extra return spaces
let sliderAlert = UIAlertController(title: "Update Defaults", message: "Increase/Decrease the slider…\n\n\n\n\n\n", preferredStyle: .alert)
//create a Slider and fit within the extra message spaces
//add the Slider to a Subview of the sliderAlert
let slider = UISlider(frame:CGRect(x: 10, y: 100, width: 250, height: 80))
slider.minimumValue = 1
slider.maximumValue = 100
slider.value = defaultSliderValue
slider.isContinuous = true
slider.tintColor = UIColor.red
sliderAlert.view.addSubview(slider)
//OK button action
let sliderAction = UIAlertAction(title: "OK", style: .default, handler: { (result : UIAlertAction) -> Void in
UserDefaults.standard.set(slider.value, forKey: "sliderValue")
})
//Cancel button action
let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)
//Add buttons to sliderAlert
sliderAlert.addAction(sliderAction)
sliderAlert.addAction(cancelAction)
//present the sliderAlert message
self.present(sliderAlert, animated: true, completion: nil)
}
The subview can be positioned just about anywhere. To make room for the slider, I added a few extra line breaks in the message to open up some space. The slider will appear and disappear with the Alert message.
Welcome to Stack Overflow.
Your question is more of a basic programming question than a Swift question.
I don't think you can put a slider in an alert. Apple introduced the UIAlertController class recently, and their docs say you should us that rather than UIAlertView for new development.
A UIAlertController lets you create alerts and action sheets, and add actions. The actions are shown as buttons. There is also a facility for adding text fields. I don't think there is any facility for adding other view objects like sliders however.
The now-deprecated UIAlertView is also not set up for adding custom fields like sliders.
You could put the slider on your view somewhere and set it's hidden property to true, then in the buttons action method, set mySlider.hidden = false.
However it sounds like you want your slider to appear on top of your UI, then go away when the user is done with it.
You could create a view with a slider and a dismiss button on it, put it on top of your view controller's content view, and set it's hidden flag to true. When the user taps your button, set hidden = false and the view will appear on top, rather like an alert does. You'd set up constraints so the slider-containing view would be centered on the screen and sized appropriately.
toggle the hidden property.
slider.hidden = true//hide it
slider.hidden = false//show it

alertViewShouldEnableFirstOtherButton delegate method does not being called

I want do display an alert message and I am using iOS SDK 8.1 with XCode 6.1. I know that UIAlertView is deprecated; however, my app has to support also iOS 7 and I have to use UIAlertView if the app is running on an iOS 7 device. This alert view has a text field and two buttons where one of them is default cancel button. The other button should be disabled as long as text field is empty.
Here is my code:
class MyViewController : UIViewController, UIAlertViewDelegate {
var addRecipientAlertView:UIAlertView?
// Irrelevant code here
func performSomething(someValue:String) {
addRecipientAlertView = UIAlertView(title: "Title", message: "Enter full name of user, email of user or a free-form text", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "Add Recipient")
addRecipientAlertView!.alertViewStyle = UIAlertViewStyle.PlainTextInput
addRecipientAlertView!.accessibilityValue = someValue
// Text Field Settings
let textField:UITextField = addRecipientAlertView!.textFieldAtIndex(0)!
textField.placeholder = "Full Name, Email or Any Text"
textField.keyboardType = UIKeyboardType.EmailAddress
textField.clearButtonMode = UITextFieldViewMode.Always
addRecipientAlertView!.show()
}
}
func alertViewShouldEnableFirstOtherButton(alertView: UIAlertView) -> Bool {
return false
}
The problem is; whatever I have tried, the first other button was not disabled anyway. Finally I gave up trying to check the text of my text field and I have implemented the alertViewShouldEnableFirstOtherButton delegate method such that it always return false. However, the result has not changed and both of the buttons (named "Cancel" and "Add Recipient" in this example) are still enabled. What am I missing?
I had the same problem and I guess it's a bug in the SDK. The only working solution that I managed to come up with with was to implement an Objective-C class that showed the alert and served as its delegate.

Resources