Make UIAlertView Button trigger function On Press

后端 未结 7 2041
小蘑菇
小蘑菇 2020-12-25 09:22

Currently I am using the following code to present a UIAlertView:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@\"Today\'s Entry Complete\"
               


        
7条回答
  •  無奈伤痛
    2020-12-25 09:32

    If you are using multiple UIAlertView instances that are not declared in the class's interface you can also set a tag to identify instances in your delegate method, for example:

    somewhere on top of your class file myClass.m

    #define myAlertViewsTag 0
    

    creating the UIAlertView:

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"My Alert"
        message:@"please press ok or cancel"
        delegate:self
        cancelButtonTitle:@"Cancel"
        otherButtonTitles:@"OK", nil];
    alert.tag = myAlertViewsTag;
    [alert show];
    [alert release];
    

    the delegate method:

    -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
        if (alertView.tag == myAlertViewsTag) {
            if (buttonIndex == 0) {
                // Do something when cancel pressed
            } else {
                // Do something for ok
            }
        } else {
            // Do something with responses from other alertViews
        }
    }
    

提交回复
热议问题