Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

extremechatter

macrumors newbie
Original poster
Jul 9, 2011
7
0
Hi,

I am trying to asign actions to the action sheet button but nothing is happening. I have xcode 4.3. Following is my code. Please tell me where am I wrong. I want to chose the source of imagepicker from action sheet.

Code:
- (IBAction)choosePhoto {
	
	// Show an image picker to allow the user to choose a new photo.
    UIActionSheet *actionSheet = 
    [[UIActionSheet alloc] initWithTitle:
     @"Choose Source" 
                                delegate:self 
                       cancelButtonTitle:nil 
                  destructiveButtonTitle:nil 
                       otherButtonTitles:@"Take Photo", @"Choose Photo", nil];
    [actionSheet showInView:self.view];
    [actionSheet release];
   
}

and then command line for action sheet

Code:
- (void)actionSheet:(UIActionSheet *)actionSheet willDissmissWithButtonIndex:(NSInteger)buttonIndex
{
   
	switch (buttonIndex)
	{
		case 0:
		{
			UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
	imagePicker.delegate = self;
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
	[self presentModalViewController:imagePicker animated:YES];
	[imagePicker release];
			break;
		}
		case 1:
		{
			UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
	imagePicker.delegate = self;
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
	[self presentModalViewController:imagePicker animated:YES];
	[imagePicker release];
			break;
		}
		
	}
}

Thanx
 
You have XCode 4.3? Really?

The delegate method is actionSheet:willDismissWithButtonIndex:. In the code you posted, you have misspelt Dismiss as Dissmiss.

Can I make a personal request? Don't put the word help or exclamation marks in thread subjects. Neither will get you more assistance, and in fact may dissuade someone from helping you.
 
Thanx Buddy, but this is not working as well.

You'll have to be a little more forthcoming - what isn't working as well as what? What does it do? What does it not do? What did you expect it to do?
Is the newly correctly spelled callback being called? What indexes are you getting?

I prefer the "did" version of this callback over the "will" version for this case - you do expect that the action sheet is going away when you press one of the buttons, and only one action is allowed.
 
Add an NSLog() as the first line of your delegate method. Print the button index.

If nothing is printed, then this means your delegate method isn't being called. That will happen when you misspell the method name, for example.

If something is printed, but the button index isn't what you expected, then that means you haven't accounted for the numbering of buttons. You should look at the UIActionSheet class reference doc, where the numberOfButtons and firstOtherButtonIndex properties are described.

If you know how to use the debugger, you could also set a breakpoint on your delegate methods, and inspect the values of the variables when the breakpoint occurs. If you don't know how to use the debugger, then the minimum tool you need for finding bugs is NSLog() and being able to print things. You should then learn to use the debugger. You won't get very far in finding or fixing bugs with just NSLog(). And you won't get anywhere unless you use some kind of debugging tool.


I also recommend using this delegate method instead of the didDismiss or willDismiss method:
Code:
 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
Again, the first line should be an NSLog() that prints the button index, until you are certain that the method is being called, and the button index value is what you expect.

Read the UIActionSheetDelegate protocol reference doc for the difference between the clickedButtonAtIndex method and the didDismiss or willDismiss methods. If they seem similar, look at how they're categorized by task: one is "Responding to Actions", the others are "Customizing Behavior".
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.