Several Buttons using the same UIActionSheetDelegate methods - iphone

I have a view that contains 5 buttons. When each button is tapped the UIActionSheetDelegate method is called: -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
I have each buttons tag property set 0-4. I'm having a hard time with the delegate method finding out which button.tag was sent. Is the sender.tag information passed along to the action sheets delegate methods?
For the delegate method I use a case statement to find out which button was pressed on the action sheet and I guess I'll use an if statement to determine which sender.tag == 0 etc. I'm just a little confused at this point and need a little assistance if at all possible.
As always thanks in advance!
T

You use the buttonIndex that is passed to actionSheet:clickedButtonAtIndex: to determine which button was pressed. The buttons are indexed starting at 0.

Related

Undo button within a UIActionSheet

I have a UIActionsheet with several buttons setup. How would I go about adding a undo function? I'm using UITextfields
Set the appropriate UIActionSheet button to call the undo method on the desired UITextField's NSUndoManager object. For all UITextField and UITextView objects in iOS 3.0 and higher, there is an undoManager object that can be accessed and used to manage undoing and redoing of operations. The necessary code would go something like this:
(UIActionSheetDelegate method implementation)
- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == /*undo index*/)
[yourTextField.undoManager undo];
}
EDIT: I suggest you look at the Apple documentation for Undo Managers, it should help you further with this should you need it.
Have you considered Shake to Undo? An Undo alert comes up if you enter some text in a UITextField/UITextView & shake your iPhone.

Objective C Which Alert did the button press come from

another probably simple iPhone obj-c question for you...
I have a couple of alerts which have buttons in them that are presented to the user in certain circumstances, both are part of one view. Now I know how to tell which button was pressed, but how do I know which alert the button pressed is related to?
Any ideas would be really handy! Cheers!
Set a tag to each of the alerts, and compare the tag of the alert caught at the delegate method didDismissWithButtonIndex.
Set tag as, alert.tag = 10;
and check in delegate as
if (alertView.tag == 10)
Try to implement a simple delgate for your alert views.
For exemple using : alertView:clickedButtonAtIndex:
you will be able to know which alert view the button pressed is related to.
You can call UIAlertView's Delegate method explained below...and check for title or alert message like this.. here i had given you the example of title...
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0 && [alertView.title isEqualToString:#"Invalid"])
{
//Your Stuff goes here
}
}

UIALertView customization

I am developing an iPhone application, in which I want to use customized alert sheet. The customization is required since I want to set image for Alert-Sheet buttons, change the size of these button etc.
I have done the following things:
Created UIView with customized controls that I wanted.
Created and displayed UIAlertView
In the delegate method of UIAlertView (UIAlertViewDelegate) i.e
- (void)willPresentAlertView:(UIAlertView *)alertView
I am removing all the subviews of UIAlertView and adding my customized view as subview.
Everything works fine till here. Since I have used customized buttons, I need to remove the alert sheet explicitly in the Action Method of the button by calling dismissWithClickedButtonIndex:animated: on UIAlertView.
Even though the UIALertView gets dismissed, it takes around 0.5 second to get dismissed.
Can someone help me out to solve the problem of this delay in dismissing OR some other way of customization of Alert View buttons.
Thanks and Regards,
Deepa
I could get it worked by passing YES to dismissWithClickedButtonIndex:animated: call i.e [alertView dismissWithClickedButtonIndex: 0 animated: YES]. Initially I was passing the flag as NO. But, I don't know whey it takes less time if we pass the animation flag as YES.
Anyone knows this?
Instead of doing this
/*
1. Created UIView with customized controls that I wanted.
2. Created and displayed UIAlertView
3. In the delegate method of UIAlertView (UIAlertViewDelegate)
*/
do this:
Create a class like this:
#interface CustomAlertView : UIAlertView
{
//For ex:
UIButton *myCustomButton;
//and other custom controls
}
Implement it in following method:
-(id)init
{
}
In the above method use:
[self addSubView: myCustomButton];
I have given just the idea. I have code but not presently to share with you.
If you are not able to implement the above I will provide later.
Thanks.
Here is the library which can solve your UIAlertView Customisation issue. It can also work as UIActionSheet. It has very good customisation options.
https://github.com/Codigami/CFAlertViewController

Add alert or action sheet to UITableView during swiping to delete

I want to pass an object to the action sheet from the declaration of the UIActionSheet, how to do it?
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex{
Create a property in your header.
Set it whenever you create your
object.
Then reference it in the
didDismissWithButotnIndex you
posted.
Easy. Peasy. Japanesy.

iPhone table view - some button/action questions

I created a table view that is populated with a custom UITableViewCell (like this). Each of the cells contains two UIButtons. I assign the action to the button like this:
[decreaseButton addTarget:self action:#selector(decrease) forControlEvents:UIControlEventTouchUpInside];
Is this the right way?
Anyway, it works, but in my "decrease" method I need to know in which of my 18 table view rows the button was pressed. indexPath.row doesn't work outside the cellForRowAtIndexPath method, of course.
Can someone explain me how to do this?
Thanks a lot in advance!
iYassin
You can do this in two ways.
Inspecting the Event Sender
Change your decrease method from:
- (void)decrease;
to:
- (void)decrease:(id)sender;
That way when decrease is called, you'll be given a reference to the button that had the touch up inside event.
Define the decrease Method Closer to the Information
Another solution would be to have a different target instance for each button (for example, implement the decrease function as part of the custom cell). That way you know the button that was touched was the one for the current cell.
The way i solved this is I keep track of data i might need inside my custom cell object. And the button is connected not to the external receiver but the cell it's self which in-turn knows how to call the real receiver of the action.
I make my cell with something like:
cell = [[MyTableViewCell alloc] initWithStyle:style
reuseIdentifier:CellIdentifier];
And I have a setup method so i can re-init a cell when I dequeue it:
[cell setupMyCellWithContext:objectID
target:[[UIApplication sharedApplication] delegate]
action:#selector(someAction)];
so inside your cell class you use the action and target that was sent in the setup method to call the true target:
- (void)doAction:(id)sender {
if ([target respondsToSelector:action]) {
[target performSelector:action withObject:objectID afterDelay:0];
}
}
So when your user taps the button, the os calls [cell doAction:], which calls the target and action selector you set up before hand with the correct context object.