2010-01-01 59 views
15

我已经创建了一个UISegmentedControl,并成功将它连接到我的navigationItem.tableView。UISegmentedControl,UIToolbar和UINavigationItem

但是,当我尝试将其附加到UIToolbar,它爆炸了。

我确定我以前在工具栏上看过UISegementedControls - 但似乎无法使它工作。思考?

// 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 
)

回答

34

你需要用一个UIBarButtonItem分段控制视图:

UIBarButtonItem *item = [[[UIBarButtonItem alloc] initWithCustomView:statusSegments_] autorelease]; 
+0

谢谢。这工作 - 但很难找到记录?你有链接吗? – 2010-01-01 20:45:03

+0

只是它属于“UIToolbar”文档:http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIToolbar_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006927- CH3-SW8 – Joost 2010-01-01 21:14:43

+0

谢谢,该链接导致了我之前应该看到的内容:“UIBarButtonItem类封装了添加到UIToolbar和UINavigationBar对象的项目的属性和行为。” http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIBarButtonItem_Class/Reference/Reference.html – 2010-01-03 03:27:17