UISegmentedControl,UIToolbar和UINavigationItem (说明:以上是三个不同的iOS界面元素)

15

我已创建了一个UISegmentedControl并成功将其附加到我的navigationItem.tableView。

但当我尝试将其附加到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];
[会话开始于2010年01月01日13:40:35 -0600。]
2010-01-01 13:40:35.182 TimeSheet[15382:20b] ***-[UISegmentedControl view]:无法识别的选择器发送给实例0x3f5c3e0
2010-01-01 13:40:35.183 TimeSheet[15382:20b] ***由于未捕获的异常:“NSInvalidArgumentException”,原因:“***-[UISegmentedControl view]:无法识别的选择器发送给实例0x3f5c3e0”终止应用程序
2010-01-01 13:40:35.184 TimeSheet[15382:20b] 堆栈: (
    10847323,
    2479902267,
    11229243,
    10798710,
    10651330,
    24428622,
    24429410,
    24133352,
    24133878,
    11379,
    24149733,
    24130330,
    24151483,
    24129979,
    23770,
    23906970,
    23890136,
    2838970,
    10631872,
    10628168,
    960429,
    960626,
    23633923
)
1个回答

34

你需要将分段控件视图包裹在一个UIBarButtonItem中:

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

谢谢。那个方法可行 - 但是我很难找到相关文档,你有链接吗? - Luther Baker
正如它应该在的地方,UIToolbar文档:http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIToolbar_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006927-CH3-SW8 - Joost
谢谢,那个链接让我看到了之前应该看到的内容:“UIBarButtonItem类封装了添加到UIToolbar和UINavigationBar对象中的项目的属性和行为。”http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIBarButtonItem_Class/Reference/Reference.html - Luther Baker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接