Swift - How can I add an event listener for cancelling an email dialog? - swift

I'm using text attribute to detect an email. It's working fine but what I need is a listener if the user did cancel sending email or not.
How can I achieve that with Xcode?
Thanks a lot

You need to provide more information , photos of your problem.
And possible a sample code.
If you can detect a result of the email being added properly then you should handle the nil.
You didn't specify if the cancel option is a button or an .addAction method. In both cases you can use a Notification / Observers.
class StepCounter {
var totalSteps: Int = 0 {
willSet(newTotalSteps) {
println("About to set totalSteps to \(newTotalSteps)")
}
didSet {
if totalSteps > oldValue {
println("Added \(totalSteps - oldValue) steps")
}
}
}
}

Related

ActionScript3 How to increment score when working with classes

I would like to increment the value of a TextField by one every time an event listener in another class is activated. This is proving very difficult because the event listener can only be triggered once the Timer in the main class reaches completion.
I have tried using a getter method on a Boolean to ensure that the event listener in a different class has been triggered. This has proven unsucsessful however because of the Timer which is only triggered once on completion.
I am a begginer to AS3 and so any help would be greately appreciated. Thank You
THIS IS THE CODE FOR THE MAIN CLASS
public function t1Finish(e:TimerEvent):void {
ranNum2 = randomNum1(0, 11);
holes[ranNum2].sendUp();
setCounter();
}
public function setCounter():void {
if(holes[ranNum2].returnHasHit() == true) {
counter1++;
txt1instance.text = counter1.toString();
} else {
txt1instance.text = counter1.toString();
}
}
THIS IS THE CODE FOR THE OTHER CLASS
public function click1(e:MouseEvent):void {
this.gotoAndPlay(32);
hasHit = true;
}
public function returnHasHit():Boolean {
return hasHit;
}
You need to properly arrange your app hierarchy. Your case is not exactly problematic: you have main class that wants to capture some events occurring in certain subservient objects.
// That HOLE class.
// I assume it is an inner event handler, not a public
// interface, thus there's no need for it to be public.
private function click1(e:MouseEvent):void
{
// Normally you don't need to explicitly state "this".
gotoAndPlay(32);
// Produce a custom event.
dispatchEvent(new Event(Event.OPEN));
}
Then, you need to catch that event in the main class.
// The MAIN class.
// You need to subscribe for that custom event somewhere.
private function subscribeAll():void
{
for each (var aHole:MovieClip in holes)
{
aHole.addEventListener(Event.OPEN, onOpen);
}
}
// The custom event handler.
private function onOpen(e:Event):void
{
// If you want to know which hole dispatched this event.
var aHole:MovieClip = e.target as MovieClip;
// Here you might want to put any additional criteria
// to check if you want to accept this event.
// This handler is invoked ONLY if that "click1" handler was invoked
// on any of the holes, so there's no need to check "wasHit" interface.
counter1++;
txt1instance.text = counter1.toString();
}

Best way to count length of UITextInput text value in Swift?

I have two UITextInput controls that I want to count the number of characters in.
For context, I have two inputs: one for email address and one for password. I also have a “Login” button. The button is inactive by default, but as soon as at least one character is entered into both inputs, I’ll programatically enable the button. This is to prevent login attempts until the user has entered a value in both fields.
So far, I’m using this approach:
if count(emailInput.text) > 0 && count(passwordInput.text) > 0 {
// Enable button
} else {
// Disable button
}
Is using the count() function acceptable? Or is there a better way? As I recall there are some gotchas with checking the length of strings in iOS/Swift.
For me personally the following code has worked fine in past.
if (!emailInput.text.isEmpty && !passwordInput.text.isEmpty) {
// enable button
} else {
// disable button
}
if((emailInput.text!.characters.count) > 0 && (passwordInput.text!.characters.count) > 0) {
// Enable button
} else {
// Disable button
}
Swift 4
emailInput.text!.count
Swift 2
self.emailInput.text!.characters.count
but if you need to do something more elegant you can create an extension like this
Nowadays, after putting count directly on text property my guess is to use this directly without extension, but if you like to isolate this sort of things, go ahead!.
extension UITextField {
var count:Int {
get{
return emailInput.text?.count ?? 0
}
}
}

