Problem with UIALERTVIEW objective c
i am having a weird problem with uialert views.
i have several alerts are member variables in my .h file
UIAlertView *changepwalert; //change password alert.
UIAlertView *delallalert; //delete selected transactions.
UIAlertView *logout; //logout alert.
UIAlertView *closingbalancealert; //closing balance alert.
UIAlertView *totalreportalert; //total report alert.
UIAlertView *ordercashalert; //order cash alert.
UIAlertView *unlockingalert; //unlocking alert.
UIAlertView *receivecashalert;
in my .m file i have the following for didDismissWithButtonIndex delegate method.
if(alertView==unlockingalert && buttonIndex==0)
{
//code
}
else if(alertView==ordercashalert && buttonIndex==0)
{
//code
}
else if(alertView==receivecashalert && buttonIndex==0)
{
开发者_StackOverflow中文版//code
}
else if (alertView==logout && buttonIndex == 0)
{
//code
}
else if(alertView==closingbalancealert && buttonIndex==0)
{
//code
}
else if (alertView==changepwalert && buttonIndex==0)
{
//code
}
else if(alertView==delallalert && buttonIndex==0)
{
//code
}
Here is a sample of how i am initializing the uialertview
ordercashalert = [[UIAlertView alloc] initWithTitle:nil message:@"Enter Password\n\n\n\n\n\n" delegate:self
cancelButtonTitle:@"Ok" otherButtonTitles:@"Cancel",nil];
logout = [[UIAlertView alloc] initWithTitle:@"Logout" message:@"Are You Sure?" delegate:self cancelButtonTitle:@"YES" otherButtonTitles:@"NO",nil];
[logout show];
[logout release];
when i click the logout button it runs the delegate method but it matches the alert to ordercashalert or receivecashalert.i am not sure why. it should actually match with logout alert. i am at the final stages of delivering the product and all of a sudden i have this bug. can some one help me please.
I'd suggest you use tag and enumeration trick. First - create enumeration for all possible alerts:
enum {
RSChangePasswordAlertTag,
RSDeleteAlertTag,
....
ReceiveCashAlertTag
};
Then when creating the alert, just assign appropriate tag to it
UIAlertView *myAlert = [[[UIAlertView alloc] initWithTitle:....] autorelease];
[myAlert setTag:RSDeleteAlertTag];
[myAlert show];
Then in delegate method use switch
statement for alertView
's tag:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSUInteger)buttonIndex {
switch (alertView.tag) {
case RSPasswordAlertTag: {
// Do password alert thing
break;
}
case RSDeleteAlertTag: {
// do delete thing
break;
}
.... // here be other cases
}
}
This way you'd be comparing only integers instead of pointers and your code will be cleaner with less instance variables.
精彩评论