Ray's playground

 

Working with view controllers(Chapter 5 of The iPhone™ Developer’s Cookbook)

 1 #import <UIKit/UIKit.h>
 2 
 3 #define COOKBOOK_PURPLE_COLOR    [UIColor colorWithRed:0.20392f green:0.19607f blue:0.61176f alpha:1.0f]
 4 #define BARBUTTON(TITLE, SELECTOR)     [[[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]
 5 
 6 @interface TestBedViewController : UIViewController
 7 @end
 8 
 9 @implementation TestBedViewController
10 
11 // Return an exhaustive descent of the view's subviews
12 NSArray *allSubviews(UIView *aView)
13 {
14     NSArray *results = [aView subviews];
15     for (UIView *eachView in [aView subviews])
16     {
17         NSArray *riz = allSubviews(eachView);
18         if (riz) results = [results arrayByAddingObjectsFromArray:riz];
19     }
20     return results;
21 }
22 
23 -(void) segmentAction: (UISegmentedControl *) sender
24 {
25     // Update the label with the segment number
26     UILabel *label = (UILabel *)[self.view viewWithTag:101];
27     [label setText:[NSString stringWithFormat:@"%0d", sender.selectedSegmentIndex + 1]];
28 }
29 
30 - (void) loadView
31 {
32     self.view = [[[NSBundle mainBundle] loadNibNamed:@"mainview" owner:self options:nil] lastObject];
33     self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR;
34     
35     // Create the segmented control. Choose one of the three styles
36     NSArray *buttonNames = [NSArray arrayWithObjects:@"One"@"Two"@"Three"@"Four"@"Five"@"Six", nil];
37     UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:buttonNames];
38     segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; 
39     [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
40 
41 // For menus, the momentary behavior is preferred. Otherwise, the segmented control
42 // provides a radio-button style interface
43 #define TESTWITHMOMENTARY        1
44     
45 #if TESTWITHMOMENTARY == 1
46     segmentedControl.momentary = YES;
47 #else
48     segmentedControl.momentary = NO;
49     segmentedControl.selectedSegmentIndex = 0;
50 #endif
51     
52     CFShow(allSubviews(segmentedControl));
53 
54 
55     // Add it to the navigation bar
56     self.navigationItem.titleView = segmentedControl;
57     [segmentedControl release];
58 }
59 @end
60 
61 @interface TestBedAppDelegate : NSObject <UIApplicationDelegate>
62 @end
63 
64 @implementation TestBedAppDelegate
65 - (void)applicationDidFinishLaunching:(UIApplication *)application {    
66     UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
67     UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[TestBedViewController alloc] init]];
68     [window addSubview:nav.view];
69     [window makeKeyAndVisible];
70 }
71 @end
72 
73 int main(int argc, char *argv[])
74 {
75     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
76     int retVal = UIApplicationMain(argc, argv, nil, @"TestBedAppDelegate");
77     [pool release];
78     return retVal;
79 }
80 

 

posted on 2010-12-27 18:46  Ray Z  阅读(226)  评论(0编辑  收藏  举报

导航