English 中文(简体)
How do you create UIBarButtonItems with a radio interface?
原标题:

I have a UIToolbar that needs three buttons in a radio style, meaning that of the three, only one button can be pushed at a time. The documentation makes reference to the possibility of setting up radio UIBarButtonItems in the class reference definition of the width property:

If this property value is positive, the width of the combined image and title are fixed. If the value is 0.0 or negative, the item sets the width of the combined image and title to fit. This property is ignored if the style uses radio mode. The default value is 0.0.

However, I did a find for "radio" in the UIKit Framework Reference and I can t find any mention of UIBarButtonItems in radio style. I know that I could alternatively use a TabBar for a radio interface, but a TabBar doesn t quite match the purpose of my UI (normal buttons + radio buttons). I see that the Calendar App uses UIBarButtonItems in a radio style (List, Day, Month), so it seems like this should be somewhere in the API and approved by the HIG. Is this hiding somewhere or would I have to create UIBarButtonItems with custom views?

最佳回答

A UISegmentedControl is what you want. It s kind of hiding in Interface Builder, as it s a different style outside of a toolbar.

The normal style:

normal segmented control

The same thing in a toolbar:

bar segmented control

You have two options for its behavior: a momentary highlight when tapped, or a radio-style behavior, which is what you want. You can set this with the "Momentary" checkbox in the Attributes inspector:

segmented control attributes

问题回答

Have you considered trying a UISegmentedControl? You can set it up so that only one of the segments is "pressed" at a time.

If you need to do this in objc in real time

  1. Create an array of stirings with your choices for radio button
  2. create and put your UISegmentedControl into a UIBarButtonItem.
  3. add that UIBarButtonItem to your navigationItem

worked for me. Code is below

NSArray *itemArray = [NSArray arrayWithObjects: @"One", @"Two", @"Three", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(35, 200, 250, 50);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.selectedSegmentIndex = 1;

// Uncomment this part if you need to do something when ratio state is selected. Also paste the function at the end of this post somewhere in your class.
// [segmentedControl addTarget:self   action:@selector(pickOne:) forControlEvents:UIControlEventValueChanged];

UIBarButtonItem *bbi= [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];


/* 
//and paste this function somethere in your class. It prints the label in debug terminal
- (void) pickOne:(id)sender{
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
NSLog(@"%@", [segmentedControl titleForSegmentAtIndex: [segmentedControl selectedSegmentIndex]]);
} 
*/

I used some code from this (not mine) post http://howtomakeiphoneapps.com/here-is-how-you-use-the-segmented-control-uisegmentedcontrol/129/





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签