objective-c : iphone programming : Showing variables on UIAlert view - iphone

i want simply to show a double value on a uialert view it is possible?

Put the value into a formatted NSString and send that to the alertView:
NSString *messageString = [NSString stringWithFormat:"#number = %.2f", 42.0];
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Title text" message:messageString delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];

How are you showing the UIAlertView now? Something like the following should work:
double myDouble = 12.6;
NSString *alertString = [NSString stringWithFormat:#"%g", myDouble];
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:alertString
message:nil
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil];
[myAlert show];

Related

How to show a UIAlertView upon selection of an item in UIPickerView

In my picker view i have a "Custom" option which should popup a UIAlertView for the user to enter a new value, the value is saved in the plist source of the picker for future reference. xxxEditingDidBegin is being called repeatedly (never ending).
I presume its because my UIAlertView is triggering the picker to close.
How should I have done this?
- (IBAction)serviceTypeFieldEditingDidEnd:(UITextField *)sender
{
UIPickerView *picker = [sender.inputView.subviews objectAtIndex:0];
NSString *selText = [serviceTypeArray objectAtIndex: [picker selectedRowInComponent:0]];
sender.text = selText;
if (NSOrderedSame==[selText compare:#"Custom"])
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Custom Role"
message:#"Enter Role Title"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[alertView show];
}
}
Implement method like
- (IBAction)doSelectDate:(UIDatePicker *)sender
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Hi" message:#"AlertView is shwoing" delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
and connect above method with ValueChanged Event of UIPickerView;
I fixed it like this
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
NSString *rowString = [serviceTypeArray objectAtIndex:row];
if ([rowString compare:#"Custom"] == NSOrderedSame)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Custom Role"
message:#"Enter Role Title"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[alertView show];
}
else
{
_serviceType.text = rowString;
}
}

UITextField Validation not working

I am new to iPad developer,
I am making Registration form in my iPad, when user click Submit button i want to validate my textfield whether it is empty or not, if it is empty then it will display alert No cannot be empty.
here is my code snippet but it is not working,
declaration:
UITextField* noTxtbox= [[UITextField alloc] initWithFrame:CGRectMake(250, 200, 180, 50)];
noTxtbox.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:noTxtbox];
Code:
if ([noTxtbox.text isEqualToString:#""]) {
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Enter your No !"
message:nil delegate:self
cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
Any help will be appreciated.
try using noTxtbox.text.length == 0
The problem could be due to white space. Check it by removing white space by :-
NSString * noTxtbox_text = [noTxtbox.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([noTxtbox_text isEqualToString:#""]) {
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Enter your No !"
message:nil delegate:self
cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}

UIAlertView show multiple message

how can i show multiple messages in my alertView from different variables?
Build the desired NSString from your multiple variables, e.g.:
NSString *foo;
NSString *bar;
NSString *baz;
// ... set values for foo, bar and baz ...
NSString *myMessage = [NSString stringWithFormat:#"%# %# %#", foo, bar, baz];
Then set your alert view to use the composite message myMessage:
NSString *myTitle = #"xyz";
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: myTitle
message: myMessage
delegate: nil
cancelButtonTitle: #"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
NSString *string1=#"total time played:30\n";
NSString *string2=#"total score :90\n";
NSString *string3=#"19/2/20010 12:00:77\n";
NSString *string=[NSString stringWithFormat:#"%#%#%#",string1,string2,string3];
UIAlertView *progressAlert = [[UIAlertView alloc] initWithTitle:#"Hello" message:string delegate:nil cancelButtonTitle:#"ok" otherButtonTitles:nil];
[progressAlert show];
[progressAlert release];
NSString *str1=#"Message 1.";
NSString *str2= #"Message 2.";
NSString *str3 = #"Message 3";
NSString *msg=[NSString stringWithFormat:#"%#\n%#\n%#",str1,str2,str3];
FreeCoinsCustomAlert* alert = [[FreeCoinsCustomAlert alloc] initWithTitle:msg
message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
You may wish to flesh out your question a bit more, but working from my understanding of your question, you should just be able to use the
- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
UIAlertView constructor. Then just concatenate your variables into a single NSString object and pass it as the (NSString*)message. The
+ (id)stringWithFormat:(NSString *)format, ...
factory method makes this trivial. I would recommend reading your options with NSString in the Apple documentation.

UIAlert View-for yes/no condition

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.

iPhone SDK simple alert message question

char asd='a';
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Are you sure?" message:asd
delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[alert show];
[alert release];
above code not compiling in iPhone simulator.
Why is that?
:)
You're trying to pass a char where an NSString is expected. Read up on Objective-C and th Cocoa/Cocoa Touch documentation.
To fix your issue, try this:
NSString *asd = #"a";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Are you sure?" message:asd
delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[alert show];
[alert release];
You don't compile in the iPhone
simulator, you compile on your mac.
Do you get an error message?
The message parameter should be an NSString, not a char?
replace
char asd='a';
With
NSString *asd=#"a";
It should compile after it...