ios - SDCAlertView 和 UIViewController 与 UIButton

标签 ios objective-c uigesturerecognizer sdcalertview

我必须在中心显示一个自定义 iOS 7 风格的警报请求程序,其中包含一组自定义按钮(具体来说是类别过滤器切换)。为此,我找到了优秀的 SDCAlertView在 GitHub 上。我的方法是创建一个自定义 UIViewController,它处理按钮的创建和按钮触摸,实例化它,然后像这样插入到警报的 contentView 中:

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];

我的 View Controller (GKSecondViewController)看起来像这样:

- (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];
}

(您可能还需要 SDCAutoLayout。)当我单击警报中的按钮时,它崩溃了,跟踪日志中没有任何提示。我错过了什么或做错了什么?

最佳答案

我认为您无法将 View Controller 的 View 添加到其他 View 层次结构中。

在您的情况下,我不会使用 GKSecondViewController。我会在你的对象的 View Controller 中创建按钮,并使用目标/ Action 来返回同一个 View Controller 。如果您必须使用 GKSecondViewController,则 SDCAlertView 必须支持 View Controller 包含,但目前不支持。

关于ios - SDCAlertView 和 UIViewController 与 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21206699/

相关文章:

ios - 检索 tableview Swift Realm 的 List 属性计数

iOS Swift keyCommands 仅读取数字

iphone - 做一个小型开源iOS API时,ARC用户和非ARC用户如何计算

iphone - 两次之间的 Objective-C 中点

ios - 从 UIPanGestureRecognizer 获取 UITouch

ios - cordova 在 Windows 上构建 IOS

ios - 自定义 View 单元格中的单元格间距

ios - 选项卡栏 Controller 项目中 2 个 ViewController 之间的通信

ios - 捏合时图像在拉伸(stretch)

iphone - iOS 按住手势 + 点击