If else statement is causing me trouble [duplicate] - ios

This question already has answers here:
How to use an UIActionSheet within an if statement? [closed]
(2 answers)
Closed 9 years ago.
I have an if else if statement that is giving me trouble.
First, here's my code:
if([_hasUserTakenAPhoto isEqual: #"YES"]) {
_xButtonAfterPhotoTaken = [[UIActionSheet alloc] initWithTitle:#"Delete" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil];
[_xButtonAfterPhotoTaken showInView:self.view];
_xButtonActionSheetTitle = [_xButtonAfterPhotoTaken buttonTitleAtIndex:0];
} else if ([_xButtonActionSheetTitle isEqualToString:#"Delete"]) {
[self performSegueWithIdentifier:#"backToHomeFromMediaCaptureVC" sender:self];
} else {
[self performSegueWithIdentifier:#"backToHomeFromMediaCaptureVC" sender:self];
}
My problem is that I need to perform the method call of:
[_xButtonActionSheetTitle isEqualToString:#"Delete"] and if it is equal to "Delete", then I need to perform a segue using this method call:
[self performSegueWithIdentifier:#"backToHomeFromMediaCaptureVC" sender:self];
The only way I know how to do this is with another if statement, which you can see in my original code I started with "else if", but this creates different scope for all of my action sheet statements.
I just need to make the title of "Delete" on my action sheet tappable and it needs to trigger the segue.
I was trying to follow this action sheet answer here: https://stackoverflow.com/a/17374248/3117509
I just can't seem to figure this out.

Closing question. The problem is that I do not have a solid grasp on UIActionSheets.

Related

UITextField validations [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Hi i am beginner in Ios in my project i am crating one registration page and there i have to provide validations for textfields as like best level and i have searched for this so many textfield validations tutorials but i did not get correct one
According to my requirement when i clicked on textfield if there is any error then need to show alert message on textfields like below screen for this i have written so many custom methods but they are not working please help me some one
You have to make your custom view for validate the textFields
You can try to do it with https://github.com/ustwo/US2FormValidator
Code in reference to Objective-C
US2ValidatorTextField *firstNameTextField = [[US2ValidatorTextField alloc] init];
firstNameTextField.validator = [[[MyProjectValidatorName alloc] init] autorelease];
firstNameTextField.shouldAllowViolation = YES;
firstNameTextField.validateOnFocusLossOnly = YES;
firstNameTextField.placeholder = #"Enter first name";
firstNameTextField.validatorUIDelegate = self;
[_textUICollection addObject:firstNameTextField];
[firstNameTextField release];
Hope it helps you.
I've written many UITextField validators and its always best to do this sort of thing yourself as each requirement is different and there is more control over it.
The best time to validate is normally when UItextField did end editing - this way you know the user is done.
You need to look at UITextFields delegate methods and use the text argument from there to check length and that sort of thing.
I can't supply any code as you haven't stated what sort of validation you want done. If its email - this is the sort of thing you need (Its in Objective-C though)
/* Email address validation test */
NSString *emailReg = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailReg];
if ([emailTest evaluateWithObject:emailAddress]){
validEmail = YES;
}
Its like you can implement this with simple code as like checking and showing an alert.
if the textfield is empty
if the textfield contains numbers
if the textfield is a email field
if the all the required text field is filled
you can achieve this by using this piece of code
// To check the email field is not empty string
if([[self.email text] isEqualToString:#""]){
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:#"Warning" message:#"Enter your email!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alertView show];
}
// To check the email field has '#' empty string
else if ([self.email.text rangeOfString:#"#"].location == NSNotFound){
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:#"Warning" message:#"Enter proper email!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alertView show];
}
you can implement like this or you can use libraries depending on your requirement.

Tracking which UIAlert button is clicked to change UILabel

I'm a complete beginner and looking to learn how to start coding to build my own apps.
I've been messing around with the classic "Hello World" design and trying to improve on it based on the limited knowledge i have managed to gather in the past week but i've got stuck and hope someone can help me out!
So far I have a UITextField for the user to enter their name, a label to display their name and a button to update the display. To help understand UIAlertViews better I have also included a Alert if the name entered is Chris (or chris) and another alert if the name entered is not Chris. All this is functioning fine so far.
I'm looking to track what button is pressed on my incorrectName UIAlertView and then update my UILabel to say "Chris" if they click the "I Want to be Called Chris" button. I have build another app similar to this before from a tutorial and I have copied across the code which i thought should function the same but it doesnt seem to work.
Here is my .m file so far
#import "Solo1ViewController.h"
#interface Solo1ViewController ()
#end
#implementation Solo1ViewController
#synthesize setMessage;
#synthesize userName;
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonTitle=([alertView buttonTitleAtIndex:buttonIndex]);
if ([buttonTitle isEqualToString:#"I Want To Be Called Chris!"])
{setMessage.text=#"Chris";}
}
-(IBAction)showMessage:(id)sender
{
setMessage.text=userName.text;
if ([userName.text isEqual:#"Chris"] || [userName.text isEqual:#"chris"])
{
UIAlertView *correctName;
correctName = [[UIAlertView alloc]initWithTitle:#":)" message:#"Great Name!" delegate:nil cancelButtonTitle:#"Bye Fellow Chris" otherButtonTitles:nil, nil];
[correctName show];
}
else
{
UIAlertView *incorrectName;
incorrectName=[[UIAlertView alloc] initWithTitle:#":(" message:#"Chris is a Better Name" delegate:nil cancelButtonTitle:#"No, Thanks" otherButtonTitles:#"I Want To Be Called Chris!", nil];
[incorrectName show];
}
}
-(IBAction)removeKeyboard:(id)sender{
[userName resignFirstResponder];
}
To me it seems as though this should work but I guess I'm probably missing something glaringly obvious or i'm doing it totally wrong.
Make sure to set the alert view's delegate to self! Right now you have the delegate set as nil.
Example:
incorrectName=[[UIAlertView alloc] initWithTitle:#":(" message:#"Chris is a Better Name" delegate:self cancelButtonTitle:#"No, Thanks" otherButtonTitles:#"I Want To Be Called Chris!", nil];

How to use an UIActionSheet within an if statement? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have an if statement that is working fine, but I need to add a 2nd if statement inside of it and I can't seem to figure out how to get it right.
Here is my code:
-(IBAction)xButton {
if([_hasUserTakenAPhoto isEqual: #"YES"]) {
_xButtonAfterPhotoTaken = [[UIActionSheet alloc] initWithTitle:#"Delete" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil];
[_xButtonAfterPhotoTaken showInView:self.view];
NSString *title = [_xButtonAfterPhotoTaken buttonTitleAtIndex:1];
if(title isEqualToString:#"Delete") {
[self performSegueWithIdentifier:#"backToHomeFromMediaCaptureVC" sender:self];
}
} else {
[self performSegueWithIdentifier:#"backToHomeFromMediaCaptureVC" sender:self];
}
}
I get errors when I add in the 2nd if statement of:
if(title isEqualToString:#"Delete") {
[self performSegueWithIdentifier:#"backToHomeFromMediaCaptureVC" sender:self];
}
I have tried making the 2nd if statement an "else if" but then it will not let me access the NSString object called "title". Is there an easier way to do this, or should I just make title a global variable?
UIActionSheets are not used that way:
- (IBAction)xButton:(UIButton*)sender
{
if ([_hasUserTakenAPhoto isEqual:#"YES"])
{
_xButtonAfterPhotoTaken = [[UIActionSheet alloc] initWithTitle:#"Delete Photo?" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"Delete" otherButtonTitles:nil];
[_xButtonAfterPhotoTaken showInView:self.view];
}
else
{
[self performSegueWithIdentifier:#"backToHomeFromMediaCaptureVC" sender:self];
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
// Check if it's the correct action sheet and the delete button (the only one) has been selected.
if (actionSheet == _xButtonAfterPhotoTaken && buttonIndex == 0)
{
[self performSegueWithIdentifier:#"backToHomeFromMediaCaptureVC" sender:self];
}
}
- (void)actionSheetCancel:(UIActionSheet *)actionSheet
{
NSLog(#"Canceled");
}
You gotta understand that interface elements are not "instant", there's plenty of asynchrony going on. When presenting an UIActionSheet for instance, the thread doesn't wait for the user to answer yes or no, it keeps running.
That's why there's delegates, and blocks, you present the UIActionSheet, and with the delegate you say "I'll take care of it when the user actually clicks it".
You'd be wondering, why not just wait for it to select it? Main thread takes care of updating the interface, animations, and retrieving user input (touches, keyboard taps, etc) and even running NSTimers that are subscript to the main NSRunLoop. Stopping main thread would lock the interface.
Try
- (IBAction)xButton
{
NSString *title;
if ([_hasUserTakenAPhoto isEqual:#"YES"])
{
_xButtonAfterPhotoTaken = [[UIActionSheet alloc] initWithTitle:#"Delete" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil];
[_xButtonAfterPhotoTaken showInView:self.view];
title = [_xButtonAfterPhotoTaken buttonTitleAtIndex:1];
if ([title isEqualToString:#"Delete"])
{
[self performSegueWithIdentifier:#"backToHomeFromMediaCaptureVC" sender:self];
}
}
else
{
[self performSegueWithIdentifier:#"backToHomeFromMediaCaptureVC" sender:self];
}
}

iOS and xcode: how to create a modal segue to a special view only the first time the app is used

I want to make a "terms and conditions" screen that pops up the very first time that app is opened. On this view I would add a button that says "agree," and upon clicking it the code would execute:
[self dismissViewControllerAnimated:YES completion:nil];
...and would go to the first view of the app.
I am currently using a Tab Bar Controller that has 4 ViewControllers. So basically, I just need to have some method in viewWillAppear on my first ViewController that checks for an NSUserDefault key:value. The first time the app opens, it will be zero. After they click agree, I'll set it to 1 and the bit of code would never execute again.
Can you please offer some code to accomplish the task of routing the view from the firstViewController's view to this alternate view controller upon loading the app?
Thanks!
In the viewWillAppear method in your FirstViewController, check NSUserDefaults then present your TermsViewController. After user click agree in TermsViewController, set NSUserDefaults then call
[self.presentingViewController dismissModalViewControllerAnimated:YES]
The use of the popover window can get complicated. Try something like the following if you have little experience with Objective-C.
- (void)viewDidLoad {
if ([termsvalue == 0]) {
NSString *msg = [NSString stringWithFormat:#"Do you agree with the terms of use?"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"- Confirmation -"
message:msg
delegate:self
cancelButtonTitle:#"Disagree"
otherButtonTitles:#"I agree", nil];
[alert setTag:100];
[alert show];
}
}
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView { // Validation
if ([alertView tag] == 100) {
return YES;
}
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { // Go
if ([alertView tag] == 100) {
if (buttonIndex == 1) {
// The user has agreed
}
}
}

Calling an IBaction in a alertView if statement

Me and my buddy are working on an app, we're total newbies but have come a long way with books and goggling.
We're stuck now on this thing. We have a bunch of texfields that we have clear button linked to it with this action, but then we want that action to be called if you click "Yes" on one of the alert view buttons.
- (IBAction)clearText:(id)sender {
Spelare1Slag1.text = #"";
Spelare1Slag2.text = #"";
}
We also have this alert view:
alertDialog = [[UIAlertView alloc]
initWithTitle: #"Warning"
message: #"Do you want to delete?"
delegate: self
cancelButtonTitle: #"No"
otherButtonTitles: #"Yes", nil];
- (void)alertView: (UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"No"]) {
}
else if ([buttonTitle isEqualToString:#"Yes"]){
Spelare1Slag1.text = #"";
}
}
So this is how we think we should do it, but we don't know what to put in the else if statement. We want the textfields to clear when you press the "yes" button in the alert view, and not when you press "no"
Thanks in advance!
The clearText method, I'm assuming, is a custom method you created to delete the text in both the fields right? So instead of it being an IBAction, it should be a void method :
- (void)clearText {
Spelare1Slag1.text = #"";
Spelare1Slag2.text = #"";
}
Now all you need to do in your UIAlertView delegate method, is call the clearText method :
- (void)alertView: (UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"Yes"]){
[self clearText];
}
}
Hope this helps
You could dismiss the AlertView in case of user has clicked NO If I understand question properly.
You could dismiss the alertview like this
[alertView dismissWithClickedButtonIndex:0 animated:YES];
but make sure to see if NO has index 0 or 1, if you are not sure then just do like this
[alertView dismissWithClickedButtonIndex:nil animated:YES];
Methods of type IBAction are just like any other method, and you can call them in your code directly. In fact, IBAction is simply a macro evaluating to void. Usually, you'll pass a nil sender argument when it needs to be called outside of the context of a target / action event being triggered.
[self clearText:nil];
Why do you need to check the actual button text? Checking the button index is the most efficient way to go. If you have two fields in your alertView, just check if the index is 0 or 1 and your good to go. Your doing extra work checking the actual text.
And btw, just do a check for the index of the YES button if you don't need to do anything specific when they press no. (Don't check both indexes if you don't need to).

Resources