0

XcodeでiPad用のマスター/詳細プロジェクトを作成しました。次に、ボタンと、ボタンがトリガーするアクション(IBのアクションにボタンを接続)を追加し、3つのボタンを持つUIAlertViewを追加してから、UIAlertViewDelegateを.hファイルに追加しました。

ボタンを押すとAlertViewが表示されますが、AlertView内のいずれかのボタンをクリックすると、.mファイルのUIAlertViewDelegateメソッドは呼び出されません。

これはプロジェクトへのダウンロードリンクですここにリンクの説明を入力してください

私は以下のコードを使用しています...

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

どんな助けでも大歓迎です。私は以前に同じコードを使用し、以前は問題なくUIAlertViewsを実装しましたが、ここで何が起こっているのかわかりません...

4

1 に答える 1

1
[[UIAlertView alloc] initWith... delegate:nil
                                          ^^^

デリゲートを に設定している場合nil、どのようになると思いますselfか?

于 2013-01-14T17:38:05.557 に答える