I made a UIAlertView subclass. It implements two UIAlertViewDelegate methods: alertView:clickedButtonAtIndex: is called when expected, however alertViewShouldEnableFirstOtherButton: is never called.
Had a look at related posts and added:
[textField sendActionsForControlEvents:UIControlEventEditingChanged];
but without results. I seem to be out of options. What am I missing here?
#implementation MyAlertView
+ (MyAlertView*)showAlertForJson:(NSDictionary*)json delegate:(id<F2WebAlertDelegate>)delegate
{
MyAlertView* view = [[MyAlertView alloc] initWithJson:json delegate:delegate];
return view;
}
- (instancetype)initWithJson:(NSDictionary*)json delegate:(id<MyWebAlertDelegate>)delegate
{
if (self = [super initWithTitle:json[#"title"]
message:json[#"message"]
delegate:self
cancelButtonTitle:json[#"cancelTitle"]
otherButtonTitles:nil])
{
_json = json;
self.webDelegate = delegate;
for (NSString* title in json[#"otherTitles"])
{
[self addButtonWithTitle:title];
}
[self initInput:json[#"input"]];
[self show];
}
return self;
}
- (void)initInput:(NSDictionary*)json
{
if (json == nil)
{
return;
}
[json[#"style"] isEqualToString:#"plain"] ? self.alertViewStyle = UIAlertViewStylePlainTextInput : 0;
...
[self initTextField:[self textFieldAtIndex:0] withJson:json[#"field0"]];
if (self.alertViewStyle == UIAlertViewStyleLoginAndPasswordInput)
{
[self initTextField:[self textFieldAtIndex:1] withJson:json[#"field1"]];
}
}
- (void)initTextField:(UITextField*)textField withJson:(NSDictionary*)json
{
[textField sendActionsForControlEvents:UIControlEventEditingChanged];
if (textField == nil || json == nil)
{
return;
}
[json[#"keyboard"] isEqualToString:#"ascii"] ? (textField.keyboardType = UIKeyboardTypeASCIICapable) : 0;
...
}
#pragma mark - Alert View Delegate
- (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
...
}
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView*)alertView
{
return YES;
}
#end
Turns out that, as the delegate method's name suggest, you must specify a list otherButtonTitles to UIAlertView's initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:.
So, buttons added later with addButtonWithTitle: do not count.
Related
Xcode 7.3, iOS 9.3.1
I want to use two custom overlay views for when the user is about to take a picture and after while editing the picture. To change the overlay I would like to know if there is a callback method from the UIImagePickerControllerDelegate that will let me know when the user starts editing the picture, or when the user has tapped the take picture button.
The only methods I know of are:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
I have looked here: UIImagePickerController with cameraOverlayView focusses when button in overlay is tapped, How to know I tap "taking photo button" with UIImagePicker and iOS - Taking A Picture After Button Tap in UIImagePickerController CustomOverlayView.
Or perhaps there is a way to get the tap event from the standard button by adding an observer.
Please help! Thanks in advance.
you could use the UINavigationControllerDelegate that you also have to implement when using UIImagePickerController. implementing the delegate method [...]didShowViewController[...] like this
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
NSLog(#"%#", [viewController class]);
}
produces the following output (using the simulator, choosing UIImagePickerControllerSourceTypePhotoLibrary as the sourceType and navigating like this: photo albums overview > specific album > editing specific photo):
2016-04-08 00:19:36.882 ImagePicker[44578:4705834] PUUIAlbumListViewController
2016-04-08 00:19:41.341 ImagePicker[44578:4705834] PUUIMomentsGridViewController
2016-04-08 00:19:47.929 ImagePicker[44578:4705834] PUUIImageViewController
hope that helps!
UIImagePickerControllerDelegate is quite poor but you can handle it by adding an observer:
Swift 3:
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "_UIImagePickerControllerUserDidCaptureItem"), object:nil, queue:nil, using: { note in
//Do something
})
Objective-C:
[[NSNotificationCenter defaultCenter] addObserverForName:#"_UIImagePickerControllerUserDidCaptureItem" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
//Do something
}];
Xcode 8.2.1, iOS10.2.1
Before implementing please read the solution for earlier version to understand the basics. Thanks! The problem was that some of the names for various subviews have changed from iOS 9.3.1 to iOS 10.2.1.
Here is the complete code (see below where to insert), that replaces "//Code goes here" below:
for (UIView *subviewImagePickerControllerView in self.imagePickerController.view.subviews)
{
if ([subviewImagePickerControllerView.class.description isEqualToString:#"UINavigationTransitionView"])
{
for (UIView *subviewUINavigationTransitionView in subviewImagePickerControllerView.subviews)
{
if ([subviewUINavigationTransitionView.class.description isEqualToString:#"UIViewControllerWrapperView"])
{
for (UIView *subviewUIViewControllerWrapperView in subviewUINavigationTransitionView.subviews)
{
if ([subviewUIViewControllerWrapperView.class.description isEqualToString:#"CAMCameraViewControllerContainerView"])
{
for (UIView *subviewCAMCameraViewControllerContainerView in subviewUIViewControllerWrapperView.subviews)
{
if ([subviewCAMCameraViewControllerContainerView.class.description isEqualToString:#"CAMViewfinderView"])
{
for (UIView *subviewCAMViewfinderView in subviewCAMCameraViewControllerContainerView.subviews)
{
if ([subviewCAMViewfinderView.class.description isEqualToString:#"CAMBottomBar"])
{
for (UIView *subviewCAMBottomBar in subviewCAMViewfinderView.subviews)
{
if ([subviewCAMBottomBar.class.description isEqualToString:#"CUShutterButton"] && [subviewCAMBottomBar.class isSubclassOfClass:[UIButton class]])
{
UIButton *shutterButton = (UIButton *)subviewCAMBottomBar;
[shutterButton addTarget:self action:#selector(touchUpInsideCMKShutterButton) forControlEvents:UIControlEventTouchUpInside];
}
else
{
nil;
}
}
}
}
}
else if ([subviewCAMCameraViewControllerContainerView.class.description isEqualToString:#"PLCropOverlay"])
{
for (UIView *subviewPLCropOverlay in subviewCAMCameraViewControllerContainerView.subviews)
{
if ([subviewPLCropOverlay.class.description isEqualToString:#"PLCropOverlayBottomBar"])
{
for (UIView *subviewPLCropOverlayBottomBar in subviewPLCropOverlay.subviews)
{
if ([subviewPLCropOverlayBottomBar.class.description isEqualToString:#"PLCropOverlayPreviewBottomBar"])
{
for (UIView *itemPLCropOverlayPreviewBottomBar in subviewPLCropOverlayBottomBar.subviews)
{
if ([itemPLCropOverlayPreviewBottomBar.class isSubclassOfClass:[UIButton class]])
{
UIButton *buttonPLCropOverlay = (UIButton *)itemPLCropOverlayPreviewBottomBar;
if ([buttonPLCropOverlay.titleLabel.text isEqualToString:#"Retake"])
{
UIButton *retakeButton = buttonPLCropOverlay;
[retakeButton addTarget:self action:#selector(touchUpInsideButtonRetake) forControlEvents:UIControlEventTouchUpInside];
}
else
{
nil;
}
}
else
{
nil;
}
}
}
else
{
nil;
}
}
}
else
{
nil;
}
}
}
else
{
nil;
}
}
}
else
{
nil;
}
}
}
else
{
nil;
}
}
}
else
{
nil;
}
}
Xcode 7.3, iOS9.3.1
I had to get this working, so I spent a lot of time figuring this out.
The gist of it is, I drill down the view hierarchy after the UIImagePickerController was presented, looking for CMKShutterButton and the retake button with a title of "Retake", then I attach a selector to the buttons' action, like so...
[shutterButton addTarget:self action:#selector(touchUpInsideCMKShutterButton) forControlEvents:UIControlEventTouchUpInside];
[retakeButton addTarget:self action:#selector(touchUpInsideButtonRetake) forControlEvents:UIControlEventTouchUpInside];
Drop the code below into the completion block that is called after your image picker is presented:
[self presentViewController:self.imagePickerController animated:true completion:^(void){
//Code goes here
}
Here is the complete code, that replaces "//Code goes here" above:
for (UIView *subviewImagePickerControllerView in self.imagePickerController.view.subviews)
{
if ([subviewImagePickerControllerView.class.description isEqualToString:#"UINavigationTransitionView"])
{
for (UIView *subviewUINavigationTransitionView in subviewImagePickerControllerView.subviews)
{
if ([subviewUINavigationTransitionView.class.description isEqualToString:#"UIViewControllerWrapperView"])
{
for (UIView *subviewUIViewControllerWrapperView in subviewUINavigationTransitionView.subviews)
{
if ([subviewUIViewControllerWrapperView.class.description isEqualToString:#"PLImagePickerCameraView"])
{
for (UIView *subviewPLImagePickerCameraView in subviewUIViewControllerWrapperView.subviews)
{
if ([subviewPLImagePickerCameraView.class.description isEqualToString:#"CMKBottomBar"])
{
for (UIView *itemCMKBottomBar in subviewPLImagePickerCameraView.subviews)
{
if ([itemCMKBottomBar.class.description isEqualToString:#"CMKShutterButton"] && [itemCMKBottomBar.class isSubclassOfClass:[UIButton class]])
{
UIButton *shutterButton = (UIButton *)itemCMKBottomBar;
[shutterButton addTarget:self action:#selector(touchUpInsideCMKShutterButton) forControlEvents:UIControlEventTouchUpInside];
}
else
{
nil;
}
}
}
else if ([subviewPLImagePickerCameraView.class.description isEqualToString:#"PLCropOverlay"])
{
for (UIView *subviewPLCropOverlay in subviewPLImagePickerCameraView.subviews)
{
if ([subviewPLCropOverlay.class.description isEqualToString:#"PLCropOverlayBottomBar"])
{
for (UIView *subviewPLCropOverlayBottomBar in subviewPLCropOverlay.subviews)
{
if ([subviewPLCropOverlayBottomBar.class.description isEqualToString:#"PLCropOverlayPreviewBottomBar"])
{
for (UIView *itemPLCropOverlayPreviewBottomBar in subviewPLCropOverlayBottomBar.subviews)
{
if ([itemPLCropOverlayPreviewBottomBar.class isSubclassOfClass:[UIButton class]])
{
UIButton *buttonPLCropOverlay = (UIButton *)itemPLCropOverlayPreviewBottomBar;
if ([buttonPLCropOverlay.titleLabel.text isEqualToString:#"Retake"])
{
UIButton *retakeButton = buttonPLCropOverlay;
[retakeButton addTarget:self action:#selector(touchUpInsideButtonRetake) forControlEvents:UIControlEventTouchUpInside];
}
else
{
nil;
}
}
else
{
nil;
}
}
}
else
{
nil;
}
}
}
else
{
nil;
}
}
}
else
{
nil;
}
}
}
else
{
nil;
}
}
}
else
{
nil;
}
}
}
else
{
nil;
}
}
And here is the method I am attaching, which goes in the view controller that is presenting the image picker controller:
- (void)touchUpInsideCMKShutterButton
{
NSLog(#"Take");
}
- (void)touchUpInsideButtonRetake
{
NSLog(#"Re-take");
}
Hope this helps someone! Thanks.
My app crashes, not always, at the following method
// overridden
- (void)dismiss
{
[super dismiss];
[containerView_ removeFromSuperview];
containerView_ = nil;
}
crash happens at removerFromSuperview.
There is a "show" method as well
// overridden
- (void)show
{
if (self.parentView == nil)
{
// No parentView, create transparent view as parent
CGSize frameSize = [UIApplication currentSize];
containerView_ = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frameSize.height, frameSize.width)];
containerView_.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
containerView_.backgroundColor = [UIColor clearColor];
self.parentView = containerView_;
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
{
if(some condition )
[[UIApplication sharedApplication].keyWindow.subviews.lastObject addSubview:containerView_];
else
[[UIApplication sharedApplication].keyWindow addSubview:containerView_];
}
else
{
[[UIApplication sharedApplication].delegate.window.rootViewController.view addSubview:containerView_];
}
}
[super show];
// This is done to allow the Cancel button to be pressed but nothing else - do after [super show]!
self.superview.userInteractionEnabled = YES;
}
It is strange, that code used to work. I am trying to compile the app for arm64, but I don't understand how that modification impacted those methods.
My app is a non-ARC app, and I cannot go to ARC right now.
Any ideas?
Change your code to dismiss view like this.
// overridden
- (void)dismiss
{
if(containerView_)
[containerView_ removeFromSuperview];
containerView_ = nil;
[super dismiss];
}
Please check with below code -
- (void)dismiss
{
if (containerView_)
{
[containerView_ removeFromSuperview];
containerView_ = nil;
[super dismiss];
}
}
Simply check if container view have superview
- (void)dismiss
{
if ([containerView_ superview])
{
[containerView_ removeFromSuperview];
containerView_ = nil;
[super dismiss];
}
}
In my app I've to create a custom alert view like the following:
So I followed this tutorial to create a custom alert view. I finished it but I'm getting issue in the following method:
- (void)addOrRemoveButtonWithTag:(int)tag andActionToPerform:(BOOL)shouldRemove {
NSMutableArray *items = [[NSMutableArray alloc]init];
[items addObject:self.buttonOk];
[items addObject:self.buttonClose];
int buttonIndex = (tag == 1);
if (shouldRemove) {
[items removeObjectAtIndex:buttonIndex];
} else {
if (tag == 1) {
[items insertObject:self.buttonOk atIndex:buttonIndex];
} else {
[items insertObject:self.buttonClose atIndex:buttonIndex];
}
}
}
I edited it than the tutorial because I don't need a UIToolBar for buttons. When I run the app it says me that I can't insert a nil object in an NSMutableArray, but I don't understand what's wrong, I hope you can help me to fix this issue.
UPDATE
Here's all the class code I developed:
#import "CustomAlertViewController.h"
#define ANIMATION_DURATION 0.25
#interface CustomAlertViewController ()
- (IBAction)buttonOk:(UIButton *)sender;
- (IBAction)buttonCancel:(UIButton *)sender;
#property (weak, nonatomic) IBOutlet UIButton *buttonClose;
#property (weak, nonatomic) IBOutlet UIButton *buttonOk;
#property (strong, nonatomic) IBOutlet UIView *viewAlert;
-(void)addOrRemoveButtonWithTag:(int)tag andActionToPerform:(BOOL)shouldRemove;
#end
#implementation CustomAlertViewController
- (id)init
{
self = [super init];
if (self) {
[self.viewAlert setFrame:CGRectMake(self.labelAlertView.frame.origin.x,
self.labelAlertView.frame.origin.y,
self.labelAlertView.frame.size.width,
self.viewAlert.frame.size.height)];
[self.buttonOk setTag:1];
[self.buttonClose setTag:0];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)showCustomAlertInView:(UIView *)targetView withMessage:(NSString *)message {
CGFloat statusBarOffset;
if (![[UIApplication sharedApplication] isStatusBarHidden]) {
CGSize statusBarSize = [[UIApplication sharedApplication] statusBarFrame].size;
if (statusBarSize.width < statusBarSize.height) {
statusBarOffset = statusBarSize.width;
} else {
statusBarOffset = statusBarSize.height;
}
} else {
statusBarOffset = 0.0;
}
CGFloat width, height, offsetX, offsetY;
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft ||
[[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) {
width = targetView.frame.size.width;
height = targetView.frame.size.height;
offsetX = 0.0;
offsetY = -statusBarOffset;
}
[self.view setFrame:CGRectMake(targetView.frame.origin.x, targetView.frame.origin.y, width, height)];
[self.view setFrame:CGRectOffset(self.view.frame, offsetX, offsetY)];
[targetView addSubview:self.view];
[self.viewAlert setFrame:CGRectMake(0.0, -self.viewAlert.frame.size.height, self.viewAlert.frame.size.width, self.viewAlert.frame.size.height)];
[UIView beginAnimations:#"" context:nil];
[UIView setAnimationDuration:ANIMATION_DURATION];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[self.viewAlert setFrame:CGRectMake(0.0, 0.0, self.viewAlert.frame.size.width, self.viewAlert.frame.size.height)];
[UIView commitAnimations];
[self.labelAlertView setText:#"CIAO"];
}
- (void)removeCustomAlertFromView {
[UIView beginAnimations:#"" context:nil];
[UIView setAnimationDuration:ANIMATION_DURATION];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[self.viewAlert setFrame:CGRectMake(0.0, -self.viewAlert.frame.size.height, self.viewAlert.frame.size.width, self.viewAlert.frame.size.height)];
[UIView commitAnimations];
[self.view performSelector:#selector(removeFromSuperview) withObject:nil afterDelay:ANIMATION_DURATION];
}
- (void)removeCustomAlertFromViewInstantly {
[self.view removeFromSuperview];
}
- (BOOL)isOkayButtonRemoved {
if (self.buttonOk == nil) {
return YES;
} else {
return NO;
}
}
- (BOOL)isCancelButtonRemoved {
if (self.buttonClose == nil) {
return YES;
} else {
return NO;
}
}
- (void)removeOkayButton:(BOOL)shouldRemove {
if ([self isOkayButtonRemoved] != shouldRemove) {
[self addOrRemoveButtonWithTag:1 andActionToPerform:shouldRemove];
}
}
- (void)removeCancelButton:(BOOL)shouldRemove {
if ([self isCancelButtonRemoved] != shouldRemove) {
[self addOrRemoveButtonWithTag:0 andActionToPerform:shouldRemove];
}
}
- (void)addOrRemoveButtonWithTag:(int)tag andActionToPerform:(BOOL)shouldRemove {
NSMutableArray *items = [[NSMutableArray alloc]init];
[items addObject:self.buttonOk];
[items addObject:self.buttonClose];
int buttonIndex = (tag == 1);
if (shouldRemove) {
[items removeObjectAtIndex:buttonIndex];
} else {
if (tag == 1) {
[items insertObject:self.buttonOk atIndex:buttonIndex];
} else {
[items insertObject:self.buttonClose atIndex:buttonIndex];
}
}
}
- (IBAction)buttonOk:(UIButton *)sender {
[self.delegate customAlertOk];
}
- (IBAction)buttonCancel:(UIButton *)sender {
[self.delegate customAlertCancel];
}
#end
UPDATE 2
Code in which I use the CustomAlertView:
#import "PromotionsViewController.h"
#import "CustomAlertViewController.h"
#interface PromotionsViewController () <CustomAlertViewControllerDelegate> {
BOOL isDeletingItem;
}
#property(nonatomic,strong) CustomAlertViewController *customAlert;
- (IBAction)buttonBack:(UIButton *)sender;
#property (weak, nonatomic) IBOutlet UIButton *buttonAlert;
- (IBAction)buttonAlert:(UIButton *)sender;
#end
#implementation PromotionsViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.buttonAlert setTitle:self.promotionSelected forState:UIControlStateNormal];
[self.customAlert setDelegate:self];
isDeletingItem = NO;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)buttonBack:(UIButton *)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)buttonAlert:(UIButton *)sender {
self.customAlert = [[CustomAlertViewController alloc]init];
[self.customAlert removeOkayButton:NO];
[self.customAlert removeCancelButton:NO];
NSString *message = [NSString stringWithFormat:#"La tua offerta %# del 20%% è stata convertita in punti IoSi x10", self.promotionSelected];
[self.customAlert showCustomAlertInView:self.view withMessage:message];
isDeletingItem = YES;
}
- (void)customAlertOk {
if (isDeletingItem) {
[self.customAlert removeCustomAlertFromViewInstantly];
} else {
[self.customAlert removeCustomAlertFromView];
}
}
- (void)customAlertCancel {
[self.customAlert removeCustomAlertFromView];
if (isDeletingItem) {
isDeletingItem = NO;
}
}
#end
Maybe you're calling addOrRemoveButtonWithTag:andActionToPerform: at a time where your UI is not fully created, since UI elements are created asynchronously. So if you call this method, right after custom alert view instanciation, you'll get your crash because the buttons in the view are not created.
To solve this issue, you need to call addOrRemoveButtonWithTag:andActionToPerform: only once your custom alert has been added to the view hierarchy.
EDIT :
With the example code you gave in edit 2, you call these lines :
- (IBAction)buttonAlert:(UIButton *)sender {
self.customAlert = [[CustomAlertViewController alloc]init];
[self.customAlert removeOkayButton:NO];
[self.customAlert removeCancelButton:NO];
}
but when you have just instantiated CustomAlertViewController, its 2 buttons are not yet created, so I suggest you add 2 properties hasOkButton and hasCancelButton and a new constructor to your custom class like this one :
- (instancetype) initWithOk:(BOOL)OkButton AndCancel:(BOOL) CancelButton
{
if(self = [super init])
{
hasOkButton = OkButton;
hasCancelButton = CancelButton;
}
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// At this time, the custom UI buttons will be created in the UI view hierarchy
[self removeOkayButton: hasOkButton];
[self removeOkayButton: hasCancelButton];
}
And in the caller you can use the following to display a custom alert View:
- (IBAction)buttonAlert:(UIButton *)sender {
self.customAlert = [[CustomAlertViewController alloc] initWithOk:NO AndCancel:NO];
// ...
}
EDIT #2
I tried your solution in a real project, I made it work by using these lines int the caller :
- (IBAction)buttonAlert:(UIButton *)sender {
self.customAlert = [self.storyboard instantiateViewControllerWithIdentifier:#"customAlertView"];
self.customAlert.hasOK = NO;
self.customAlert.hasCancel = YES;
NSString *message = [NSString stringWithFormat:#"La tua offerta %# del 20%% è stata convertita in punti IoSi x10", self.promotionSelected];
[self.customAlert showCustomAlertInView:self.view withMessage:message];
isDeletingItem = YES;
}
In the CustomAlertViewController declare 2 visible properties hasOK and hasCancel in.h.
And modify your .m by adding method :
-(void)viewWillAppear:(BOOL)animated
{
[self removeOkayButton:self.hasOK];
[self removeCancelButton:self.hasCancel];
}
Be sure to modify your storyboard (if eligible) to have the "customAlertView" defined this way :
Don't forget also to bind your UIButton to the controller this can be a mistake too in your implementation :
Hope this will help you :)
I found on the web a tutorial to create custom alert view by using code, if you are interested you can go to this tutorial. I used it for my issue and it worked great! You have to fix a few things, because it uses deprecated command but it's easy to fix it.
If you are interested just take a look about this tutorial. I think you can integrate it in your app and after you can easily use for other stuff if it's necessary. I hope that my answer will help someone.
I am creating a custom delegate UIAlertView's alert:buttonClickedAtIndex: method, but it is not working properly. I am subclassing a UIView, and I have two buttons that are tagged as 0 and 1. This still does not work when I go to check the delegate for my custom view. Here the code that I did.
Custom View
- (void) buttonTouchedWithIdentifier:(NSInteger)identifier
{
if (identifier == 0) {
[self.delegate alert:self didClickButtonWithTagIdentifier:0];
}
if (identifier == 1) {
[self.delegate alert:self didClickButtonWithTagIdentifier:1];
}
}
* in my showInViewMethod *
[self.dismissButton addTarget:self action:#selector(buttonTouchedWithIdentifier:) forControlEvents:UIControlEventTouchDown];
[self.continueButton addTarget:self action:#selector(buttonTouchedWithIdentifier:) forControlEvents:UIControlEventTouchDown];
self.dismissButton.tag = 0;
self.continueButton.tag = 1;
* in my view controller *
nextLevelAlert = [[ARAlert alloc] init];
nextLevelAlert.delegate = self;
[nextLevelAlert showInView:self.view
withMessage:[NSString stringWithFormat:#"Congratulations, you have completed level %i.\nWould you like to continue?", levelNumber]
dismissButtonTitle:#"Menu"
continueButtonTitle:#"Next Level"];
- (void)alert:(ARAlert *)alert didClickButtonWithTagIdentifier:(NSInteger)tagId
{
if (alert == nextLevelAlert) {
if (tagId == 0) {
NSLog(#"User does not want to continue.");
}
}
}
Now, nextLevelAlert has the delegate set to self, and I do have the delegate declared in my view controller's class. Also, when i do the showInView... for nextLevelAlert, it DOES appear, it is recognizing what button is being pressed.
My guess is your param is not a NSInteger but the button, you should change buttonTouchedWithIdentifier like this :
- (void) buttonTouchedWithIdentifier:(id)sender
{
UIButton *button = (UIButton*)sender;
NSLog(#"buttonTouchedWithIdentifier %#",#(button.tag));
if (button.tag == 0) {
[self.delegate alert:self didClickButtonWithTagIdentifier:0];
}
if (button.tag == 1) {
[self.delegate alert:self didClickButtonWithTagIdentifier:1];
}
}
Also when comparing two objects use isEqual: instead of ==
- (void)alert:(ARAlert *)alert didClickButtonWithTagIdentifier:(NSInteger)tagId
{
if ([alert isEqual:nextLevelAlert]) {
if (tagId == 0) {
NSLog(#"User does not want to continue.");
}
}
}
How to know if there is a UIActionSheet showing in View Hierarchy?
Also I want to get a reference to that UIActionSheet, any good idea?
Notice that on iOS6, you need a recursive search to finde the UIActionSheet, it is not a direct subview of UIWindow now.
static UIView *actionSheet = nil;
-(void)findActionSheetInSubviewsOfView:(id)view
{
if (actionSheet) {
return;
}
if ([[view valueForKey:#"subviews"] count] != 0) {
for (id subview in [view valueForKey:#"subviews"]) {
if ([subview isKindOfClass:[UIActionSheet class]]) {
actionSheet = subview;
}
else
[self findActionSheetInSubviewsOfView:subview];
}
}
}
-(UIActionSheet*) actionSheetShowing {
actionSheet = nil;
for (UIWindow* window in [UIApplication sharedApplication].windows) {
[self findActionSheetInSubviewsOfView:window];
}
if (actionSheet) {
return (UIActionSheet*)actionSheet;
}
return nil;
}