I'm trying to trigger an alert with an if statement. but the code im using wont do what i want.
This is one attempt at triggering the alert
-(Void)ShowAlert
if (mainInt == 120) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Title" message:#"Message"
delegate:nil cancelButtonTitle:#"Dismiss"otherButtonTitles:nil, nil];
[alert show];
}
i have declared 'ShowAlert' in the .h file as -(void)ShowAlert just under my -(IBAction) declares.
But still no alert when ran in simulator. Any help with this would be greatly appreciated!!
THANKS
(I'm am using Xcode 4.6.2)
Maybe if (mainInt == 120) is never true.
I would try with an ELSE if i were you. And log whole process about what's going on like this:
-(void)ShowAlert{
if (mainInt == 120){
NSLog(#"TRUE i should see the alertview! , mainInt = %i",mainInt);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Title" message:#"Message"
delegate:nil cancelButtonTitle:#"Dismiss"otherButtonTitles:nil, nil];
[alert show];
}else{
NSLog(#"FALSE , mainInt = %i",mainInt);
}
}
Related
I am creating a cocoa application with finder sync extension. I'm trying to open dialog box on some events of finder app.I am beginner for x code and objective c. I have tried the below code on some event..
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:#"OK"];
[alert addButtonWithTitle:#"Cancel"];
[alert addButtonWithTitle:#"OK"];
[alert addButtonWithTitle:#"Cancel"];
[alert setMessageText:#"Delete the record?"];
[alert setInformativeText:#"Deleted records cannot be restored."];
[alert setAlertStyle:NSWarningAlertStyle];
if ([alert runModal] == NSAlertFirstButtonReturn) {
// OK clicked, delete the record
}
But this code is just for presenting alert box.
Can anyone suggest me how do I get dialog box.
Try UIAlertView and UIAlertController or else simply use NSAppleScript
I'm trying to check if the current user has verified it's email before
they proceed to the next step. What am i doing wrong please help thank you. The phone number saving in background works.. When i call on the "SavePhoneInBackground" the app crashes
(The SVProgressHUd is the activity indicator)
-(void) SavePhoneInBackground {
PFUser *user = [PFUser currentUser];
user[#"phone"] = _phone_register.text;
[user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
NSLog(#"PhoneUpdatingSucces!");
_phone_register.text = nil;
[self checkUserEmailVerify];
}else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Whoops!" message:#"Something went wrong! Try again." delegate:nil cancelButtonTitle:#"ok" otherButtonTitles:nil];
[alert show];
[SVProgressHUD dismiss];
NSLog(#"There was an error in the registration!");
}
}];
}
-(void) checkUserEmailVerify {
PFUser *user = [PFUser currentUser];
if (![[user objectForKey:#"emailVerified"] boolValue]) {
// Refresh to make sure the user did not recently verify
[user refresh];
if (![[user objectForKey:#"emailVerified"] boolValue]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Whoops!" message:#"You need to verify your emailadress!" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
return;
[SVProgressHUD dismiss];
}
}
// This is a triumph.
[SVProgressHUD dismiss];
[self performSegueWithIdentifier:#"login2" sender:self];
}
func giveCaketoUser(user: PFUser) {
if (user.objectForKey("emailVerified").boolValue == true){
println("true")
} else {
println("flase")
}
}
Here is something I used in one of my recent projects:
- (BOOL)validateEmail:(NSString *)emailStr {
NSString *emailRegex = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegex];
return [emailTest evaluateWithObject:emailStr];
}
There are other manipulations of the regex you can use. When I used this method the problem I had was that the project would continue to register the user while still showing my custom alert and NSLog error, but that may have been failure on my end and not the method.
Hope this will send you down the right direction!
This is how I did it in my App and it will only let verified email users to access. You will need to modify it slightly for what you want.
- (void)logInViewController:(MyLogInViewController *)logInController didLogInUser:(PFUser *)user {
if (![[user objectForKey:#"emailVerified"] boolValue]){
NSLog(#"User not validated email");
[[[UIAlertView alloc] initWithTitle:#"Access restricted" message:#"To access this area please validate your email address" delegate:nil cancelButtonTitle:#"ok" otherButtonTitles:nil] show];
}
else if ([[user objectForKey:#"emailVerified"] boolValue]){
NSLog(#"Email validated");
[self dismissViewControllerAnimated:YES completion:NULL];
[[[UIAlertView alloc] initWithTitle:#"Welcome back" message:(#"%#", user.username) delegate:nil cancelButtonTitle:#"ok" otherButtonTitles:nil] show];
}
// [self dismissViewControllerAnimated:YES completion:NULL];
NSLog(#"Login sucessfull and username is %#",user.username);
}
Hope this helps if you have not worked it out yet!
I am trying to set up an alert view so that when the "Ok" button is pressed, an action sheet comes up with two options. I believe i have it in the right format and there are no errors, but when I run it, nothing happens. please help and thank you in advanced.
-(IBAction)sendSG:(id)sender{
UIAlertView *message = [[UIAlertView alloc]
initWithTitle:#"Send Study Guides!"
message:#"Please send your study guides to help create a bigger and more efficent network of study guides. You can send them by email, or you can take a picture of your study guide and send it to us."
delegate:self //Changed Here
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
[message show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
UIActionSheet *sendOptions = [[UIActionSheet alloc]
initWithTitle:#"Add study guide"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Destructive Button"
otherButtonTitles:#"Email", #"Take a picture", nil];
[sendOptions showInView:self.view];
}
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
NSString *emailTitle = #"Study Guides";
NSArray *toRecipents = [NSArray arrayWithObject:#"blank#gmail.com"];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
[mc setSubject:emailTitle];
[mc setToRecipients:toRecipents];
[self presentViewController:mc animated:YES completion:NULL];
}
}
set Delegate of your alert view to self.
In your code,
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
is not being called due to this
Yes, and change your button index too… forgot to tell you that. it should be 1 for both alertView & actionSheet.
Make your viewController ActionSheet Delegate. add "UIActionSheetDelegate" in your viewController.h
#interface XYZViewController : UIViewController UIActionSheetDelegate (enclose in angular braces)
Everything Else will work fine.. Let me know if there is any issue
You got the wrong button index.
The button buttonIndex == 0 is the cancel button,buttonIndex == 1 is the ok button.
Try this in alert view's callback:
double delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
// show action sheet here.
});
If it works for you, modify the delayInSeconds as you wish.
I have a problem regarding the UIAlertView on iOS7.
When I launch my application, it crashes with the following message:
*** Assertion failure in -[UIKeyboardTaskQueue performTask:], /SourceCache/UIKit_Sim/UIKit-2903.2/Keyboard/UIKeyboardTaskQueue.m:388
The error occurs on the following line:
- (IBAction)updatePositions:(id)sender{
_alert = [[UIAlertView alloc] initWithTitle:#"text" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[_alert show]; <====== IT CRASHS HERE
[NSThread detachNewThreadSelector:#selector(updateDataThread) toTarget:self withObject:nil];
}
I'm using ARC and the property _alert is set defined as: #property (nonatomic,strong)
This error seems strange, because on iOS6 the code works perfectly and I don't know what should be different on iOS7.
Does anyone have an idea what could the error?
Thanks in advance.
I encountered the same error, and the issue was that the UIAlertView was attempting to be shown from a thread which wasn't the main thread.
The crash however wouldn't always occur, only when a first AlertView was already being shown while this second AlertView was trying to pop up as well.
In my case, a simple fix was to do:
//Your code here
...
//Alert
_alert = [[UIAlertView alloc] initWithTitle:#"text" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
dispatch_async(dispatch_get_main_queue(), ^{
//Show alert here
[_alert show];
});
//Resume your code here
...
I just had this problem after forgetting that I was working from a background thread. I don't know if that's the case here, but I'd make sure you're not trying to call updatePositions: from anything other than the main thread.
Change your code like this :
_alert = [[UIAlertView alloc] initWithTitle:#"text" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[_alert show];
removing [[ and ]] around #"text"
But, your I don't think your problem came from this UIAlertView.
I had the same problem as well but not too familiar with the method dispatch_async. I used
[alert performSelectorOnMainThread:#selector(show) withObject:nil waitUntilDone:NO];
and the problem hasn't come up again.
Put your alertview code in a separate function like
-(void)showAlert
{
_alert = [[UIAlertView alloc] initWithTitle:#"text" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[_alert show];
}
Then in your IBAction do this
- (IBAction)updatePositions:(id)sender
{
[self performSelectorOnMainThread:#selector(showAlert) withObject:nil waitUntilDone:YES];
[NSThread detachNewThreadSelector:#selector(updateDataThread) toTarget:self withObject:nil];
}
You can also do like this:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Your title" message:#"Your message" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert performSelectorOnMainThread:#selector(show) withObject:nil waitUntilDone:YES];
However, if you need to show the same alert in multiple places it's better to create a separate function for it.
When the IBAction login is invoked it supposed to use a response from SOAP Web Services of either true or false, false meaning the user is not authorized to use the app.
I have it using these if statements after it gets the response, but for some reason it runs both true and false ifs.
{
[soapResults appendString: string];
NSLog(soapResults);
if (soapResults = #"true")
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:soapResults delegate:self cancelButtonTitle:#"Try Again",nil otherButtonTitles:nil];
[alert show];
[alert release];
[soapResults release];
soapResults = nil;
[loginIndicator stopAnimating];
loginIndicator.hidden = TRUE;
loggedinLabel.text = usernameField.text;
loggedinLabel.textColor = [UIColor blackColor];
NSLog(#"Valid Login");
}
if (soapResults = #"false")
{
NSLog(#"Invalid Login");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:soapResults delegate:self cancelButtonTitle:#"Try Again",nil otherButtonTitles:nil];
[alert show];
[alert release];
[soapResults release];
soapResults = nil;
[loginIndicator stopAnimating];
loginIndicator.hidden = TRUE;
}
}
Please Help
There is only one equal sign in the if statements. This assigns the string to the soapResults variable which causes the if statement to evaluate the string (which will always be true).
if (#"true")
Instead, use two equal signs to do the comparison.
if (soapResults == #"true")
Some avoid this common problem by always placing the variable at the end of the comparison.
if (#"true" == soapResults)
This way if you forget the second equal sign it will cause a compilation error which is much easier to find.
Update: as the commentators kindly pointed out, you should not compare Objective-C strings using the == operator. Instead use the isEqualToString method.
if ([soapResults isEqualToString:#"true"])