Make UIAlertView Button trigger function On Press

后端 未结 7 2047
小蘑菇
小蘑菇 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条回答
  •  旧时难觅i
    2020-12-25 09:28

    You need to setup the delegate for your UIAlertView, before showing it. Then do the work in the delegate callback as such:

    -(void)alertView:(UIAlertView*)alert didDismissWithButtonIndex:(NSInteger)buttonIndex;
    {
        if ([[alert buttonTitleAtIndex] isEqualToString:@"Do it"]) {
            // Code to execute on Do it button selection.
        }
    }
    

    My CWUIKit project over at https://github.com/Jayway/CWUIKit has an addition to UIAlertView that allow you to do the same thing but with blocks. Redusing the same operation for both creating, showing and handling the alert to this:

    [[UIAlertView alertViewWithTitle:@"My Title"
                             message:@"The Message"
                   cancelButtonTitle:@"Cancel"
      otherTitlesAndAuxiliaryActions:@"Do it", 
                                     ^(CWAuxiliaryAction*a) {
                                        // Code to execute on Do it button selection.
                                     }, nil] show];
    

提交回复
热议问题