In my iPhone app, I have an actionsheet which has 4 buttons.
Now to perform actions on click of these buttons, I have implemented ActionSheet Delegate.
The action sheet delegate method does not get called on click of the buttons. The same code works when integrated to another project.
I have declared all the method names properly.
Below is the screenshot which shows the delegate method
What could be wrong?
Make sure you have set your UIActionSheet delegate to self:
actionSheet.delegate = self;
and also make sure you're implementing <UIActionSheetDelegate> in the header file.
If you use the code to call the action sheet like below
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"YOUR TITLE" delegate:self cancelButtonTitle:nil destructiveButtonTitle:#"YOUR MESSAGE" otherButtonTitles:#"Cancel", nil];
[actionSheet showInView:self.view];
[actionSheet release];
Then there won't be an issue calling the delegate method,
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
Cheers
Related
I have a class that has an extension of UIButton that shows a UIAlertview under certain circumstance.
#interface CellButton : UIButton {}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"You Lose!"
message:#"Play Again?"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:#"cancel", nil];
[alert show];
This works fine, but I need to present a view controller when user presses ok.But as you may know you cannot present a view controller with an extension of UIButton.
So I was wondering if I can put the code below in another viewcontroller and allow it to work with the UIAlert in Cellbutton class.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) { // and they clicked OK.
GameController*myNewVC = [[GameController alloc] init];
[self presentModalViewController:myNewVC animated:NO];
}
}
You don't do it inside the UIButton.
The target of clicking the UIButton should be a UIViewController. After that, show an alert view FROM the view controller, and the view controller will be the delegate of the UIButton. From their everything will work fine.
This would work as long as you have set the alert view delegate to the view controller you want to handle the presentation.
I would suggest moving all of the functionality to the view controller though, i.e. present and handle the alert view from the same view controller, this can be triggered from an event from the button. I think this makes the code more readable and it doesn't really make sense for a button to know about alert views
You can declare a global variable (in appDelegate - and how to do it HERE) then ;
1 - set the variable 1 when user click button
2 - get the value from other viewcontroller's action
if the value is 1 then go ahead.
I am working on a conference based application. I want to show an incoming call window to the user, I am using UIActionSheet to show that notification. Now, the problem here is that the call may come at any time from the server, at that we may be in our application any view, how can I show the incoming call notification using UIActionSheet? What delegate I have to set?
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:msg_string
delegate:(id)??????????????????
cancelButtonTitle:#"ACCEPT"
destructiveButtonTitle:#"DECLINE"
otherButtonTitles: nil];
Any suggestion is greatly appreciated.
Thanks.
an object that is guaranteed to be alive; e.g. the application's delegate. also, in a typical iOS application structure, it has a reference to the top level of the view hierarchy. -Alan
you should set like below.
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:msg_string
delegate:self
cancelButtonTitle:#"ACCEPT"
destructiveButtonTitle:#"DECLINE"
otherButtonTitles: nil];
delegate is class where to UIActionSheetDelegate method is implemented.
like e.g
#implementation ViewController
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{ // you code }
#end
so clickedButtonAtIndex is actionSheet delegate method which is in viewController Class and you alertView also shown from ViewController Class then you have to set self. or if alertView shown from other class and delegate method body written in ViewController class then as delegate you need to set object of ViewController class.
i need to add button dynamically in UIActionSheet in iphone.Please help me.
Just alloc and init a new UIActionSheet instance and add the buttons one after another using –addButtonWithTitle:. This method returns you the index at which the button has been added to. You can then set the Index of the destructive button via -setDestructiveButtonIndex.
Here is an example that adds one button and adds another one if the boolean value useDestructiveButton is YES (and directly sets it as destructive button, making it red):
UIActionSheet *sheet = [[UIActionSheet alloc] init];
[sheet addButtonWithTitle:#"Button 1"];
if (useDestructiveButton) {
[sheet setDestructiveButtonIndex:[sheet addButtonWithTitle:#"Button 2"]];
}
Don't forget to call the appropriate show method.
UIActionSheet * as=[[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:NSLocalizedString(#"Cancel",#"Cancel") destructiveButtonTitle:nil otherButtonTitles:nil];
[as setTag:0];
[as addButtonWithTitle:NSLocalizedString(#"First Button",#"First Button")];
[as addButtonWithTitle:NSLocalizedString(#"Second Button",#"Second Button")];
[as showInView:someController.view];
[as autorelease];
Also remember that your parent controller has to conform to the UIActionSheetDelegate protocol.
I have a UIView with a date picker that I'd like to display in an action sheet. I'm using the following code:
-(IBAction) button_click:(id)sender{
//UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:#"the title" delegate:nil cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"Destructive" otherButtonTitles:#"other", nil];
UIActionSheet *sheet = [[UIActionSheet alloc] init];
ActionSheetController *actionSheet = [[ActionSheetController alloc] initWithNibName:#"ActionSheetView" bundle:nil];
[sheet addSubview:actionSheet.view];
[sheet showInView:self.view];
}
What I get is a little bit of the top part of the new view coming up from the bottom and that's it. If I comment the two middle lines of code and uncomment the top part to display a regular action sheet, it works fine. Any ideas what I might be doing wrong?
use UIActionSheetDelegate and after that you can with this delegate add for example a image to button of alertsheet
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet{
UIImageView *sample = [[UIImageView alloc] initWithImage:[UIImage
imageNamed:#"Someimage.png"]];
[actionSheet addSubView:sample];
}
I do not believe you can specify Custom Views in a UIActionSheet. My understanding is that you can only display a title and 1 or more buttons. Below is the description of UIActionSheet in the Apple documentation:
Use the UIActionSheet class to present the user with a set of alternatives for how to proceed with a given task. You can also use action sheets to prompt the user to confirm a potentially dangerous action. The action sheet contains an optional title and one or more buttons, each of which corresponds to an action to take.
UIActionSheet Class Reference
I've discovered the behavoir occurs because I haven't used the UIActionSheet constructor properly. The buttons and title should be included:
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"Date Picker" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
You might want to check out an UIActionSheet alternative I made which lets you display custom content views without any dirty hacks: https://github.com/JonasGessner/JGActionSheet It has some other advantages over UIActionSheet as well!
In my tabbar app, i bring up a UIActionsheet from an action, called from a button in a navigation controller title bar.
The UIActionsheet functions as per normal, except for the bottom half of the below button 'cancel', which strangely doesnt respond to touch in the iPhone Simulator. The bottom half of the cancel button is where the UITabBar lies behind, and therefore is probably the problem.
Any ideas?
alt text http://img12.imageshack.us/img12/2166/screenshot20100119at236.png
Solution
My solution was from the first answer. Here is my working example code
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:message delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"OK" otherButtonTitles:nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
[actionSheet release];
This looks like an UIActionSheet to me...
Anyway, you should show the action sheet as a subview of the application window, not of the current view.
UIActionSheet *actionSheet = [[UIActionSheet alloc] init...];
// ...
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];