Can someone please post a simple example of how to do an alert (UIAlertView) with an OK and Cancel button and how I could simply change detect which button was pressed?
When I read the docs it would appear that I may have to create a UIAlertView delegate and being a Cocoa newbie I am not sure how to go about doing that.
Look up UIAlertViewDelegate in your documentation, its all in there. Basically you just declare the proper method and it will get called for you passing in the button index.
Then just add the following in the class you need the alert:
Code:
- (void)alertOKCancelAction {
// open a alert with an OK and cancel button
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView" message:@"<Alert message>" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[alert show];
[alert release];
}
and
Code:
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 0)
{
NSLog(@"ok");
}
else
{
NSLog(@"cancel");
}
}
Then just add the following in the class you need the alert:
Code:
- (void)alertOKCancelAction {
// open a alert with an OK and cancel button
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView" message:@"<Alert message>" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[alert show];
[alert release];
}
and
Code:
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 0)
{
NSLog(@"ok");
}
else
{
NSLog(@"cancel");
}
}
- (IBAction)showCheat
{
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Warning!"
message: @"Are You Sure you Want to See the Answer? Beware it Takes 3 Points Away From your Grade"
delegate: self
cancelButtonTitle: @"Cancel"
otherButtonTitles: @"OK"];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the OK/Cancel buttons
.........
}
but get the following errors on the red line:
Wrong type argument to unary minus
expected ';' before ':' token
thanks for any suggestions
As far as the above code is concerned, the method - (void)alertViewUIAlertView *)actionSheet clickedButtonAtIndexNSInteger)buttonIndex { is declared inside another method (IBAction) showCheat.
You should add a curly bracket } before the definition of the method -(void)alertView .....
I had this problem recently. I ended up using tags:
Code:
alert1.tag=1;
alert2.tag=2;
and then in the delegate method:
Code:
if (actionSheet.tag==1) {
//check buttonIndex value and do something
}
else if (actionSheet.tag==2) {
//check buttonIndex value and do something different
}
I had this problem recently. I ended up using tags:
Code:
alert1.tag=1;
alert2.tag=2;
and then in the delegate method:
Code:
if (actionSheet.tag==1) {
//check buttonIndex value and do something
}
else if (actionSheet.tag==2) {
//check buttonIndex value and do something different
}
Tags seem like a bit of a bodge, is there a more correct way?
Can someone please post a simple example of how to do an alert (UIAlertView)
with login.. like username and password whenever username and password wrong at that time alert will appear..
Below is an example on how to create a UIAlterView with a single Text Box, if you want two text boxes then you will need to adjust and add what you need to make both fit. Also, I found it easier to make the TextBox an ivar, this way I can use the self delegate to grab the text that was entered after the Update button was pressed, this should get you on the right path:
I'm also using the alert tag to determine which UIAlertView is present since this screen that I pulled this from has 2 other Alerts that show up depending on the WebService that is sending data (such as a Timeout Message from the DB).