UISegmentedControl, UIToolbar and UINavigationItem
Asked Answered
K

1

15

I've create a UISegmentedControl and successfully attached it to my navigationItem.tableView.

But when I try instead to attach it to a UIToolbar, it blows up.

I'm sure I've seen UISegementedControls on toolbars before - but can't seem to get it working. Thoughts?

// works
NSArray *statusItems = [[NSArray alloc] initWithObjects:@"one", @"two", nil];
statusSegments_ = [[UISegmentedControl alloc] initWithItems:statusItems];
self.navigationItem.titleView = statusSegments_;

// doesn't work
NSArray *statusItems = [[NSArray alloc] initWithObjects:@"one", @"two", nil];
statusSegments_ = [[UISegmentedControl alloc] initWithItems:statusItems];
NSArray *toolbarItems = [[NSArray alloc] initWithObjects:statusSegments_, nil];
self.toolbarItems = toolbarItems;
[toolbarItems release];
[Session started at 2010-01-01 13:40:35 -0600.]
2010-01-01 13:40:35.182 TimeSheet[15382:20b] *** -[UISegmentedControl view]: unrecognized selector sent to instance 0x3f5c3e0
2010-01-01 13:40:35.183 TimeSheet[15382:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UISegmentedControl view]: unrecognized selector sent to instance 0x3f5c3e0'
2010-01-01 13:40:35.184 TimeSheet[15382:20b] Stack: (
    10847323,
    2479902267,
    11229243,
    10798710,
    10651330,
    24428622,
    24429410,
    24133352,
    24133878,
    11379,
    24149733,
    24130330,
    24151483,
    24129979,
    23770,
    23906970,
    23890136,
    2838970,
    10631872,
    10628168,
    960429,
    960626,
    23633923
)
Kizzie answered 1/1, 2010 at 20:11 Comment(0)
L
34

You need to wrap the segmented control view in a UIBarButtonItem:

UIBarButtonItem *item = [[[UIBarButtonItem alloc] initWithCustomView:statusSegments_] autorelease];
Lobbyism answered 1/1, 2010 at 20:13 Comment(3)
Thanks. That worked - but having a hard time finding that documented? Do you have a link?Kizzie
Just were it belongs, the UIToolbar documentation: developer.apple.com/iphone/library/documentation/UIKit/…Lobbyism
Thanks, that link led to what I should have seen earlier: "The UIBarButtonItem class encapsulates the properties and behaviors of items added to UIToolbar and UINavigationBar objects." developer.apple.com/iphone/library/documentation/UIKit/…Kizzie

© 2022 - 2024 — McMap. All rights reserved.