I'm using a library called Chute to manage photo selection (from photo library, facebook, etc.).
I'm trying to implement a UIAlertView whenever selectionCount >= 20. The problem is, the Chute library handles alertView protocols in its own way, meaning some file is trying to execute
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{}
whenever my alert with buttons is displayed, which crashes the app. What should I do to work around the current method? I tried to subclass UIAlertView into a new custom class but I'm not sure where to implement the UIAlertViewDelegate or its protocol. Not sure if this is right either. Any help appreciated, thanks.
alertView: clickedButtonAtIndex: gets called on UIAlertView's delegate.
Related
Several ViewControllers in my application have to show the same UIAlertView and perform – alertView:clickedButtonAtIndex: method in the same manner. I don't want to write same code in several files so i need to have a kind of global UIAlertView with one alertView: clickedButtonAtIndex: method available for all my ViewControllers. I've created a method which calls the UIAlertView in my AppDelegate.m file and overridden alertView:clickedButtonAtIndex: in the same place, but unfortunately alertView:clickedButtonAtIndex: doesn't get called from the different file. Do you have any ideas? Thanks for your time
Why don't you subclass UIViewController to have all the functionality you require for the UIAlertView, and then have your AppDelegate cal that method on the current subclassed UIViewController on the navigation stack. Hope that Helps!
when you create the UIAlertView in your other view controllers, make sure the delegate is set to your AppDelegate (or whererever the clickedButtonAtIndex: method is declared)
I'm running into a problem where 9 times out of ten, when I call UIAlertView's dismissWithClickedButtonIndex:animated:, the delegate method alertView:willDismissWithButtonIndex: is not called. Is anyone else running into this problem? I'm about to file a bug with Apple but I'm curious to see if anyone else has run into this issue and figured out any workarounds.
To ensure a consistent behavior across iOS4 and 5, you could just remove the UIAlertView's delegate just prior to calling its dismissWithClickedButtonIndex:animated: method, then manually invoke the delegate method. e.g.
- (void)somethingDidHappen {
id<UIAlertViewDelegate> delegate = myAlertView.delegate;
myAlertView.delegate = nil;
// now, we know the delegate won't be called...
[myAlertView dismissWithClickedButtonIndex:0 animated:NO];
// ...so we call it ourselves below
[delegate alertView:myAlertView clickedButtonAtIndex:0];
}
(That code isn't tested, but you get the point.)
Delegates of UI objects are only called when the user performs an action. Apple assumes that when you do something from code, you already know what you're doing and you don't need to be informed. That applies to all delegates (scrolling delegate methods of UIScrollView vs. code-scrolling, Table View manipulation, ...)
Anyway, what button index should the delegate be called with?.. there is no one when you dismiss programmatically
According to Why doesn't dismissWithClickedButtonIndex ever call clickedButtonAtIndex? the problem is that a different method is being called. However, that doesn't explain why you get erratic calls. On the devices I tested the dismiss method gets called correctly, so I only redirect it to the click version.
Maybe you should file a bug with Apple if you continue seeing the erratic behaviour.
There are alertView:clickedButtonAtIndex:, alertView:didDismissWithButtonIndex: and alertView:willDismissWithButtonIndex:. The method that you're referring to (clickedButtonAtIndex:) is only called when the user explicitly taps on a button on your alert view (hence 'clicked').
Programmatic calls via dismissWithClickedButtonIndex:animated: to dismiss the alert does not seem to call alertView:clickedButtonAtIndex:.
So, if you need some behavior to be always triggered upon the dismissal of the alert view—whether it was triggered by the user tapping on a button or triggered programmatically—then using the didDismissWithButtonIndex: and willDismissWithButtonIndex: makes more sense.
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.
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
}
}
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