fix out of bounds error in itemWithTitle:

This commit is contained in:
Klaas Pieter Annema committed 2011-04-29 12:01:51 +02:00
1 parent ead6e4a592
commit 0f88207d7b
2 files changed
+22 -11

No files matched your search

+8 -10
View File
@@ -86,10 +86,7 @@ CPPopUpButtonStatePullsDown = CPThemeState("pulls-down");
[self setPullsDown:shouldPullDown];
var options = CPKeyValueObservingOptionNew |
CPKeyValueObservingOptionOld;/* |
CPKeyValueObservingOptionInitial;
*/
var options = CPKeyValueObservingOptionNew |CPKeyValueObservingOptionOld;/* |CPKeyValueObservingOptionInitial;*/
[self addObserver:self forKeyPath:@"menu.items" options:options context:nil];
[self addObserver:self forKeyPath:@"_firstItem.changeCount" options:options context:nil];
[self addObserver:self forKeyPath:@"selectedItem.changeCount" options:options context:nil];
@@ -402,9 +399,13 @@ CPPopUpButtonStatePullsDown = CPThemeState("pulls-down");
*/
- (CPMenuItem)itemWithTitle:(CPString)aTitle
{
var menu = [self menu];
var menu = [self menu],
itemIndex = [menu indexOfItemWithTitle:aTitle];
return [menu itemAtIndex:[menu indexOfItemWithTitle:aTitle]];
if (itemIndex === CPNotFound)
return nil;
return [menu itemAtIndex:itemIndex];
}
/*!
@@ -810,10 +811,7 @@ var DEPRECATED_CPPopUpButtonMenuKey = @"CPPopUpButtonMenuKey",
[self setObjectValue:[aCoder decodeObjectForKey:DEPRECATED_CPPopUpButtonSelectedIndexKey]];
}
var options = CPKeyValueObservingOptionNew |
CPKeyValueObservingOptionOld;/* |
CPKeyValueObservingOptionInitial;
*/
var options = CPKeyValueObservingOptionNew | CPKeyValueObservingOptionOld;/* | CPKeyValueObservingOptionInitial */
[self addObserver:self forKeyPath:@"menu.items" options:options context:nil];
[self addObserver:self forKeyPath:@"_firstItem.changeCount" options:options context:nil];
+14 -1
View File
@@ -4,7 +4,7 @@
@implementation CPPopUpButtonTest : OJTestCase
{
CPPopUpButton button;
CPPopUpButton button @accessors;
}
- (void)setUp
@@ -189,4 +189,17 @@
// [self assert:4990000 equals:[dict objectForKey:@"weight"]];
}
- (void)testItemWithTitle
{
[self assert:nil equals:[[self button] itemWithTitle:@"I dont exist"]];
[[self button] addItemWithTitle:@"one"];
[[self button] addItemWithTitle:@"two"];
[[self button] addItemWithTitle:@"three"];
[self assert:0 equals:[[self button] indexOfItemWithTitle:@"one"]];
[self assert:1 equals:[[self button] indexOfItemWithTitle:@"two"]];
[self assert:2 equals:[[self button] indexOfItemWithTitle:@"three"]];
}
@end