Titanium_iphone contacts

I am trying to list contacts with Titanium. WOrks on android device, but on iphone simulator doesn't return anything.
var contacts= Ti.Contacts.getAllPeople();
alert("contacts.length");
returns 0. I am not sure what am i missing here.
Make sure that you have contacts created on your iOS simulator:
Use Home button (CMD+Shift+H) to go to dashboard.
Open up Contacts app
Create few contacts which you want to retrieve in your app.
Also before you call Ti.Contacts.getAllPeople() you have to request authorisation to contact list. Try code below:
function processContacts() {
Ti.API.info('authorized');
var contacts = Ti.Contacts.getAllPeople();
Ti.API.info(contacts.length);
}
if (Ti.Contacts.contactsAuthorization == Ti.Contacts.AUTHORIZATION_AUTHORIZED){
processContacts();
} else if (Ti.Contacts.contactsAuthorization == Ti.Contacts.AUTHORIZATION_UNKNOWN){
Ti.Contacts.requestAuthorization(function(e){
if (e.success) {
processContacts();
} else {
Ti.API.info('disallowed');
}
});
} else {
Ti.API.info('disallowed');
}
One last thing, in your code you wrote: alert("contacts.length") which will always display dialog view with "contacts.length" as a string, not value. To call it properly you should write: alert(contacts.length) without double quotes.

How to recognise whether form is modified or not in extjs4

I am working in extjs4. I am having form with multiple textboxes,radio buttons and some more elements.Suppose i clicked on modify form. If user made some editing in form,then i want to display message box window in order to ask confirmation of saving of changes that user has made.If user closes form without any modifications,then that messagebox should not be appear.I was trying to use dirtychange event of form as-
me.getForm().on('dirtychange', function(form, isDirty) {
console.log(form.events);
if( isDirty ) {
isChanged=true;
}
else {
isChanged=false;
}
});
But its setting isChanged variable to always true though form is not modified.
So how to identify whether form is modified or not so that i can display confirmation message box.
You can use Ext.data.Model-property-dirty to determine if a record has been modified.
form.getRecord().dirty
If your record is dirty without you making any modifications, you can try this to find which field is dirty.
if (form.isDirty()) {
var fields = form.getForm().getFields().items;
for (var i=0; i<fields.length; i++) {
if (fields[i].isDirty()) {
if (Ext.isDefined(fields[i].name)) {
alert(fields[i].name);
}
}
}
}

alert handling in ui automation iphone app unable to cancel the option

system.logElementTree();
var target = UIATarget.localTarget();
target.onAlert = function onAlert(alert) {
UIALogger.logDebug("There was an alert!");
target.onAlert.buttons()["No"].tap({x:164,y:278});
return false;
even though no option is clicked systen not performing any action
Can anyone please help me ...
Instead of BamboOS suggestion which loops through various positions, you can try this inside your onAlert function:
alert.tapWithOptions({tapOffset:{x:0.5, y:0.6}});
This tap targets the middle of the UIAAlert (x:0.5) and 60% from top to bottom (y:0.6). This works when there is only one button. You have multiple buttons, then you have to changed the value of x. This works for me.
I just published a blog post regarding UI Automation and dealing with alerts:
http://www.conduce.net/Blog.aspx?f=Automated-Test-of-iPad-Apps
Basically following alert handler worked for me:
UIATarget.onAlert = function onAlert(alert){
var name = alert.name();
UIALogger.logMessage("alert "+name+" encountered");
if(name == "errorAlert"){
var positionX = 500;
for(var positionY=300; positionY<600;positionY+=10){
target.tap({x:positionX,y:positionY});
}
return true;
}
return false;
}
I would either use the "cancelButton" or "defaultButton" methods when handling alerts.