如何创建具有多个"部分"的UIBarButtonItem?

Tod*_*odd 2 iphone cocoa-touch objective-c

我想在我的iPhone应用程序上创建一个UIBarButtonItem,它有两个"部分".基本上我希望功能相当于YouTube iPhone应用程序"观看最多"部分中的"今天","本周"和"全部"按钮.

看起来这个功能不是用多个UIBarButtonItem实现的,因为一次只能选择三个"部分"中的一个.有谁知道这是怎么做的?

Jim*_*vey 7

你看到的是一个UISegmentedControl.这很容易设置.YouTube正在做的事情(可能)是这样的:

NSArray * items = [[NSArray alloc] initWithObjects: NSLocalizedString(@"Today", @""),
                      NSLocalizedString(@"This Week", @""),
                      NSLocalizedString(@"All", @""), nil];
UISegmentedControl * switcher = [[UISegmentedControl alloc] initWithItems: items];
[items release];

// setup the switcher: correct UI style, tint, etc.
switcher.style = UISegmentedControlStyleBar;
switcher.tint = self.navigationController.navigationBar.tintColor; // unnecessary?

// set the target function -- needs to check selectedIndex property
[switcher addTarget: self 
             action: @selector(switcherTapped:)
   forControlEvents: UIControlEventValueChanged];

// set default selection
switcher.selectedSegmentIndex = 0;

// set the switcher as a custom view to use in place of the normal title
self.navigationItem.titleView = switcher;
[switcher release];
Run Code Online (Sandbox Code Playgroud)