how to show alert when values is null - iphone

I want to show alert when values is null i have titleCategory if it has value null then it should show alert
NSString*test=titleCategory;
if ([titleCategory isEqualToString:nil])
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Select Category " delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else
{
FirstViewController*targetController=[[FirstViewController alloc]init];
[self.navigationController pushViewController:targetController animated:YES];
}

if(test == nil)
{
UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:#"" message:#"Test is null" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil] autorelease];
[alert show];
}

Do this:
if (!(titleCategory.length > 0) || titleCategory == nil )
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Select Category " delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else
{
FirstViewController*targetController=[[FirstViewController alloc]init];
[self.navigationController pushViewController:targetController animated:YES];
[targetController release]; // if not ARC
}

Please Use this code:
if([titleCategory.text isKindOfClass:[NSNull class]]){
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Select
Category " delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else{
FirstViewController*targetController=[[FirstViewController alloc]init];
[self.navigationController pushViewController:targetController animated:YES];
[targetController release]; // if not ARC
}

Related

Putting the text of a UITextField into a UIAlertView

Im trying to put the text from a textfield into a UIAlertView, but it's not shown. It is just showing blank.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Kik" message:self.kik.text delegate:self cancelButtonTitle:#"Got it!" otherButtonTitles: nil];
[alert show];
You should try,
if ( [self.kik.text length])
{
UIAlertView *alert = [UIAlertView alloc]initWithTitle:#"Kik" message:[NSString stringWithFormat:#"%#",self.kik.text] delegate:self cancelButtonTitle:#"Got it!" otherButtonTitles: nil];
[alert show];
}
else
{
UIAlertView *alert = [UIAlertView alloc]initWithTitle:#"Kik" message:#"Please enter a valid string" delegate:self cancelButtonTitle:#"Got it!" otherButtonTitles: nil];
[alert show];
}
Check..

Warning: Attempt to present <SLTwitterComposeViewController: 0x233141c0> on <MainController: 0x2083ebb0> whose view is not in the window hierarchy

I am receiving this error when trying to post an image to Twitter using the following code, cannot think what I am doing wrong any ideas for how to solve?;
- (void)PostToTwitter
{
UIImage* pxImage = [[self GetResultImage] retain];
TWTweetComposeViewController *twitter = [[[TWTweetComposeViewController alloc] init] autorelease];
[twitter setInitialText:#"Twitter Pic"];
[twitter addImage:pxImage];
FaceAppDelegate* app = (FaceAppDelegate*)[[UIApplication sharedApplication] delegate];
[app.mainCtrl presentViewController:twitter animated:YES completion:NULL];
twitter.completionHandler = ^(TWTweetComposeViewControllerResult res)
{
if(res == TWTweetComposeViewControllerResultDone)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Success" message:#"You have posted successfully." delegate:self cancelButtonTitle:#"Close" otherButtonTitles: nil];
[alert show];
[alert release];
}
else if(res == TWTweetComposeViewControllerResultCancelled)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Cancelled" message:#"You have cancelled posting to Twitter." delegate:NULL cancelButtonTitle:#"Close" otherButtonTitles: nil];
[alert show];
[alert release];
}
[app.mainCtrl dismissModalViewControllerAnimated:YES];
};
[pxImage release];
}
I think, that your view isn't presented. Try to use app.window.rootViewController, not property mainContr.

UIAlertView not showing correctly in iPhone app

