Невозможно полностью отказаться от делегирования, но вы можете создать обертку для этого эффекта по этим линиям:
@interface MyAlertViewDelegate : NSObject<UIAlertViewDelegate>
typedef void (^AlertViewCompletionBlock)(NSInteger buttonIndex);
@property (strong,nonatomic) AlertViewCompletionBlock callback;
+ (void)showAlertView:(UIAlertView *)alertView withCallback:(AlertViewCompletionBlock)callback;
@end
@implementation MyAlertViewDelegate
@synthesize callback;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
callback(buttonIndex);
}
+ (void)showAlertView:(UIAlertView *)alertView
withCallback:(AlertViewCompletionBlock)callback {
__block MyAlertViewDelegate *delegate = [[MyAlertViewDelegate alloc] init];
alertView.delegate = delegate;
delegate.callback = ^(NSInteger buttonIndex) {
callback(buttonIndex);
alertView.delegate = nil;
delegate = nil;
};
[alertView show];
}
@end
(ARC предполагается, если вы не используете его изменить delegate = nil
к [delegate release]
.)
Usage будет что-то вроде:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Confirm" message:@"Yes or No?" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes",@"No", nil];
[MyAlertViewDelegate showAlertView:alert withCallback:^(NSInteger buttonIndex) {
// code to take action depending on the value of buttonIndex
}];
UIAlertView без делегации есть только один способ, что и создать пользовательский UIAlertView. – ios
Вот сайт, который реализует блочный UIAlertview (наряду с другими вещами): http://blog.mugunthkumar.com/coding/ios-code-block-based-uialertview-and-uiactionsheet/ – lnafziger
UIAlertView * aletView = [[UIAlertView alloc] initWithTitle: @ Сообщение «Any Title»: @ «Hiii» делегат: self cancelButtonTitle: @ «Отменить» otherButtonTitles: nil, nil]; [alert show]; не используйте делегатов, если хотите. –