在以编程方式添加到视图时,UISlider会崩溃应用程序

时间:2013-05-17 14:24:04

标签: ios objective-c uislider

我有相机的自定义叠加层,它覆盖了我 UISlider

@implementation LWOverlayView

- (id)initWithFrame:(CGRect)frame imagePath:(NSString*) imagepath{
    if ((self = [super initWithFrame:frame])) {
        // Clear the background of the overlay:
        self.opaque = NO;
        self.backgroundColor = [UIColor clearColor];


        UIImageView *overlayGraphicView = [[UIImageView alloc] init];
        overlayGraphicView.contentMode = UIViewContentModeScaleAspectFill;
        [overlayGraphicView setImageWithURL:[NSURL URLWithString:imagepath]
                       placeholderImage:[UIImage imageNamed:@"placeholder.png"]];


        overlayGraphicView.frame = CGRectMake(80, 140, 160, 120);
        [self addSubview:overlayGraphicView];

        UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 436, 320, 44)];
        [toolbar setBarStyle:UIBarStyleBlackOpaque];

        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismiss)];

        CGRect frame = CGRectMake(0.0, 0.0, 200.0, 10.0);
        UISlider *slider = [[UISlider alloc] initWithFrame:frame];
        [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
        [slider setBackgroundColor:[UIColor clearColor]];
        slider.minimumValue = 0.0;
        slider.maximumValue = 50.0;
        slider.continuous = YES;
        slider.value = 25.0;


        [toolbar setItems:[NSArray arrayWithObjects:doneButton, slider, nil]];

        [self addSubview:toolbar];

    }
    return self;
}

App崩溃:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UISlider view]: unrecognized selector sent to instance 0x20854fe0'

任何提示?

3 个答案:

答案 0 :(得分:4)

正如已经指出的那样,您不能直接将UISlider(或任何其他UIView)直接添加到工具栏项列表中。

您需要做的是创建一个包含滑块的UIBarButtonItem

UIBarButtonItem *sliderBtn = [[UIBarButtonItem alloc] initWithCustonView:slider];

现在将sliderBtn添加到数组而不是slider

您可能会发现需要在width上设置sliderBtn属性。

答案 1 :(得分:2)

您无法将滑块添加到工具栏:

[toolbar setItems:[NSArray arrayWithObjects:doneButton, slider, nil]];

这就是破坏你的代码,你只能添加UIBarButtonItem

答案 2 :(得分:0)

工具栏可以包含UIBarButtonItem项。 UISlider不是UIBarButtonItem。这可能与崩溃有关。尝试删除滑块并查看其工作原理。