`

UINavigationController

阅读更多

http://www.cnblogs.com/mac_arthur/archive/2010/01/18/1650762.html

[quote=""]UINavigationController iPhone导航控制器/导航栏 是在iPhone程序中广为使用的用户数据互动方式。


这是一个简单的导航栏截图,我们可以设置其内置UIView的title,而导航栏会显示出这个title。而不是设置导航栏的title。我们也可以设置其左侧,或者右侧的按钮或者自定义视图对象。我们下面来一步一步的看看导航栏的使用:

创建并使用一个UINavigationController


UINavigationController *aNav = [[UINavigationController alloc] init];


然后添加一个视图进去,否则导航栏也没有意义的


UIViewController *aView = [[UIView alloc] initWithNibName: (*xib文件名*)];

[aNav pushViewController:aView animated:NO];

//导航栏的第一个视图不要动画化


设置导航栏的左右按钮:

我说过,设置导航栏的按钮并不是去设置导航栏本身,而是当时被导航的视图控制器,比如我们对aView作设置。

设置其标题:


aView.title = @"标题";



UIBarButtonItem *callModalViewButton = [[UIBarButtonItem alloc]

initWithTitle:@"Button"

style:UIBarButtonItemStyleBordered

target:self

action:@selector(callModalList)];

self.navigationItem.leftBarButtonItem = callModalViewButton;

[callModalViewButton release]; //由于本地视图会retain它,所以我们可以release了


可以看到,还是很简单的嘛。

其他常用方法和属性:


本地视图.navigationItem.leftBarButtonItem //左边栏项目

本地视图.navigationItem.rightBarButtonItem //右边栏项目

本地视图.navigationItem.backBarButtonItem //后退栏项目


本地视图.navigationItem.hidesBackButton //隐藏后退按钮(YES or NO)




Navigation Controller 是最重要的iPhone组建之一了,以下是一些“关键方法”


pushViewController:viewController animated:BOOL


(加载视图控制器)

– 添加指定的视图控制器并予以显示,后接:是否动画显示


popViewControllerAnimated:BOOL


(弹出当前视图控制器)

– 弹出并向左显示前一个视图


popToViewController:viewController animated:BOOL


(弹出到指定视图控制器)

– 回到指定视图控制器, 也就是不只弹出一个


popToRootViewControllerAnimated:BOOL


(弹出到根视图控制器)

– 比如说你有一个“Home”键,也许就会实施这个方法了。


setNavigationBarHidden:BOOL animated:BOOL


(设置导航栏是否显示)

– 如果你想隐藏导航栏,这就是地方了。参照Picasa的WebApp样式




Navigation Controller 模式弹出新的Navigation Controller








UINavigationController *navController = [[UINavigationController alloc]      initWithRootViewController:addViewController];


    [self.navigationController presentModalViewController:navController animated:YES];


或:    SubView *printView=[[EIPrintPreprint alloc] initWithNibName:@"SubView" bundle:nil];    [self presentModalViewController:printView animated:YES];    [printView release];


实现pushViewController:animated:的不同页面转换特效




1. 首先要明确的是,不使用pushViewController的默认动画,所以在调用这个函数时,要将animated设置为NO.

2. 使用普通的来CATransition实现转换效果,代码如下:



CATransition *animation = [CATransition animation];


[animation setDuration:0.3];


[animation setType: kCATransitionMoveIn];


[animation setSubtype: kCATransitionFromTop];


[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];




    [self.navigationController pushViewController:m_poseAddIssueViewController animated:NO];




    [self.navigationController.view.layer addAnimation:animation forKey:nil];


1,add a navigationcontroller to window


UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

//set a root controller

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[HelloController alloc] init]];

[window addSubview:nav.view];

[window makeKeyAndVisible];





2.set title with info.plist

self.title = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];



3.custom back button


UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStylePlain 

                  target:nil action:nil];

 self.navigationItem.backBarButtonItem = backButton;

 [backButton release];




4.add a segment to navigationItem

 NSArray *buttonNames = [NSArray arrayWithObjects:@"One", @"Two", @"Three", @"Four", nil];

 UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:buttonNames];

 segmentedControl.momentary = YES;

 [(UITextView *)self.view setText:@""];

 

 segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;

 segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;

 segmentedControl.frame = CGRectMake(0, 0, 400, 30);

 [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];

 

 self.navigationItem.titleView = segmentedControl;

 [segmentedControl release];





5.toolbar in navigation

 NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:4];



 UIBarButtonItem *flexibleSpaceItem;

 flexibleSpaceItem = [[[UIBarButtonItem alloc] 

        initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace

        target:nil action:NULL] autorelease]; 

 [buttons addObject:flexibleSpaceItem];

 [flexibleSpaceItem release];

 

 UIBarButtonItem *item;

 item = [[UIBarButtonItem alloc] 

   initWithImage:[UIImage imageNamed:@"down.png"]

   style:UIBarButtonItemStylePlain 

   target:self 

   action:@selector(decrement:)];

 [buttons addObject:item];

 [item release];



 item = [[UIBarButtonItem alloc] 

   initWithImage:[UIImage imageNamed:@"up.png"]

   style:UIBarButtonItemStylePlain target:self 

   action:@selector(increment:)];

 [buttons addObject:item];

 [item release];

 

 flexibleSpaceItem = [[[UIBarButtonItem alloc] 

        initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace

        target:nil action:NULL] autorelease]; 

 [buttons addObject:flexibleSpaceItem];

 [flexibleSpaceItem release];

 

 UIToolbar *toolbar = [[UIToolbar alloc] init]; 

 toolbar.barStyle = UIBarStyleBlackOpaque;

 [toolbar setItems:buttons animated:YES];

 [toolbar sizeToFit];

 

 self.navigationItem.titleView = toolbar;

 [toolbar release];

 

 // Add a left button

 self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]

            initWithTitle:@"Red" 

            style:UIBarButtonItemStylePlain 

            target:self 

            action:@selector(goRed)] autorelease];

 

 // Add a right button

 self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]

             initWithTitle:@"Blue" 

             style:UIBarButtonItemStylePlain 

             target:self 

             action:@selector(goBlue)] autorelease];





6 可拖动图片选项栏

#import <UIKit/UIKit.h>

#import "math.h"



@interface BrightnessController : UIViewController

{

 int brightness;

}

@end



@implementation BrightnessController



// MyCreateBitmapContext: Source based on Apple Sample Code

CGContextRef MyCreateBitmapContext (int pixelsWide,

         int pixelsHigh)

{

    CGContextRef    context = NULL;

    CGColorSpaceRef colorSpace;

    void *          bitmapData;

    int             bitmapByteCount;

    int             bitmapBytesPerRow;

 

    bitmapBytesPerRow   = (pixelsWide * 4);

    bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);

 

    colorSpace = CGColorSpaceCreateDeviceRGB();

    bitmapData = malloc( bitmapByteCount );

    if (bitmapData == NULL)

    {

        fprintf (stderr, "Memory not allocated!");

  CGColorSpaceRelease( colorSpace );

        return NULL;

    }

    context = CGBitmapContextCreate (bitmapData,

          pixelsWide,

          pixelsHigh,

          8,

          bitmapBytesPerRow,

          colorSpace,

          kCGImageAlphaPremultipliedLast);

    if (context== NULL)

    {

        free (bitmapData);

        fprintf (stderr, "Context not created!");

        return NULL;

    }

    CGColorSpaceRelease( colorSpace );

 

    return context;

}



// addRoundedRectToPath: Source based on Apple Sample Code

static void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth,

         float ovalHeight)

{

 float fw, fh;

 if (ovalWidth == 0 || ovalHeight == 0) {

  CGContextAddRect(context, rect);

  return;

 }

 

 CGContextSaveGState(context);

 CGContextTranslateCTM(context, CGRectGetMinX(rect), CGRectGetMinY(rect));

 CGContextScaleCTM(context, ovalWidth, ovalHeight);

 fw = CGRectGetWidth(rect) / ovalWidth;

 fh = CGRectGetHeight(rect) / ovalHeight;

 

 CGContextMoveToPoint(context, fw, fh/2);  // Start at lower right corner

 CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);  // Top right corner

 CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); // Top left corner

 CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); // Lower left corner

 CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // Back to lower right

 

 CGContextClosePath(context);

 CGContextRestoreGState(context);

}



// Build an image tinted at the given percentage

id createImage(float percentage)

{

 CGContextRef context  = MyCreateBitmapContext(30, 30);

 addRoundedRectToPath(context, CGRectMake(0.0f, 0.0f, 30.0f, 30.0f), 4.0f, 4.0f);

 CGFloat gray[4] = {percentage, percentage, percentage, 1.0f};

 CGContextSetFillColor(context, gray);

 CGContextFillPath(context);

 CGImageRef myRef = CGBitmapContextCreateImage (context);

 free(CGBitmapContextGetData(context));

 CGContextRelease(context);

 return [UIImage imageWithCGImage:myRef];

}





#define MAXDEPTH 8



-(BrightnessController *) initWithBrightness: (int) aBrightness

{

 self = [super init];

 brightness = aBrightness;

 self.title = [NSString stringWithFormat:@"%d%%", brightness * 10];

 [self.tabBarItem initWithTitle:self.title image:createImage(((float) brightness / 10.0f)) tag:0];

 return self;

}



- (void)loadView

{

 UIView *contentView = [[UIView alloc] init];

 float percent = brightness * 0.1;

 contentView.backgroundColor = [UIColor colorWithRed:percent green:percent blue:percent alpha:1.0];

 contentView.autoresizesSubviews = YES;

 contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);

 self.view = contentView;

    [contentView release];

}

@end



@interface SampleAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate>

@end



@implementation SampleAppDelegate



- (void)applicationDidFinishLaunching:(UIApplication *)application { 

 

 UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];



 // Create the array of UIViewControllers

 NSMutableArray *controllers = [[NSMutableArray alloc] init];



 for (int i = 0; i < 11; i++) {

  BrightnessController *bControl = [[BrightnessController alloc] initWithBrightness:i];

  UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:bControl];

  nav.navigationBar.barStyle = UIBarStyleBlackTranslucent;

  [controllers addObject:nav];

  [bControl release];

  [nav release];

 }

 

 // Create the toolbar and add the view controllers

 UITabBarController *tbarController = [[UITabBarController alloc] init];

 tbarController.viewControllers = controllers;

 tbarController.customizableViewControllers = controllers;

 tbarController.delegate = self;

 

 // Set up the window

 [window addSubview:tbarController.view];

 [window makeKeyAndVisible];

 

 [controllers release];

}

@end



int main(int argc, char *argv[])

{

 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

 int retVal = UIApplicationMain(argc, argv, nil, @"SampleAppDelegate");

 [pool release];

 return retVal;

}
 






 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics