diff --git a/URBSegmentedControl.m b/URBSegmentedControl.m index 73469ba..ed98b2e 100644 --- a/URBSegmentedControl.m +++ b/URBSegmentedControl.m @@ -39,6 +39,7 @@ - (UIImage *)defaultBackgroundImage; @implementation URBSegmentedControl { NSInteger _selectedSegmentIndex; NSInteger _lastSelectedSegmentIndex; + NSInteger _initInternalFinished; } - (void)initInternal{ @@ -72,6 +73,7 @@ - (void)initInternal{ _backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; _backgroundView.frame = self.frame; [self insertSubview:_backgroundView atIndex:0]; + _initInternalFinished = YES; } - (id)init { @@ -325,7 +327,7 @@ - (BOOL)isEnabledForSegmentAtIndex:(NSUInteger)segment { } - (void)setSelectedSegmentIndex:(NSInteger)selectedSegmentIndex { - if (_selectedSegmentIndex != selectedSegmentIndex) { + if (_selectedSegmentIndex != selectedSegmentIndex && _initInternalFinished) { NSParameterAssert(selectedSegmentIndex < (NSInteger)self.items.count && selectedSegmentIndex >= 0); // deselect current segment if selected