I got a UITextField and I want to send a UIAlertView if the user enter nothing in the UITextField.
First capture the input:
NSString *text = myTextField.text;
Then test for null string and present alertview:
if (title == [NSNull null] || title.length == 0 ) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:delegate cancelButtonTitle:button1 otherButtonTitles:button2, nil];
[alertView show];
}
Related
I am new to objective-c and have recently started making iOS applications in Xcode. I was making a login application that has two view controllers. It looks like this: http://imgur.com/W3nxMEG
Now my issue is that I was the app to go to second ViewController only if:
1) Text fields are filled
2) Passwords match
that's the code that I've written. I couldnt find any command that controls the transition between the viewControlled based on the certain constraints:
(IBAction)loginButton:(id)sender {
BOOL passMatch, emptyUser, emptyPass, emptyrePass;
passMatch = [password isEqualToString:reEnter];
emptyUser = [username isEqualToString:#""];
emptyPass = [password isEqualToString:#""];
emptyrePass = [reEnter isEqualToString:#""];
if (emptyPass || emptyUser || emptyrePass){
UIAlertView *error=[[UIAlertView alloc] initWithTitle:#"Ooops!" message:#"You must complete all the fields " delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[error show];
}
else{
passMatch = [password isEqualToString:reEnter];
if (passMatch){
UIAlertView *pass=[[UIAlertView alloc] initWithTitle:#"Success!" message:#"Passwords match " delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[pass show];
} else{
UIAlertView *error=[[UIAlertView alloc] initWithTitle:#"Ooops!" message:#"Passwords dont match " delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[error show];
}
}
Shall be thankful if anyone can help me solve this
First make sure your UITextField and UIButton from your storyBoard are hooked with a property in LoginViewController.h
It'll look like this:
LoginViewController.h
#interface LoginViewController : UIViewController
#property (strong, nonatomic) IBOutlet UITextField *password;
#property (strong, nonatomic) IBOutlet UITextField *username;
// and so on..
// for your button
#property (strong, nonatomic) IBOutlet UIButton *loginButton;
#end
LoginViewController.m
#implementation LoginViewController
-(IBAction)loginButton:(id)sender
{
// you can enable/disable your button here also, by:
// UIButton *senderButton = (UIButton *)sender;
// senderButton.enabled = YES/NO;
// i'm just using .length because i trust numbers more that the #""
NSString *errorMessage;
if (self.username.text.length == 0)
errorMessage = #"username is required";
else if (self.password.text.length == 0)
errorMessage = #"password is required";
else if (self.reEnter.text.length == 0)
errorMessage = #"please confirm your password";
else if (self.password.text.length == 0 || self.username.text.length == 0 || self.reEnter.text.length == 0)
errorMessage = #"You must complete all the fields";
if (errorMessage.length > 0)
{
UIAlertView *error=[[UIAlertView alloc] initWithTitle:#"Ooops!" message:errorMessage delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[error show];
}
else
{
if ([self.username.text isEqual: #"youUsername"] && [self.password.text isEqual: #"youPassword"] && [self.password.text isEqual: self.reEnter.text])
{
UIAlertView *pass=[[UIAlertView alloc] initWithTitle:#"Success!" message:#"Login successful" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[pass show];
//presentYourSecondViewController
//[self presentViewController:(UIViewController) animated:(BOOL) completion:nil];
//[self.navigationController pushViewController:(UIViewController)animated:(BOOL)];
}
else
{
UIAlertView *error=[[UIAlertView alloc] initWithTitle:#"Ooops!" message:#"Login failed" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[error show];
}
}
/* Your code...
BOOL passMatch, emptyUser, emptyPass, emptyrePass;
passMatch = [password isEqualToString:reEnter];
emptyUser = [username isEqualToString:#""];
emptyPass = [password isEqualToString:#""];
emptyrePass = [reEnter isEqualToString:#""];
if (emptyPass || emptyUser || emptyrePass){
UIAlertView *error=[[UIAlertView alloc] initWithTitle:#"Ooops!" message:#"You must complete all the fields " delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[error show];
}
else
{
passMatch = [password isEqualToString:reEnter];
if (passMatch){
UIAlertView *pass=[[UIAlertView alloc] initWithTitle:#"Success!" message:#"Passwords match " delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[pass show];
} else{
UIAlertView *error=[[UIAlertView alloc] initWithTitle:#"Ooops!" message:#"Passwords dont match " delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[error show];
}
}
*/
}
#end
There is nothing wrong with your condition i just over did it i guess.. Anyway hope i've help you Happy coding cheers..
How can I add an alert that displays instructions before game starts:
See code below:
- (void)viewDidLoad
{
[super viewDidLoad];
if (questions && configDictionary) {
[questionLabel setText:[[questions objectAtIndex:currentQuestonIndex] objectForKey:#"question"]];
NSArray *answers = [[questions objectAtIndex:currentQuestonIndex] objectForKey:#"answers"];
[answerLabel0 setText:[answers objectAtIndex:0]];
[answerLabel1 setText:[answers objectAtIndex:1]];
[answerLabel2 setText:[answers objectAtIndex:2]];
[answerLabel3 setText:[answers objectAtIndex:3]];
[pointsPerAnswerLabel setText:[NSString stringWithFormat:#"+%d points", [[configDictionary objectForKey:kPointsPerCorrectAnswer] intValue]]];
[currentQuestionNumberLabel setText:[NSString stringWithFormat:#"question %d", currentQuestonIndex+1]];
}
}
Use a UIAlertView:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Instructions"
message:#"Your Instructions..." delegate:self cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil, nil];
[alert show];
If you want to alert the user every time the app launches place it in the
- (void)applicationDidFinishLaunching:(UIApplication *)application {
}
Edit
You said you wanted to start the game after the dismiss button is pressed. So take advantage of the UIAlertView delegate:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0){
//Start your game!
}
}
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"How to play"
message:#"Answer the questions correctly to get points blablabla..."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
I am new to iOS.
I am working on alertviews. Here is my code. Here there are 2 alertviews: successfulallert and unsuccessfulallert for login page. I am using alertview delegate also here, it will work for both alertviews but I want to work only for successful alertview and navigation should be done only for successful alertview. If anybody knows this please help me.
NSString *responseOfResult = [[NSString alloc]initWithString:[result response]];
NSRange match;
// NSLog(#"string= %#", str);
match = [responseOfResult rangeOfString: #"successful"];
if(match.location == NSNotFound)
{
UIAlertView *unsuccessfulAllert = [[UIAlertView alloc]
initWithTitle:#"Alert"
message:responseOfResult
delegate:self
cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[unsuccessfulAllert show];
}
else {
UIAlertView *successfulAllert = [[UIAlertView alloc]
initWithTitle:#"Message" message:#"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[successfulAllert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0){
[[self navigationController]pushViewController:registerUserScreen animated:YES];
}
}
Why don't you put "OK" as cancelButtonTitle? Everything will be handled automatically.
UIAlertView *successfulAllert = [[UIAlertView alloc]
initWithTitle:#"Message" message:#"Login successful." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[successfulAllert show];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0){
//POP here with this:
[self.navigationController pushViewController:addItemView animated:NO];
}
}
NSString *responseOfResult = [[NSString alloc]initWithString:[result response]];
NSRange match;
// NSLog(#"string= %#", str);
match = [responseOfResult rangeOfString: #"successful"];
if(match.location == NSNotFound)
{
UIAlertView *unsuccessfulAllert = [[UIAlertView alloc]
initWithTitle:#"Alert"
message:responseOfResult
delegate:self
cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[unsuccessfulAllert setTag:1];
[unsuccessfulAllert show];
}
else {
UIAlertView *successfulAllert = [[UIAlertView alloc]
initWithTitle:#"Message" message:#"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[successfulAllert setTag:2];
[successfulAllert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag == 2)
{
[[self navigationController]pushViewController:registerUserScreen animated:YES];
}
else
{
//[[self navigationController]pushViewController:registerUserScreen animated:NO];
// OR
return;
}
}
You have many ways to correct your code, the first and very common is to use the tag property (integer) of the UIView. Since UIAlertview inherits from UIView, it has the tag property, so each time you want create an alert (or a view), set the tag and the check your condition like:
...
alert.tag=1;
[alert show];
then to know wich alert is calling the callback:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag==theTagOfYourAlert){
//do your stuff
}
}
another way, in your case, could be:
if([alertView.title isEqualToString:#"Alert"]){
//do your stuff
}
}
Add tag to the two alert views and check for tag in alert view delegate.
Sample code:
NSString *responseOfResult = [[NSString alloc]initWithString:[result response]];
NSRange match;
// NSLog(#"string= %#", str);
match = [responseOfResult rangeOfString: #"successful"];
if(match.location == NSNotFound)
{
UIAlertView *unsuccessfulAllert = [[UIAlertView alloc]
initWithTitle:#"Alert"
message:responseOfResult
delegate:self
cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[unsuccessfulAllert setTag:1];
[unsuccessfulAllert show];
}
else {
UIAlertView *successfulAllert = [[UIAlertView alloc]
initWithTitle:#"Message" message:#"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[successfulAllert setTag:2];
[successfulAllert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag==2 && buttonIndex == 0){
[[self navigationController]pushViewController:registerUserScreen animated:YES];
}
Yes the delegate would work for both the alertviews but you can assign a tag to each alertview Object and check for the tag in the delegate and then perform event if the tag for that particular AlertView onject matches.If u need code , i will provide.
For things like Login status updates, you might want to have the "Login Successful" message disappear automatically. Try this instead:
https://github.com/camclendenin/flashbox
This works nicely and comes in handy for situations like this. Plus you don't have to deal with all the clutter involved with UIAlertViews.
I can't set action event for alertview. Here when I click the alertview button it can't be actioned. What's the problem in my code.
Here is my code :
-(IBAction)savebuttons
{
if([username.text isEqualToString:#""] && [password.text isEqualToString:#""] && [emailid.text isEqualToString:#""] && [phonenum.text isEqualToString:#""] && [address.text isEqualToString:#""] && [city.text isEqualToString:#""] && [state.text isEqualToString:#""] && [country.text isEqualToString:#""] && [zipcode.text isEqualToString:#""]) {
UIAlertView *view = [[UIAlertView alloc] initWithTitle:#"Warning" message:#"Please enter all the details" delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[view show];
[view release];
}
else if([username.text isEqualToString:#""])
{
views = [[UIAlertView alloc] initWithTitle:#"Email ID" message:#"Email ID cannot be blank" delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
views.tag=1;
[views show];
[views release];
}
else if([password.text isEqualToString:#""]){
UIAlertView *view = [[UIAlertView alloc] initWithTitle:#"Password" message:#"Password cannot be blank" delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
view.tag=2;
[view show];
[view release];
}
else
{
.....
......
}
- (void)alertView:(UIAlertView *)alertViews clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertViews.tag == 1)
{
if(buttonIndex == 0)
{
[username becomeFirstResponder];
NSLog(#"Username");
}
}
if(alertViews.tag == 2)
{
if(buttonIndex == 0)
{
[password becomeFirstResponder];
NSLog(#"Password");
}
}
I have only one button for alertview like "OK". But it's not responding.
you have set delegate of alert views to nil, set it to self, if you did not set delegate , delegate methods are not called
The message gets sent to the delegate, which you specified as nil. Set the delegate to self, and it would work.
UIAlertView *view = [[UIAlertView alloc] initWithTitle:#"Warning"
message:#"Please enter all the details"
delegate:self
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:#"Alert !!" message:#"This is my Alert"
delegate:self cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",nil];
[myAlert show];
myAlert.tag = 1;
[myAlert release];
// Delegate Method of UIAlert :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==1 && myAlert.tag == 1)
{
// Action to be done/ called on button index 1 (OK button) of myAlert
}
else if (buttonIndex==0 && myAlert.tag == 1)
{
// Action to be done/ called on button index 0 (Cancel button) of myAlert
}
}
my app needs alert msg and if yes button pressed then one more alert msg and then i have to called a method.This is my code:
-(IBAction)resetPressed:(id)sender
{
NSString *title= [NSString stringWithFormat:#"Warning"];
NSString *message = [NSString stringWithFormat:#"Are you sure you want to Reset"];
NSString *ok = [NSString stringWithFormat:#"No"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView.tag ==1)
{
NSString *title= [NSString stringWithFormat:#"Warning"];
NSString *message = [NSString stringWithFormat:#"Are you sure you want to Reset"];
NSString *ok = [NSString stringWithFormat:#"No"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
alert.tag =2;
[alert show];
[alert release];
}
else if(alertView.tag ==2)
{
[self resetArray];
}
}
Thanks.
I'm not sure what your goal is but a few things look wrong to me anyways:
First of all you should create your strings this way:
NSString *title= #"Warning";
There's no need to use stringWithFormat in your case.
Then, it doesn't seem you properly set the first UIAlert's tag to 1, and the default value for tags is 0 so I guess the if statements in didDismissWithButtonIndex are never true.
Also, you should check which button was pressed using buttonIndex, otherwise you are going to show both alert and call [self resetArray] whichever button is pressed by the user.
Hope that helps.
In your code, you create the first alert, but never actually set the tag on it. You should do:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
alert.tag = 1; //Or 2, or something.
[alert show];
[alert release];
Then the code in your delegate method will run.
Please define two separate UIAlertView in .h file
#interface XYZViewController:UIViewController
{
UIAlertView *firstAlertView;
UIAlertView *secondAlertView;
}
Now in your .m file modify as below:
-(IBAction)resetPressed:(id)sender
{
NSString *title= [NSString stringWithFormat:#"Warning"];
NSString *message = [NSString stringWithFormat:#"Are you sure you want to Reset"];
NSString *ok = [NSString stringWithFormat:#"No"];
if(firstAlertView == nil)
{
firstAlertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
}
[firstAlertView show];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView == firstAlertView)
{
NSString *title= [NSString stringWithFormat:#"Warning"];
NSString *message = [NSString stringWithFormat:#"Are you sure you want to Reset"];
NSString *ok = [NSString stringWithFormat:#"No"];
if(secondAlertView == nil)
{
secondAlertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
}
[secondAlertView show];
}
else if(alertView == secondAlertView)
{
[self resetArray];
}
}
and in dealloc method please release the allocated UIAlertviews.
Hope i am clear to you.
Thanks,
Jim.