Two types of view in one view controller

I have two UIAlertView in the same view controller and I want to use the delegate method

 -(void)alertView:(UIAlertView *οΌ‰alertView clickedButtonAtIndex:(NSInteger) buttonIndex 

This method will be called when a button is clicked in the warning view. However, both view modes resemble the same method.

How can I separate the two views?

+4
source share
1 answer

When a warning is displayed, set the tag property to different values. This is just an integer and can be requested in the callback / delegation method.

Here is an example (using an ActionSheet, not an AlertView, but the principle is exactly the same):

 UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Some option", nil]; actionSheet.actionSheetStyle = UIActionSheetStyleDefault; actionSheet.tag = 10; [actionSheet showInView:self.view]; [actionSheet release]; 

Then in your selector:

 -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { switch (actionSheet.tag) { case 10: // do stuff break; case 20: // do other stuff break; } } 

Of course, you should use constants, not literal values, localized strings, etc., but this is the main idea.

+8
source

All Articles