UIActionsheet not dismissing - iphone

I have 3 buttons in my UIActionsheet.
I want one cancel button at the bottom of it.
I also want that all of the buttons should be able to dismiss UIActionsheet.
Currently, none of them does the job for me.
Here is how I display it:
UIActionSheet *actionSheet = nil;
NSArray *otherButtons = nil;
otherButtons = [NSArray arrayWithObjects:#"Button1", #"Button2", #"Button3",
nil];
actionSheet = [[UIActionSheet alloc] initWithTitle:title
delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];
for( NSString *btntitle in otherButtons)
{
[actionSheet addButtonWithTitle:btntitle];
}
[actionSheet addButtonWithTitle:#"Cancel"];
actionSheet.cancelButtonIndex = actionSheet.numberOfButtons - 1;
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view];
In my delegate, I do this:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//some code
[actionSheet dismissWithClickedButtonIndex:buttonIndex animated:YES];
}
But it does not dismiss. I do not want to change my design and position of any buttons.
What should I do?

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
[actionSheet dismissWithClickedButtonIndex:buttonIndex animated:YES];
[self performSelector:#selector(OtherOperations) withObject:nil afterDelay:0.0];
}
-(void)OtherOperations{ //some code
}

Be careful when opening an action sheet from a lpgr. Only open it on "Began", not again on "Ended".
- (void) handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
CGPoint p = [gestureRecognizer locationInView:self.tblYourVids];
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
NSIndexPath *indexPath = [self.aTable indexPathForRowAtPoint:p];
if (indexPath == nil)
NSLog(#"long press on table view but not on a row");
else {
NSLog(#"long press on table view at row %d", indexPath.row);
self.selectedCell = [self.aTable cellForRowAtIndexPath:indexPath];
DLog(#"open action sheet only once");
[self showActionSheet];
}
} else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
// eat this one
}
}

you have to add this to yours .h file.
<UIActionSheetDelegate>
Also add this line to yours .m file.
actionSheet.delegate = self;
Let me know, this is working or not. If works, accept it as right answer

just try to use the below code.it will automatically dismiss.
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:#""
delegate:self
cancelButtonTitle:#"cancel"
destructiveButtonTitle:#"Printer"
otherButtonTitles: nil];
[sheet showInView:self.view];
[sheet release];
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
}
let me know whether it is working or not
Happy Coding!!!!!

This is not the right way to cancel UIActionSheet firstly you have to disable to default UIActionSheet by javascipt
and go through this web
http://www.icab.de/blog/2010/07/11/customize-the-contextual-menu-of-uiwebview/

Related

UITextView is not resigning when added as sub view of UIAlertView

