How to dismiss action sheet when touch out side action sheet? - cocoa-touch

I used following method to use actionsheet programmatically with xcode 6, but I don't have idea to show and dismiss it, any one help me on this ..
var actionSheet = UIAlertController(title: "Title Text", message: "Message Text", preferredStyle: UIAlertControllerStyle.Alert)
actionSheet.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(actionSheet, animated: true, completion: nil)

This will allow users to cancel the alert. I'm not quite sure how to allow users to tap anywhere outside of the modal. You could possibly use a tap gesture recognizer on the base view, but I think the modal would probably steal those touches.
This should help to get you started on the right track.
let alertController = UIAlertController(title: "Select action", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
// First action
let firstAction = UIAlertAction(title: "First action", style: UIAlertActionStyle.Default, handler: {
(alertAction: UIAlertAction!) in
println("First action selected")
})
// Cancel action
let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: { (alertAction: UIAlertAction!) in
alertController.dismissViewControllerAnimated(true, completion: nil)
})
alertController.addAction(firstAction)
alertController.addAction(cancel)
self.presentViewController(alertController, animated: true, completion: nil)

Related

Turbolinks 5 iOS app link not pushing VisitableViewController onto stack

I've been playing around with Turbolinks 5 and I can't seem to get it to visit a new page correctly after clicking a link within my application. The app loads the new view as if it was replaced inside the webview and doesn't push a new view controller on to the stack like I would expect. It's as if it doesn't perform a Turblonks.visit
I'm running a rails 5.1 application with the Turbolinks 5 enabled. My link looks like this:
<%= link_to 'View', test_path(test_id), class: 'btn btn-secondary btn-block marginTop_short' %>
As you can see there is nothing special about this link!
My iOS app code is very basic:
import UIKit
import Turbolinks
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var navigationController = UINavigationController()
var session = Session()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window?.rootViewController = navigationController
startApplication()
return true
}
func startApplication() {
session.delegate = self
visit(URL: NSURL(string: "http://localhost:3000")!)
}
func visit(URL: NSURL) {
print("visiting", URL)
let visitableViewController = VisitableViewController(url: URL as URL)
navigationController.pushViewController(visitableViewController, animated: true)
session.visit(visitableViewController)
}
}
extension AppDelegate: SessionDelegate {
func session(_ session: Session, didProposeVisitToURL URL: URL, withAction action: Action) {
print("trying to visit", URL)
print("action", action)
visit(URL: URL as NSURL)
}
func session(_ session: Session, didFailRequestForVisitable visitable: Visitable, withError error: NSError) {
let alert = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
navigationController.present(alert, animated: true, completion: nil)
}
}
When I click a link it doesn't fire the func session(_ session: Session, didProposeVisitToURL URL: URL, withAction action: Action) callback.
Maybe it's more accurate to say when a link is clicked the webview is not responding to or createing a visit proposal?
What am I missing? Any help would be appreciated.
Let me know if any more detail is require or clarification.

iOS10: tapping an action from local notification does not bring app to the foreground

I am trying to implement the action from notification. And so far I am able to trigger the right delegate functions, but after tapping the app is not brought to the foreground.
Relevant code:
#available(iOS 10.0, *)
func registerCategory() -> Void{
print("register category")
let callNow = UNNotificationAction(identifier: "call", title: "Call now", options: [])
let clear = UNNotificationAction(identifier: "clear", title: "Clear", options: [])
let category : UNNotificationCategory = UNNotificationCategory.init(identifier: "IDENT123", actions: [callNow, clear], intentIdentifiers: [], options: [])
let center = UNUserNotificationCenter.currentNotificationCenter()
center.setNotificationCategories([category])
}
#available(iOS 10.0, *)
func scheduleNotification(event : String, interval: NSTimeInterval) {
print("schedule ", event)
let content = UNMutableNotificationContent()
content.title = event
content.body = "body"
content.categoryIdentifier = "CALLINNOTIFICATION"
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: interval, repeats: false)
let identifier = "id_"+event
let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger)
let center = UNUserNotificationCenter.currentNotificationCenter()
center.addNotificationRequest(request) { (error) in
}
}
#available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
print("willPresent")
completionHandler([.Badge, .Alert, .Sound])
}
#available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
let notification: UNNotification = response.notification
let UUID = notification.request.content.userInfo["UUID"] as! String
switch (response.actionIdentifier) {
case "COMPLETE":
UNUserNotificationCenter.currentNotificationCenter().removeDeliveredNotificationsWithIdentifiers([UUID])
case "CALLIN":
let call = Call()
CalendarController.sharedInstance.fetchMeetingByUUID(UUID, completion: { (thisMeeting) -> Void in
if(!CallIn.Yield(thisMeeting).ConferenceCallNumber.containsString("None")){
call._call(thisMeeting)
}else{
//will open detail view, in case that no number were detected
NSNotificationCenter.defaultCenter().postNotificationName("OpenDetailViewOfMeeting", object: self, userInfo: ["UUID":UUID])
}
})
UNUserNotificationCenter.currentNotificationCenter().removeDeliveredNotificationsWithIdentifiers([UUID])
default: // switch statements must be exhaustive - this condition should never be met
log.error("Error: unexpected notification action identifier: \(UUID)")
}
completionHandler()
}
I am able to hit the delegate function didReceiveNotificationResponse() with a breakpoint, and it does some actions that I put there, but not in a way that is expected (It has to start a device-call, instead it just dismisses notifications list, and nothing happens, however when I manually open the app again, the call starts as if there is no permission to open the app from notification).
I found out the reason myself, so this might be helpful to someone in the future. The answer turned out to be quite simple. When creating an action of the notification, there is this parameter: options. When you register category, you need to put it either way .Foreground or .Destructive like this:
func reisterCategory () {
let callNow = UNNotificationAction(identifier: NotificationActions.callNow.rawValue, title: "Call now", options: UNNotificationActionOptions.Foreground)
let clear = UNNotificationAction(identifier: NotificationActions.clear.rawValue, title: "Clear", options: UNNotificationActionOptions.Destructive)
let category = UNNotificationCategory.init(identifier: "NOTIFICATION", actions: [callNow, clear], intentIdentifiers: [], options: [])
let center = UNUserNotificationCenter.currentNotificationCenter()
center.setNotificationCategories([category])
}

UIActionSheet with swift

I created an action sheet, but the problem is that the delegate method is not called
myActionSheet = UIActionSheet()
myActionSheet.addButtonWithTitle("Add event")
myActionSheet.addButtonWithTitle("close")
myActionSheet.cancelButtonIndex = 1
myActionSheet.showInView(self.view)
/// UIActionSheetDelegate
func actionSheet(myActionSheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int){
if(myActionSheet.tag == 1){
if (buttonIndex == 0){
println("the index is 0")
}
}
}
I used another way which worked good with iOS 8 but did not work with iOS 7:
var ActionSheet = UIAlertController(title: "Add View", message: "", preferredStyle: UIAlertControllerStyle.ActionSheet)
ActionSheet.addAction(UIAlertAction(title: "Add event", style: UIAlertActionStyle.Default, handler:nil))
self.presentViewController(ActionSheet, animated: true, completion: nil)
Any idea to solve the problem?
UIActionSheet in swift language :-
Action Sheet with cancelButton and destructiveButton
set the UIActionSheetDelegate.
let actionSheet = UIActionSheet(title: "ActionSheet", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: "Done")
actionSheet.showInView(self.view)
Action Sheet with cancelButton , destructiveButton and otherButton
let actionSheet = UIActionSheet(title: "ActionSheet", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: "Done", otherButtonTitles: "Yes", "No")
actionSheet.showInView(self.view)
create the Action sheet function
func actionSheet(actionSheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int)
{
switch buttonIndex{
case 0:
NSLog("Done");
break;
case 1:
NSLog("Cancel");
break;
case 2:
NSLog("Yes");
break;
case 3:
NSLog("No");
break;
default:
NSLog("Default");
break;
//Some code here..
}
UIActionSheet is deprecated since iOS8, I would recommend using UIAlertController if you don't have to support version below:
private func presentSettingsActionSheet() {
let settingsActionSheet: UIAlertController = UIAlertController(title:nil, message:nil, preferredStyle:UIAlertControllerStyle.ActionSheet)
settingsActionSheet.addAction(UIAlertAction(title:"Send Feedback", style:UIAlertActionStyle.Default, handler:{ action in
self.presentFeedbackForm()
}))
settingsActionSheet.addAction(UIAlertAction(title:"Tell Me a Joke!", style:UIAlertActionStyle.Default, handler:{ action in
self.presentRandomJoke()
}))
settingsActionSheet.addAction(UIAlertAction(title:"Cancel", style:UIAlertActionStyle.Cancel, handler:nil))
presentViewController(settingsActionSheet, animated:true, completion:nil)
}
Here is what it looks like presented:
You never set the action sheet's delegate:
myActionSheet = UIActionSheet()
myActionSheet.delegate = self
Show Action sheet in Swift 4 / 5
#IBAction func showActionSheet(sender: AnyObject) {
let alert = UIAlertController(title: "Title", message: "Please Select an Option", preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Edit", style: .default , handler:{ (UIAlertAction)in
print("User click Edit button")
}))
alert.addAction(UIAlertAction(title: "Delete", style: .destructive , handler:{ (UIAlertAction)in
print("User click Delete button")
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:{ (UIAlertAction)in
print("User click Dismiss button")
}))
self.present(alert, animated: true, completion: {
print("completion block")
})
}
Updated for Swift 3 :
If you want to show/open UIActionSheet on Button click, used below simple and updated code in yourViewController:
Method definition:
func showPaymentModeActionSheet() {
// 1
let optionMenu = UIAlertController(title: nil, message: "Choose Payment Mode", preferredStyle: .actionSheet)
// 2
let fullAction = UIAlertAction(title: "FULL", style: .default, handler: {
(alert: UIAlertAction!) -> Void in
self.mPaymentModeTextField.text = "FULL"
})
let advanceAction = UIAlertAction(title: "ADVANCE", style: .default, handler: {
(alert: UIAlertAction!) -> Void in
self.mPaymentModeTextField.text = "ADVANCE"
})
//
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {
(alert: UIAlertAction!) -> Void in
})
// 4
optionMenu.addAction(fullAction)
optionMenu.addAction(advanceAction)
optionMenu.addAction(cancelAction)
// 5
self.present(optionMenu, animated: true, completion: nil)
}
Method call:
#IBAction func actionOnPaymentModeButton(_ sender: Any) {
// open action sheet
showPaymentModeActionSheet()
}

Ext.Msg.confirm() is not working on tap of image in sencha touch

I want to show confirmation alert message to user on tap of the image. in the listeners i have defined tap function, within that defined Ext.Msg.confirm(""). when i tap the image i am not getting confirmation message. so below of this one i have defined normal alert message "Hi". after this one i am able to see only normal alert message "HI". not getting confirmation message. on image tap i need to display confirmation message to user to confirm Yes/No. same code i have used for button handler function. Ext.Msg.confirm(""). message working fine. is image tap support Ext.Msg.confirm("") or not? if possible then how to achieve this one. any one tell me how to do.
Here is Mycode:
{
xtype: 'image',
src: 'Imagepath',
listeners: {
tap: function () {
// confiramation message
Ext.Msg.confirm(
"Confirmation",
"Are you sure you want to proceed?",
function (btn) {
if (btn === 'yes') {
Ext.Msg.alert("You have selected Yes", "");
}
else {
Ext.Msg.alert("You have selected NO", "");
}
},
this
);
//Normal alert message
alert("Hi");
}
}
I tried this code in Sencha Touch documentation live code preview and it works fine...
var img = Ext.create('Ext.Img', {
src: 'http://www.sencha.com/assets/images/sencha-avatar-64x64.png',
height: 64,
width: 64,
listeners: {
tap: function(){
Ext.Msg.confirm(
"Confirmation",
"Are you sure you want to proceed?",
function (btn) {
if (btn === 'yes') {
Ext.Msg.alert("You have selected Yes", "");
}
else {
Ext.Msg.alert("You have selected NO", "");
}
},
this
);}
}
});
Try to follow this way and hope it fixes your problem.

Adding a button and navigating between views

1.) I need to add 2 buttons, one below another. My working so far is demonstrated below; It only shows one button, but how can i add another button with a different button image below this button ?
2.) When the user clicks a button, i need to navigate to another screen. How can i do this ?
I need the equivalent of the following objective-c code ?
View1 *view1 = [[View1 alloc] initWithNibName:#"View1" bundle:nil];
[self.navigationController pushViewController:View1 animated:YES];
3.) How can i add a navigation Bar (equivalent to the navigation bar shown in iPhone)
The code for the 1st question;
{
items:[
{
xtype:'button',
text: 'Submit',
ui:'confirm',
handler: function(){
var values = Ext.getCmp('contactForm').getValues();
Ext.Ajax.request({
url: 'http://loonghd.com/service/',
failure: function (response) {
//do something
}, success: function (response) {
// do something
}
});
}
}
]
}
1) For getting two buttons one below the other, you can add two separate buttons (with different ui property) as childs of a form panel. I think, this is what you need.
Just like this,
....
....
items : [
{
xtype:'button',
text: 'Submit',
ui:'confirm', // makes the button color as green
handler: function(){
var values = Ext.getCmp('contactForm').getValues();
Ext.Ajax.request({
url: 'http://loonghd.com/service/',
failure: function (response) {
//do something
},
success: function (response) {
// do something
}
});
}
},
{
xtype:'button',
text:'Second button',
ui:'decline', // makes the button color as red.
listeners : {
tap : function() {
Ext.Msg.alert('You just clicked Second button');
}
}
}
]
....
....
2) 3) For your 2nd and 3rd question, navigationview is the solution.
Solution posted by M-x is great, but it's very advanced level example & also difficult to understand at first instance.
Here's an easy solution of navigatioview from Sencha Docs.
//create the navigation view and add it into the Ext.Viewport
var view = Ext.Viewport.add({
xtype: 'navigationview',
//we only give it one item by default, which will be the only item in the 'stack' when it loads
items: [
{
//items can have titles
title: 'Navigation View',
padding: 10,
//inside this first item we are going to add a button
items: [
{
xtype: 'button',
text: 'Push another view!',
handler: function() {
//when someone taps this button, it will push another view into stack
view.push({
//this one also has a title
title: 'Second View',
padding: 10,
//once again, this view has one button
items: [
{
xtype: 'button',
text: 'Pop this view!',
handler: function() {
//and when you press this button, it will pop the current view (this) out of the stack
view.pop();
}
}
]
});
}
}
]
}
]
});
Maybe a navigation view will work for you? It's the same idea but it's like starting with a UITableView:
http://docs.sencha.com/touch/2-0/#!/example/navigation-view
In the app/controller/Application.js, when you tap on a contact, the detail view gets pushed. All the source is in the examples directory.
onContactSelect: function(list, index, node, record) {
var editButton = this.getEditButton();
if (!this.showContact) {
this.showContact = Ext.create('AddressBook.view.contact.Show');
}
// Bind the record onto the show contact view
this.showContact.setRecord(record);
// Push the show contact view into the navigation view
this.getMain().push(this.showContact);
},