Action Sheet

Discussion in 'iOS Programming' started by yaboy10holla, Feb 21, 2011.

  1. yaboy10holla, Feb 21, 2011
    Last edited by a moderator: Feb 21, 2011

    yaboy10holla macrumors newbie

    Joined:
    Feb 19, 2011
    #1
    Hi, I'm trying to test out some action sheet skills and I can't figure out why this isnt working. I'm trying to get the title of the action sheet button to simply post in a UITextField. here are my actions:


    Code:
    -(IBAction)doActionSheet:(id)sender {
    	UIActionSheet *actionSheet;
    	actionSheet=[[UIActionSheet alloc]initWithTitle:@"Available Actions"
    										   delegate:nil 
    								  cancelButtonTitle:@"Cancel" 
    							 destructiveButtonTitle:@"Destroy" 
    								  otherButtonTitles:@"Negotiate", @"Compromise", nil];
    	[actionSheet showInView:self.view];
    }
    
    ---------and then this -------------


    Code:
    - (void)actionSheet:(UIActionSheet *)actionSheet
    clickedButtonAtIndex:(NSInteger)buttonIndex {
    	NSString *buttonTitle=[actionSheet buttonTitleAtIndex:buttonIndex];
    	if ([buttonTitle isEqualToString:@"Destroy"]) {
    		userOutput.text=@"Destroy";
    	} else if ([buttonTitle isEqualToString:@"Negotiate"]) {
    		userOutput.text=@"Negotiate";
    	} else if ([buttonTitle isEqualToString:@"Compromise"]) {
    		userOutput.text=@"Compromise";
    	} else {
    		userOutput.text=@"Cancel";
    	}
    }
    


    I modified the controller header file to include the UIActionSheetDelegate protocol as well. So I'm not sure why nothing happens when you click the button.

    any help greatly appreciated!! thanks
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    The delegate of your action sheet is nil. What effect would this have, given that actionSheet:clickedButtonAtIndex: is a delegate method?
     
  3. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #3
    This is what I do

    Go to the .H file, do this -->

    Code:
    @interface DetailViewController : UIViewController <UIPopoverControllerDelegate, UIActionSheetDelegate>
    Code:
     UIBarButtonItem *settingsBarButtonItem;
    Code:
     @property (nonatomic, retain) IBOutlet UIBarButtonItem *settingsBarButtonItem;
    .M file

    Code:
    @synthesize settingsBarButtonItem;
    Code:
    - (IBAction) changeToSettings:(id)sender {
    	//NSLog(@"gets called changeToSettings");
    	UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Account Settings"
    															 delegate:self 
    													cancelButtonTitle:nil 
    											   destructiveButtonTitle:nil 
    													otherButtonTitles:@"Setting 1", 
    								  @"Setting 2", nil];
    	[actionSheet showFromBarButtonItem:[self settingsBarButtonItem] animated:YES]; 
    	[actionSheet release];   
    }

    Code:
    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    	
    	switch (buttonIndex) {
    		case 0:
    			userOutput.text = @"Settings 1 clicked";
    			break;
    			
    		case 1:
    			userOutput.text = @"Settings 2 clicked"; 
    			break;
    			
    		default:
    			NSLog(@"default got called");
    			break;
    	}
    }
    
    Something like that I guess..
     
  4. yaboy10holla thread starter macrumors newbie

    Joined:
    Feb 19, 2011
    #4
    thanks guys really appreciate it. changed the delegate to self and now it works perfect!
     

Share This Page