In iphone app i am using alertview to check the if textbox values is empty then it should show alert other wise move to next screen but when i also enter the values again alert view is shows i don't know what is the problem
It always shows if true if i also enter value then also
-(IBAction)buttonClick{
if (monthTextField.text=#"") {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Please enter values first" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else if (motionSicknessTextField.text=#"") {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Please enter values first" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else if (prescriptionTextField.text=#"") {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Please enter values first" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else if (otherMeansTextField.text=#"") {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Please enter values first" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else if (cereniaTextField.text=#"") {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Please enter values first" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else if (costToClientTextField.text=#"") {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Please enter values first" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else if (clinicMarkup.text=#"") {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Please enter values first" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else{
GraphsViewController*targetController=[[GraphsViewController alloc]init];
NSString*dogswithMotionSickness=motionSicknessTextField.text;
NSString*valueOne=cereniaTextField.text;
NSString*valueTwo=prescriptionTextField.text;
NSString*valueThree=otherMeansTextField.text;
NSString*valueFour=overtheCounterTextField.text;
cerenia=cereniaTextField.text;
NSString*costToClient=costToClientTextField.text;
NSString*clinicalMarkup=clinicMarkup.text;
perMonth=monthTextField.text;
targetController.perMonth=perMonth;
targetController.dogswithMotionSickness=dogswithMotionSickness;
targetController.valueOne=valueOne;
targetController.valueTwo=valueTwo;
targetController.valueThree=valueThree;
targetController.valueFour=valueFour;
targetController.cerenia=cerenia;
targetController.costToClient=costToClient;
targetController.clinicalMarkup=clinicalMarkup;
[self.navigationController pushViewController:targetController animated:YES];
}}
If you want to check empty strings, use :
if([monthTextField.text isEqualToString:#""])
OR
if(monthTextField.text.length > 0)
I define these two macros in my AppName-Prefix.pch file
#define IS_EMPTY_STRING(str) (!(str) || ![(str) isKindOfClass:NSString.class] || [(str) length] == 0)
#define IS_POPULATED_STRING(str) ((str) && [(str) isKindOfClass:NSString.class] && [(str) length] > 0)
This way I can check for both empty strings and nil string objects.
IS_EMPTY_STRING(myString);
also use
if ([motionSicknessTextField.text isEqualToString:#""] || [motionSicknessTextField.text isEqualTo:nil])
it is filter with nil and also blank object or value of textField

Iphone mail application

It is possible open Iphone Mail application without compose new message?
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"mailto:%#?subject=My Subject", _recipient]]];
This code not suitable for me.
I need just open mail application on main screen.
Thanks
P.S Sorry for my English ))
-(void) EmailMethod
{
MFMailComposeViewController *picker=[[MFMailComposeViewController alloc]init];
picker.mailComposeDelegate = self;
[picker setToRecipients:#"mailid#mymail.com"];
[picker setSubject:#"Place your subject of mail here."];
[picker setMessageBody:#"Place your body of mail here." isHTML:YES];
[self presentModalViewController:picker animated:YES];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
switch (result)
{
case MFMailComposeResultCancelled:
UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:#"Canceled !!"
message:#"Mail sending cancelled." delegate:nil
cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[ErrorAlert show];
[ErrorAlert release];
break;
case MFMailComposeResultSaved:
UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:#"Saved"
message:#"Mail saved to Drafts." delegate:nil
cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[ErrorAlert show];
[ErrorAlert release];
break;
case MFMailComposeResultSent:
UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:#"Email Sent"
message:#"Thank you for recommending us to your friends via Email."
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[ErrorAlert show];
[ErrorAlert release];
break;
case MFMailComposeResultFailed:
UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:#"Error !!"
message:#"Failed to send mail." delegate:nil
cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[ErrorAlert show];
[ErrorAlert release];
break;
default:
UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:#"Error !!"
message:#"Failed to send mail." delegate:nil
cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[ErrorAlert show];
[ErrorAlert release];
break;
}
[self becomeFirstResponder];
[self dismissModalViewControllerAnimated:YES];
}

Is it possible to show alert in `clickedButtonAtIndex` method of alertview

In delegate method ofUIalerview I am trying to show another alert after finishing the process on clicking button index. Is it possible to do so? Also I want to call a method on button click of alert. How can I do that?
I am trying in this way. Is is correct?
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 1)
{
inappPurchaseViewController = [[InAppPurchaseViewController alloc] init];
[inappPurchaseViewController Upgrade:nil];
[inappPurchaseViewController release];
UIAlertView *purchasedone = [[UIAlertView alloc] initWithTitle:#"Enable Ads" message:#"You can Enable Ads from Settings Option" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[purchasedone show];
[purchasedone release];
}
}
You want to use tags for your UIAlertViews
Assigning tags
UIAlertView *firstAlert = [[UIAlertView alloc] initWithTitle:#"Do something first" message:#"This is the first UIAlertView" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[firstAlert setTag: 0];
[firstAlert show];
[firstAlert release];
Handling those tags
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (alertView.tag)
{
case 0: /* firstAlert */
{
if(buttonIndex == 1)
{
inappPurchaseViewController = [[InAppPurchaseViewController alloc] init];
[inappPurchaseViewController Upgrade:nil];
[inappPurchaseViewController release];
UIAlertView *purchaseDone = [[UIAlertView alloc] initWithTitle:#"Enable Ads" message:#"You can Enable Ads from Settings Option" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[purchaseDone setTag: 1];
[purchaseDone show];
[purchaseDone release];
}
}
break;
case 1: /* purchaseDone */
{
/* purchaseDone uialertview was triggered, handle it here. */
}
break;
}
}
UIAlertView *a = [[UIAlertView alloc] initWithTitle:#"a" message:#"b" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"ca",nil];
[a show];
[a release];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 1)
{
UIAlertView *purchasedone = [[UIAlertView alloc] initWithTitle:#"Enable Ads" message:#"You can Enable Ads from Settings Option" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
purchasedone.tag = 55;
[purchasedone show];
[purchasedone release];
}
if([alertView tag] == 55 && buttonIndex == 0)
{
UIAlertView *purchasedone = [[UIAlertView alloc] initWithTitle:#"New Alert" message:#"New Alert Message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[purchasedone show];
[purchasedone release];
}
}
write UIAlertViewDelegate in .h file.
put the following line on top of your .m file
`#define PREVIOUS_ALERT 101`
`#define NEW_ALERT 102`
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
//you need to make a condition here for checking the alert else it will go into infinite loom of showing alert after alert
//you can do it by setting tag to alert something like this
if(alertView.tag == PREVIOUS_ALERT) //sot the previous alert tag to this where you created it
{
if(buttonIndex == 1)
{
inappPurchaseViewController = [[InAppPurchaseViewController alloc] init];
[inappPurchaseViewController Upgrade:nil];
[inappPurchaseViewController release];
UIAlertView *purchasedone = [[UIAlertView alloc] initWithTitle:#"Enable Ads" message:#"You can Enable Ads from Settings Option" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
purchasedone.tag = NEW_ALERT;
[purchasedone show];
[purchasedone release];
}
}
else
{
//code for your second alert
}
}