IT tutorials
 
Mobile
 

iPhone : Working with View Controllers - Building a Simple Two-Item Menu & Adding a Segmented Control

10/13/2011 3:28:14 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

Building a Simple Two-Item Menu


Although many applications demand serious user interfaces, sometimes you don’t need complexity. A simple one- or two-button menu can accomplish a lot. Use these steps to create a hand-built interface for simple utilities:

1.
Create a UIViewController subclass that you use to populate your primary interaction space.

2.
Allocate a navigation controller and assign an instance of your custom view controller to its root view.

3.
In the custom view controller, create one or two buttons and add them to the view’s navigation item.

4.
Build the callback routines that get triggered when a user taps a button.

Recipe 1 demonstrates these steps. It creates a simple view controller called TestBedViewController and assigns it as the root view for a UINavigationController. In the loadView method, two buttons populate the left and right custom slots for the view’s navigation item. When tapped, these show an alert, indicating which button was pressed. This recipe is not feature rich, but it provides an easy-to-build two-item menu.

This code uses a handy bar button creation macro. When passed a title and a selector, this macro returns a properly initialized autoreleased bar button item ready to be assigned to a navigation item.

#define BARBUTTON(TITLE, SELECTOR) [[[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]


If you’re looking for more complexity than two items can offer, consider having the buttons trigger UIActionSheet menus. Action sheets,  let users select actions from a short list of options (usually between two and five options, although longer scrolling sheets are possible) and can be seen in use in the Photos and Mail applications for sharing and filing data.

Note

You can add images instead of text to the UIBarButtonItem instances used in your navigation bar. Use initWithImage: style: target: action: instead of the text-based initializer.


Recipe 1. Creating a Two-Item Menu Using a Navigation Controller
@implementation TestBedViewController
- (void) rightAction: (id) sender
{
showAlert(@"You pressed the right button");
}
- (void) leftAction: (id) sender
{
showAlert(@"You pressed the left button");
}

- (void) loadView
{
self.view = [[[NSBundle mainBundle] loadNibNamed:@"mainview"
owner:self options:nil] lastObject];
self.navigationItem.rightBarButtonItem =
BARBUTTON(@"Right",@selector (rightAction:));
self.navigationItem.leftBarButtonItem = BARBUTTON(@"Left",selector(leftAction:));
}
@end

@implementation TestBedAppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
UIWindow *window = [[UIWindow alloc]
initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController *nav = [[UINavigationController alloc]
initWithRootViewController:[[TestBedViewController alloc] init]];
[window addSubview:nav.view];
[window makeKeyAndVisible];
}
@end



Adding a Segmented Control

The preceding recipe showed how to use the two available button slots in your navigation bar to build mini menus. Recipe 2 expands on that idea by introducing a six-item UISegmentedControl and adding it to a navigation bar’s custom title view, as shown in Figure 2. When tapped, each item updates the main view with its number.

Figure 1. Adding a segmented control to the custom title view allows you to build a multi-item menu. Notice that no items remain highlighted even after an action takes place. (In this case, the One button was pressed.)


The key thing to pay attention to in this recipe is the momentary attribute assigned to the segmented control. This transforms the interface from a radio button style into an actual menu of options, where items can be selected independently and more than once. So after tapping item three, for example, you can tap it again. That’s an important behavior for menu interaction.

Unlike Recipe 1, all items in the segmented control trigger the same action (in this case, segmentAction:). Determine which action to take by querying the control for its selectedSegmentIndex and use that value to create the needed behavior. This recipe updates a central text label. You might want to choose different options based on the segment picked.

Note

If you want to test this code with the momentary property disabled, set the selectedSegmentIndex property to match the initial data displayed. In this case, segment 0 corresponds to the displayed number 1.


Segmented controls use styles to specify how they should display. The sample here, shown in Figure 2, uses a bar style. It is designed for use with bars, as it is in this example. The other two styles (UISegmentedControlStyleBordered and UISegmentedControlStylePlain) offer larger, more metallic-looking presentations. Of these three styles, only UISegmentedControlStyleBar can respond to the tintColor changes used in this recipe.

Recipe 2. Adding a Segmented Control to the Navigation Bar
-(void) segmentAction: (UISegmentedControl *) sender
{
// Update the label with the segment number
UILabel *label = (UILabel *)[self.view viewWithTag:101];
[label setText:[NSString stringWithFormat:
@"%0d", sender.selectedSegmentIndex + 1]];
}

- (void) loadView
{
self.view = [[[NSBundle mainBundle] loadNibNamed:@"mainview"
downer:self options:nil] lastObject];

// Create the segmented control
NSArray *buttonNames = [NSArray arrayWithObjects:@"One", @"Two",
@"Three", @"Four", @"Five", @"Six", nil];
UISegmentedControl* segmentedControl = [[UISegmentedControl alloc]
initWithItems:buttonNames];
segmentedControl.segmentedControlStyle =
UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
@selector(segmentAction:[segmentedControl addTarget:self action:)
forControlEvents:UIControlEventValueChanged];

// Add it to the navigation bar
self.navigationItem.titleView = segmentedControl;
[segmentedControl release];
}



 
Others
 
- iPhone : Working with View Controllers - Developing with Navigation Controllers & Utility Function
- Fundamentals of Java ME MIDP Programming : MIDP Security Model (part 3) - The Security Model in Practice
- Fundamentals of Java ME MIDP Programming : MIDP Security Model (part 2)
- Fundamentals of Java ME MIDP Programming : MIDP Security Model (part 1) - The X.509 PKI & Certification Paths
- Fundamentals of Java ME MIDP Programming : Non-GUI APIs in MIDP
- iPad : Advanced Wi-Fi Options (Hidden or Undiscoverable Networks)
- iPad : Wi-Fi Connections
- Beginning Android 3 : Install the ADT for Eclipse & Install Apache Ant
- Install the Android SDK
- The Anatomy of a Mobile Site : Site structure and concepts (part 2) - Entry Points and URLs
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us