I created a Master-Detail project for the iPad in Xcode. I then added a button and an action that the button triggers (I connected the button to the action in IB) an UIAlertView with three buttons, then added the UIAlertViewDelegate to the .h file.
When the button is pressed the AlertView is displayed but when clicking on any button inside the AlertView the UIAlertViewDelegate method in the .m file is not called.
This is a download link to the project enter link description here
I am using the code below...
DetailViewController.h
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController <UISplitViewControllerDelegate, UIAlertViewDelegate>
@property (strong, nonatomic) id detailItem;
@property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
-(IBAction)triggerAlert:(id)sender;
@end
DetailViewController.m
#import "DetailViewController.h"
@interface DetailViewController ()
@property (strong, nonatomic) UIPopoverController *masterPopoverController;
- (void)configureView;
@end
@implementation DetailViewController
#pragma mark - Managing the detail item
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
if (self.masterPopoverController != nil) {
[self.masterPopoverController dismissPopoverAnimated:YES];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
self.detailDescriptionLabel.text = [self.detailItem description];
}
}
-(IBAction)triggerAlert:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertMessage Title" message:@"UIAlertMessage" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Button 1", @"Button 2", @"Button 3", nil];
[alert show];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(@"Detail", @"Detail");
}
return self;
}
#pragma mark - Split view
- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
barButtonItem.title = NSLocalizedString(@"Master", @"Master");
[self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
self.masterPopoverController = popoverController;
}
- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
// Called when the view is shown again in the split view, invalidating the button and popover controller.
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
self.masterPopoverController = nil;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"This code was run!");
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"Button 1"])
{
NSLog(@"Button 1 was selected.");
}
else if([title isEqualToString:@"Button 2"])
{
NSLog(@"Button 2 was selected.");
}
else if([title isEqualToString:@"Button 3"])
{
NSLog(@"Button 3 was selected.");
}
}
@end
Any help would be greatly appreciated. I have used the same code before and have implemented UIAlertViews without any problems before and am not sure what is going on here...
[[UIAlertView alloc] initWith... delegate:nil
^^^
If you're setting the delegate to nil
, how do you expect it to be self
?