I've added UITextView as a sub view of UIAlertView, I've added UIToolBar with a Done button (using UIBarButtonItem) as inputAccessoryView to allow user to resign keyboard`. It's just working fine in iOS6.0. And not in iOS5.0.
I've break point and checked in all the way I can, for reconfirming my self, I've made a sample project and checked the same in both the iOS versions, and the problem is same.
Here's the code, that's messing with me,
-(UIToolbar *)accessoryView
{
if (!accessoryView) {
accessoryView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.0)];
UIBarButtonItem *btn = [[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(hideKeyBoard)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
accessoryView.items = [NSArray arrayWithObjects:flexibleSpace,btn, nil];
[accessoryView setTintColor:[[UIColor blackColor]colorWithAlphaComponent:0.5]];
}
return accessoryView;
}
-(IBAction) showAlertWithTextView: (id) sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithFrame:CGRectMake(0, 50, 320, 200)];
alert.title = nil;
alert.message = nil;
alert.delegate = self;
//textview I've added in .h file
textView = [[UITextView alloc] initWithFrame:alert.bounds];
textView.text = #"This is a UITextView";
textView.keyboardAppearance = UIKeyboardAppearanceAlert;
textView.editable = YES;
textView.inputAccessoryView = [self accessoryView];
[alert addSubview:textView];
[alert show];
}
- (void)hideKeyBoard {
[textview resignFirstResponder];
//[self.view endEditing:YES]; //this is also not worked
}
Here's list of steps I'm doing,
Showing Alert with Textview
Focus to Textview
Tap on Done button to resign TextView
Its not resigning in iOS5 but resigning in iOS6
Any idea? What's going wrong?
Actually your textview is not the first responder. The first responder is your alertView.
So you should try this....
[textField resignFirstResponder];
[alertView resignFirstResponder];
To use this code you must declare your alertView object in your.h file
I've solved it, as solution given by #iOS Developer, but as I've so many UIAlertView objects to handle, I've added a property of UIAlertView in delegate, and assigning it with the object of my alertview like this,
-(void)willPresentAlertView:(UIAlertView *)alertView
{
//set current alertview
[[AppDelegate sharedDelegate] setCurrentAlertView:alertView];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//will remove current alertview reference
[[AppDelegate sharedDelegate] setNilCurrentAlertView];
}
- (void)hideKeyBoard {
[textView resignFirstResponder];
//when need to resign UITextView, get current alertview object
UIAlertView *alertView = [[AppDelegate sharedDelegate] getCurrentAlertView];
if(alertView) {
[alertView resignFirstResponder];
}
}
In AppDelegate.m file,
#pragma mark - UIAlertView Resign
- (void) setCurrentAlertView:(UIAlertView *)alert{
self.alertObj = alert;
}
- (void) setNilCurrentAlertView {
self.alertObj = nil;
}
- (UIAlertView *)getCurrentAlertView {
return (UIAlertView *)self.alertObj;
}
As it is somewhat late but I am wondering that your code will not work in iOS7 or not as from iOS7 onwards UIAlertView is not allowing to add any subview (As you are adding UITextView as subview of UIAlertView).
If your requirement is not to compulsory using the UITextView then you can use UIAlertView with following configuration with default textfield in alert.
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"Message"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
[message setAlertViewStyle:UIAlertViewStylePlainTextInput];
[message show];
And you can get value of this textfield in delegate of UIAlertView like this,
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
[alertView textFieldAtIndex:0].text;
}

UIAction Sheet Not Dismissing When Pressing UIBarButton

I am presenting a UIActionSheet from a UIBarButtonItem. I want the action sheet to dismiss when I click the bar button again, instead it is creating a new one each time layered on top of each other. Any ideas?
- (IBAction)actionButtonClicked:(id)sender
{
UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:#"Action Menu" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Help", #"Lock", nil];
popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[popupQuery showFromBarButtonItem:sender animated:YES];
[popupQuery release];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
[self erxButtonClicked];
}
else if (buttonIndex == 1)
{
[self erxRefillButtonClicked];
}
}
I would declare a #property for popupQuery on my class and use it to keep track of the action sheet.
- (IBAction)actionButtonClicked:(id)sender
{
// If self.popupQuery is not nil, it means the sheet is on screen and should be dismissed. The property is then set to nil so a new sheet can be created next time.
if (self.popupQuery)
{
[self.popupQuery dismissWithClickedButtonIndex:self.popupQuery.cancelButtonIndex animated:YES];
self.popupQuery = nil;
return;
}
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:#"Action Menu" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Help", #"Lock", nil];
sheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
sheet.delegate = self;
self.popupQuery = sheet;
[sheet release];
[self.popupQuery showFromBarButtonItem:sender animated:YES];
}
// Implementing this method to be notified of when an action sheet dismisses by means other than tapping the UIBarButtonItem. We set the property to nil to prepare for lazy instantiation next time.
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
self.popupQuery = nil;
}

Creating two action sheets in one view

I created two action sheets in one view. There are two buttons, each will initiate one action sheet.
The problem: when i press on first choice in both action sheets the same action is triggered.
Here's my code:
-(IBAction) ChangeArrow:(id)sender{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Change Arrow"
delegate:self
cancelButtonTitle:#"cancel"
destructiveButtonTitle:#"Red"
otherButtonTitles:#"Blue",#"Black",nil];
[actionSheet showInView:self.view];
[actionSheet release];}
- (void) actionSheet: (UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex ==[actionSheet destructiveButtonIndex]) {
self.bar.image=[UIImage imageNamed:#"red"];
}
else if(buttonIndex == 1){
self.bar.image=[UIImage imageNamed:#"blue"];
}
else if(buttonIndex == 2){
self.bar.image=[UIImage imageNamed:#"dark"];}
}
//Second Action sheet:
-(IBAction) Background:(id)sender{
UIActionSheet *actionSheet2 = [[UIActionSheet alloc] initWithTitle:#"Change Background"
delegate:self
cancelButtonTitle:#"cancel"
destructiveButtonTitle:#"Sky"
otherButtonTitles:#"Thumbs",#"Smiley",nil];
[actionSheet2 showInView:self.view];
[actionSheet2 release];
}
- (void) actionSheet2: (UIActionSheet *)actionSheet2 didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex ==[actionSheet2 destructiveButtonIndex]) {
self.background.image=[UIImage imageNamed:#"sky"];
}
else if(buttonIndex == 1){
self.background.image=[UIImage imageNamed:#"thumbs"];
}
else if(buttonIndex == 2){
self.background.image=[UIImage imageNamed:#"smiley"];}
}
Set the tag property on each actionsheet to a different value. Then you can check sender.tag to see which action sheet called your method.
Ex.
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Change Arrow"
delegate:self
cancelButtonTitle:#"cancel"
destructiveButtonTitle:#"Red"
otherButtonTitles:#"Blue",#"Black",nil];
actionSheet.tag = 1;
UIActionSheet *actionSheet2 = [[UIActionSheet alloc] initWithTitle:#"Change Arrow"
delegate:self
cancelButtonTitle:#"cancel"
destructiveButtonTitle:#"Red"
otherButtonTitles:#"Blue",#"Black",nil];
actionSheet2.tag = 2;
Then
- (void) actionSheet: (UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
if(actionSheet.tag == 1) {
//do something
} else if(actionSheet.tag == 2) {
//do something else
}
}

iphone modal dialog like native "take picture, choose existing"

How do I create a modal dialog with customisable button choices just like the "take picture or video" | "choose existing" dialog on the iPhone? Those buttons aren't normal UIButton's and I'm sure they aren't hand crafted for every app out there.
It sounds like you want to use a combination of UIActionSheet and UIImagePickerController. This code shows a popup that lets the user choose to take a photo or choose an existing one, then the UIImagePickerController pretty much does everything else:
- (IBAction)handleUploadPhotoTouch:(id)sender {
mediaPicker = [[UIImagePickerController alloc] init];
[mediaPicker setDelegate:self];
mediaPicker.allowsEditing = YES;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Take photo", #"Choose Existing", nil];
[actionSheet showInView:self.view];
} else {
mediaPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:mediaPicker animated:YES];
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
mediaPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else if (buttonIndex == 1) {
mediaPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
[self presentModalViewController:mediaPicker animated:YES];
[actionSheet release];
}
NOTE: This assumes you have a member variable "mediaPicker" which keeps a reference to the UIImagePickerController

Create popup animation

How to create a pop up animation on iPhone? I want show my uiview as a popup view, like uialertview design.
- (void) killHUD
{
[alertView dismissWithClickedButtonIndex:0 animated:YES];
[alertView release];
alertView = nil;
}
- (void)presentSheet:(NSString *)message withFrame:(CGRect)frame
{
if(!alertView)
{
alertView = [[CustomAlertView alloc] initWithFrame:frame onView:self.window];
alertView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
[alertView setNeedsDisplay];
[alertView setText:message];
[alertView show];
}
}
You can use these functions creaed by me to show an hide the loading animation popup..
Happy Coding..