0

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?

Scott Berrevoets
  • 16,921
  • 6
  • 59
  • 80
gklka
  • 2,459
  • 1
  • 26
  • 53

1 Answers1

1

I don't believe you can take a view controller's view and add it to some other view hierarchy.

In your case, I would not use GKSecondViewController. I would create the buttons in the view controller of your object, and use target/action to talk back to the same view controller. If you must use GKSecondViewController, SDCAlertView would have to support view controller containment, which it doesn't at this point.

Scott Berrevoets
  • 16,921
  • 6
  • 59
  • 80