IOS导航栏UINavigation

在Iphone开发中,UINavigation算是最常用的控件,因为大多数的应用都会用到导航,UINavigation的用法也是非常的简单

创建一个新的Single View模板项目

IOS导航栏UINavigation_第1张图片

AppDelegate.h

@property (strong, nonatomic) UINavigationController *navController;

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    MainViewController *mainView = [[[MainViewController alloc] initWithNibName:@"MainView" bundle:nil] autorelease];
    navController = [[[UINavigationController alloc] initWithRootViewController:mainView] autorelease];
    self.window.rootViewController = navController;
    [self.window makeKeyAndVisible];
    return YES;
}

MainViewController是要显示的第一个窗口

MainViewController.m

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"myNav";
    }
    return self;
}

// 跳转到第二个窗口
- (IBAction)doJumpNextView:(id)sender
{
    OneViewController *oneView = [[[OneViewController alloc] initWithNibName:@"OneView" bundle:nil] autorelease];
    oneView.title = [[sender titleLabel] text];
    [self.navigationController pushViewController:oneView animated:YES];
}

- (void)viewDidLoad
{
    // 设置一个名字为style的按钮
    UIBarButtonItem *btnStyle = [[UIBarButtonItem alloc] initWithTitle:@"style" style:UIBarButtonItemStyleDone target:self action:@selector(onClick:)];
    // 添加该按钮到导航的左边
    self.navigationItem.leftBarButtonItem = btnStyle;
    [super viewDidLoad];
}

// 弹出UIActionSheet选择样式
- (void)onClick:(id)sender
{
    UIActionSheet *styleSheet = [[UIActionSheet alloc] initWithTitle:@"选择样式" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"样式一",@"样式二",@"样式三", nil];
    [styleSheet showInView:self.view];
    [styleSheet release];
}

// 变更UINavigationController的样式
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
            self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
            break;
        case 1:
            self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
            break;
        default:
            self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
            break;
    }
}
IOS导航栏UINavigation_第2张图片 IOS导航栏UINavigation_第3张图片 IOS导航栏UINavigation_第4张图片


DEMO下载链接

http://pan.baidu.com/share/link?shareid=72483&uk=101519637



你可能感兴趣的:(application,iPhone,action,stylesheet)