Cancel button click highlight after showing UIActionView on iphone - iphone

I have a view that has a button which brings up a UIActionSheet pop up on click. If the user clicks cancel, the original button that brought up the action sheet is still highlighted as if it is being clicked. How do I reset the state after user cancels the action?

Use one of the protocol methods to change the state of the button that was clicked:
#protocol UIActionSheetDelegate <NSObject>
#optional
// Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
// Called when we cancel a view (eg. the user clicks the Home button). This is not called when the user clicks the cancel button.
// If not defined in the delegate, we simulate a click in the cancel button
- (void)actionSheetCancel:(UIActionSheet *)actionSheet;
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet; // before animation and showing view
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet; // after animation
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex; // before animation and hiding view
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex; // after animation
#end

Sorry about my false assumption that you were using UIButton. Here should be what you need:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//your implementation here
....
//Then deselect the row so it quits the highlighted state
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

Related

View locked out after UIActionSheet or UIAlertView (iPhone / iPad)

I'm having some trouble with either a UIAlertView or UIActionSheet stealing the focus and not letting go.
To elaborate, I have a setup whereby a user presses a button. This then displays a UIAlertView with a UITextField. When the user presses the 'Ok' button a UIActionSheet is displayed (called from the UIAlertView delegate method) confirming something with the user. However when they press one of the buttons the UIActionSheet goes away but the focus doesn't get returned to the main view.
I have been playing around and whatever I seem to do I always end up with my main UIView being covered with a layer like this (obviously you can seem my view through the layer). I have tried removing all subviews of the view but it didn't have any success.
Please can someone help me out?
Here is the source code:
// This method displays the UIAlertView which contains a UITextView linked up to a
// UIPickerView.
- (IBAction)buttonPressed:(id)sender
{
UIAlertView *deleteAlert = [[UIAlertView alloc] initWithTitle:#"Delete Something"
message:#"Select something to delete:"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
deleteAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
deleteAlert.tag = kDeleteAlert;
deleteTextField = [deleteAlert textFieldAtIndex:0];
[pickerView reloadAllComponents];
deleteTextField.inputView = pickerView;
[deleteAlert show];
}
// The UIAlertView delegate method is then used to display a UIActionSheet to confirm
// whether the user wants to proceed.
- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
{
UIActionSheet *confirmDelete = [[UIActionSheet alloc] initWithTitle:#"Delete blah?"
delegate:self
cancelButtonTitle:#"No"
destructiveButtonTitle:#"Yes"
otherButtonTitles:nil];
confirmDelete.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[confirmDelete showInView:self.parentViewController.tabBarController.view];
}
// Then the focus should be returned to the original view (i.e. where the first button
// was pressed to launch the UIAlertView. However the focus is still locked and the
// view appears slightly dimmed around the edges.
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == actionSheet.destructiveButtonIndex)
{
// Do some stuff ...
}
}
I think your issue is a result of the deleteAlert alert view not being dismissed when the actionSheet is called.
I sounds to me like the alert view still has focus but is in an unknown state, because it hasn't been dismissed, but you have done something with it's button press.
Presumably, you want to dismiss the alert view when the action sheet is presented? Then when the action sheet is dismissed, you get back to your main view? So, the order you want:
1) Present alert view
2) If a button on the alert view is pressed, it notifies it's delegate using - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
3) Dismiss the alertView - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated and present the action sheet.
3) When the action sheet is used, it calls it's delegate method - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex and within that method, dismiss it, or whatever else you need to do.
You would put all of these delegate methods in your original calling viewcontroller, conforming it to <UIAlertViewDelegate, UIActionSheetDelegate>.
You will need to make the original viewcontroller the delegate of the alert view AND the action sheet when you create them. I hope this makes sense.
More information http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html
I would say it's probably b/c you are not resigning the first responder on the UITextField that is inside the UIAlertView. Make sure that before you show your UIActionSheet you do
[myTextField resignFirstResponder];

How to dismiss modal view controller using UIAlertView?

I'd like to dismiss the whole modal view controller by tapping cancel on the UIAlertView that I implemented. The alert view asks the user if he wants to logout and if the user taps Yes, it'll dismiss the modal view controller.
How can I do this in Xcode?
Thanks!
You can start by checking out the documentation on uialertviewdelegate.
First you need to declare your class as the delegate for the uialertview and then implement the method to get the index of button that the user has clicked.
You can use these methods to check the user's choice
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
Use alert view delegate method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[self dismissModalViewControllerAnimated:YES];
}
In case you have more than OK button, you need to mention button index, i.e.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==0)
{
//Do something
}
if(buttonIndex==1)
{
[self dismissModalViewControllerAnimated:YES];
}
}
If you are using an alertView to dismiss a modal view controller,
Use didDismissWithButtonIndex:(NSInteger)buttonIndex instead of clickedButtonAtIndex:(NSInteger)buttonIndex
The latter causes a crash.

UISearchDisplayController - Method for when cancel button is clicked

I have a UISearchDisplayController that shows the cancel button. I would like to call a method when a user clicks the cancel button. Is there a way to do this?
You can use the following searchbar delegate method,
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
// DO ur operations
}
For UISearchDisplayController, the following 2 methods can be considered.
- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
NSLog(#"WillEndSearch");
}
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
NSLog(#"DidEndSearch");
}

How to handle user click the "delete button " in setEditing in TableView?

I added a editButton on the table like this:
self.navigationItem.leftBarButtonItem = self.editButtonItem;
and, having a setEditing method:
- (void) setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.watchListDetailTableView setEditing:editing animated:animated];
if (editing) {
// you might disable other widgets here... (optional)
} else {
// re-enable disabled widgets (optional)
}
}
after I click the edit, I can have a cross and delete button, which method should I do to handle the delete button click? thank you.
This should be it:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
More Info Here
When users tap the insertion (green
plus) control or Delete button
associated with a UITableViewCell
object in the table view, the table
view sends this message to the data
source, asking it to commit the
change. (If the user taps the deletion
(red minus) control, the table view
then displays the Delete button to get
confirmation.) The data source commits
the insertion or deletion by invoking
the UITableView methods
insertRowsAtIndexPaths:withRowAnimation:
or
deleteRowsAtIndexPaths:withRowAnimation:,
as appropriate.

make return button on keyboard perform an action

I have a searchbox. I want to make it so when the user pushes search on there keyboard it performs and IBAction. How can i link that key to the -(IBAction)Method.
There one delegate method which is fired when search button is pressed
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
//CALL YOUR IBACTION METHOD HERE
}
-Happy Coding
(BOOL)textFieldShouldReturn:(UITextField *)textField{
[self yourButtonName:nil];
}