Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

FrogNL

macrumors newbie
Original poster
Jun 19, 2010
14
0
I want to use a NSRuleEditor in my application, but I can't make it work... :)

I've added an NSRuleEditor in Interface Builder and I've set its type to list and I've connected its datasource to my class. In that class I've implemented three delegate calls:

Code:
- (NSInteger)ruleEditor:(NSRuleEditor *)editor numberOfChildrenForCriterion:(id)criterion withRowType:(NSRuleEditorRowType)rowType {
	NSLog([NSString stringWithFormat:@"Criterion: %@, %@", criterion, rowType]);
	if (criterion == nil) {
		return 3;
	} else {
		return 0;
	}
}
- (id)ruleEditor:(NSRuleEditor *)editor child:(NSInteger)index forCriterion:(id)criterion withRowType:(NSRuleEditorRowType)rowType {
	NSString *myString = [NSString stringWithFormat:@"Item %d", index];
	return myString;
}
- (id)ruleEditor:(NSRuleEditor *)editor displayValueForCriterion:(id)criterion inRow:(NSInteger)row {
	return criterion;
}

I just want a list of rules with two popups in each row. Each popup should have 3 options. I thought I should return 2 in the first delegate call to have two popups in each row, however that didn't work. Can anyone help me solving this problem?

Thanks!
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
NSRuleEditor example

Well I was kind of curious how NSRuleEditor worked as I've never used it before so I whipped up an example project. The docs aren't that helpful and I didn't find existing examples so here ya go :)

If you've worked with NSTableView or NSOutlineView it's pretty similar. The key is how you setup your data. Once you have that in place, the delegate methods do very little.

I made the code as simple as I could but added some other examples to it beyond just popups. The built-in views you can use are popups and labels. Anything else must (as far as I can tell) be implemented through a custom view, which I do for an NSTextField.
 

Attachments

  • RuleEditorTest.zip
    38.6 KB · Views: 786

FrogNL

macrumors newbie
Original poster
Jun 19, 2010
14
0
Thanks a lot for your sample project! I really liked the way you programmed it, however, I think it's a bit overkill for me. I only have 1 type of row with 2 popups with 3 options each. I think I can do that with less code than you wrote, however, I can't get it working.

I've changed my code again to this:

Code:
- (void)awakeFromNib {
	criteria = [[NSMutableArray alloc] init];
	
	NSMutableArray *criteria1 = [[NSMutableArray alloc] initWithObjects:@"Tweet", @"Follow", @"Retweet", nil];	
	[criteria addObject:criteria1];
	
	NSMutableArray *criteria2 = [[NSMutableArray alloc] initWithObjects:@"All", @"Checked", @"Selected", nil];	
	[criteria addObject:criteria2];
}

- (NSInteger)ruleEditor:(NSRuleEditor *)editor numberOfChildrenForCriterion:(id)criterion withRowType:(NSRuleEditorRowType)rowType {
	NSLog([NSString stringWithFormat:@"int: %@", criterion]);
	
	if (criterion == nil) {
		return [criteria count];
	} else if (criterion == @"type") {
		return [[criteria objectAtIndex:0] count];
	} else if (criterion == @"selection") {
		return [[criteria objectAtIndex:1] count];
	} else {
		return 0;
	}
}

- (id)ruleEditor:(NSRuleEditor *)editor child:(NSInteger)index forCriterion:(id)criterion withRowType:(NSRuleEditorRowType)rowType {
	NSLog([NSString stringWithFormat:@"string: %@", criterion]);
	
	if (criterion == nil) {
		if (index == 0) {
			return @"type";
		} else {
			return @"selection";
		}
	} else if (criterion == @"type") {
		return [[criteria objectAtIndex:0] objectAtIndex:index];
	} else if (criterion == @"selection") {
		return [[criteria objectAtIndex:1] objectAtIndex:index];
	} else {
		return nil;
	}
}

- (id)ruleEditor:(NSRuleEditor *)editor displayValueForCriterion:(id)criterion inRow:(NSInteger)row {
	return criterion;
}

This doesn't work. I know this is bad code, but I just wanted to get it working and I will refine my code when it works. Can you tell me why this doesn't work.

Thanks!
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
You need to be more specific. "doesn't work" is too vague. What are the results you're expecting? What are the results you're seeing?
 

FrogNL

macrumors newbie
Original poster
Jun 19, 2010
14
0
Sorry for being unclear. I expect to see two popups in each row with both 3 options, one Tweet, Follow and Retweet and the other one All, Checked and Selected.

I see one popup with two options (all, selected) and one popup with three options (Tweet, Follow, Retweet).

I think I just misunderstand the delegate calls, but I don't know how to solve it. Thanks a lot for your help thus far.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
If you want to do it that way, then these two methods need to be like this:

Code:
- (NSInteger)ruleEditor:(NSRuleEditor *)editor numberOfChildrenForCriterion:(id)criterion withRowType:(NSRuleEditorRowType)rowType {
    if (criterion == nil)
        return [[criteria objectAtIndex:0] count];
    else if ([[criteria objectAtIndex:0] containsObject:criterion])
        return [[criteria objectAtIndex:1] count];
    return 0;
}

- (id)ruleEditor:(NSRuleEditor *)editor child:(NSInteger)index forCriterion:(id)criterion withRowType:(NSRuleEditorRowType)rowType {
    if (criterion == nil)
        return [[criteria objectAtIndex:0] objectAtIndex:index];
    return [[criteria objectAtIndex:1] objectAtIndex:index];
}

I suggest studying my original project to better understand how it all works. It's not very easy to explain in text.
 

FrogNL

macrumors newbie
Original poster
Jun 19, 2010
14
0
Thanks a lot! Thanks to your code I now understand the way NSRuleEditor works. I thought you should define everything over the x-axis and then y-axis, while it is the other way around. Thanks!

I'm also going to look into your example a bit more, however I think it's a bit overkill. Thanks!
 

jmbldwn

macrumors newbie
Oct 18, 2013
1
0
So close...

I'm thinking of using NSRuleEditor to make a very simple workflow sequence editor. The example project from kainjow is almost exactly what I need, but I'm missing one thing.

I need some of my criteria to have two or more text fields, so I can require multiple parameters for those rules.

If I make a criteria with two TextFields as children, it makes a popup where I can pick one or the other. How would you make a multiple text field row?

Thanks!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.