Uialertview * myalert = [[uialertview alloc] initwithtitle: @ "sorry" message: @ "1234567890" delegate: Self cancelbuttontitle: @ "OK" identifier: nil, nil]; myalert. delegate = self; [myalert show]; // [myalert release]; // If release is not in the delegate below, remember to release it here; otherwise, the memory will leak.
Here is how to set delegate. To add delegate, we only need to display a pop-up window warning. We can use the following code to solve the problem without taking other actions:
[[[[UIAlertView alloc] initWithTitle:@"sorry" message:@"1234567890" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] autorelease] show];
What is the role of setting delegation? We can implement a delegate method of uialertview, so that we can process the action after the button is pressed in this method, and decide the shot-like operation based on the button that the user presses, for example, the operations to be performed after the OK button and the cancel button are pressed may be different:
-(void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ NSLog(@"Button %d pressed",buttonIndex); [alertView release];}
See no? We can use buttonindex to determine which button the user presses and perform corresponding processing. At the same time, we can also click release here, because we may not know where to release the button in the above Code, so is release the most secure after you press the button?