I have to display a custom iOS 7-style alert requester with a set of custom buttons (category filter toggles to be specific) in the center. For this, I've found the excellent SDCAlertView on GitHub. My approach is to create a custom UIViewController
, which handles the creation of buttons and the button touches, instantiate it, then insert into the alert's contentView
like this:
SDCAlertView *alert = [[SDCAlertView alloc] initWithTitle:@"Filter"
message:nil
delegate:self
cancelButtonTitle:@"Clear"
otherButtonTitles:@"Filter", nil];
GKSecondViewController *vc = [[GKSecondViewController alloc] init];
UIView *view = vc.view;
[alert.contentView addSubview:view];
[view sdc_centerInSuperview];
[alert.contentView sdc_pinHeight:100];
[alert.contentView sdc_pinWidth:100];
[alert.contentView setBackgroundColor:[UIColor redColor]];
[alert show];
My view controller (GKSecondViewController
) looks like this:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self.view setBackgroundColor:[UIColor grayColor]];
[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view sdc_pinHeight:100];
[self.view sdc_pinWidth:100];
UIButton *button = [[UIButton alloc] init];
[button setTitle:@"Button" forState:UIControlStateNormal];
[button addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]];
[button setTranslatesAutoresizingMaskIntoConstraints:NO];
[button sdc_pinHeight:100];
[button sdc_pinWidth:100];
[self.view addSubview:button];
[button sdc_centerInSuperview];
}
return self;
}
- (void)tap:(UIGestureRecognizer *)gesture
{
gesture.view.backgroundColor = [UIColor blackColor];
}
(You may need SDCAutoLayout too.) When I click on the button in the alert, it crashes with no hints in the tracelog. What do I miss or do wrong?