Skip to content

Instantly share code, notes, and snippets.

@yaoxinghuo
Created October 7, 2013 08:14
Show Gist options
  • Save yaoxinghuo/6864249 to your computer and use it in GitHub Desktop.
Save yaoxinghuo/6864249 to your computer and use it in GitHub Desktop.
Block应用 UIAlertView来实现点击回调
//
// TRYBlockAlertView.h
// HelloWorld
//
// Created by Terry on 2013-10-07.
// Copyright (c) 2013 Terry. All rights reserved.
//
#import <UIKit/UIKit.h>
typedef void(^ClickedBlock)(NSInteger);
@interface TRYBlockAlertView : UIAlertView
- (id)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitle:(NSString *)otherButtonTitle clickedBlock:(ClickedBlock) clickedBlock;
@end
//
// TRYBlockAlertView.m
// HelloWorld
//
// Created by Terry on 2013-10-07.
// Copyright (c) 2013 Terry. All rights reserved.
//
#import "TRYBlockAlertView.h"
@implementation TRYBlockAlertView
ClickedBlock clickedBlock;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (id)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitle:(NSString *)otherButtonTitle clickedBlock:(ClickedBlock) block {
self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitle, nil];
if (self != nil) {
clickedBlock = block;
}
return self;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
clickedBlock(buttonIndex);
}
@end
//
// TRYViewController.m
// HelloWorld
//
// Created by Terry on 2013-10-07.
// Copyright (c) 2013 Terry. All rights reserved.
//
#import "TRYViewController.h"
#import "TRYBlockAlertView.h"
@interface TRYViewController ()
@end
@implementation TRYViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)btnClick:(id)sender {
TRYBlockAlertView *alert = [[TRYBlockAlertView alloc]initWithTitle:@"Title" message:@"Message" cancelButtonTitle:@"Cancel" otherButtonTitle:@"Other" clickedBlock:^(NSInteger index) {
switch (index) {
case 0:
NSLog(@"点了取消");
break;
case 1:
NSLog(@"点了 Other");
break;
default:
break;
}
}];
[alert show];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment