あなたが直面している問題については、即時の修正として、「alert」をローカル オブジェクトとして保持するのではなく、クラスの強力なプロパティとして宣言してみてください。
ただし、customAlert のプロパティとして「UIAlertView」を使用するのではなく、「customAlert」を「UIAlertView」のサブクラスとして保持することをお勧めします。
カスタム アラート クラスの例 (コメントはあまり追加していません。コードは単純で自己記述的です)。
CustomAlert.h
#import <UIKit/UIKit.h>
@protocol customAlertDelegate<NSObject>
- (void)pressedOnYES;
- (void)pressedNO;
@end
@interface CustomAlert : UIAlertView
- (CustomAlert *)initWithDelegate:(id)delegate;
@property (weak) id <customAlertDelegate> delegate1;
@end
CustomAlert.m
#import "CustomAlert.h"
@implementation CustomAlert
@synthesize delegate1;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (CustomAlert *)initWithDelegate:(id)delegate
{
self = [super initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
if (self) {
//Assigning an object for customAlertDelegate
self.delegate1 = delegate;
}
return self;
}
//Method called when a button clicked on alert view
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex) {
[self.delegate1 pressedOnYES];
} else {
[self.delegate1 pressedNO];
}
}
@end
デリゲート メソッドを使用したビュー コントローラー
ViewController.h
#import <UIKit/UIKit.h>
#import "CustomAlert.h"
@interface ViewController : UIViewController <customAlertDelegate>
@end
ViewController.m
#import "ViewController.h"
@implementation ViewController
- (IBAction)pressBtn:(id)sender
{
CustomAlert *alert=[[CustomAlert alloc] initWithDelegate:self] ;
[alert show];
}
- (void)pressedOnYES
{
//write code for yes
}
- (void)pressedNO
{
//write code for No
}
@end