1天学习1个类 UIBarButtomItem 类 示例

先看演示后面直接发代码部分.

没有全部测试完API,部分测试代码注释.大家可以自己测试.

1天学习1个类 UIBarButtomItem 类 示例_第1张图片


示例全部采用代码实现..

新建工程->empty application

删除Applegate.h和Applegate.m 修改main.m文件.粘贴下面代码



//
//  main.m
//  ControlDemo
//
//  Created by watsy0007 on 12-6-3.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController 

@property (nonatomic) NSInteger nDepth;

@end

@implementation ViewController

@synthesize nDepth = _nDepth;

- (id) init {
    if (self = [super init]) {
        _nDepth = 0;
    }
    return self;
}

- (void) BarButtonItemAction:(id) sender {

    if (_nDepth == 0) {
        ViewController *vc = [[ViewController alloc] init];
        vc.nDepth = self.nDepth + 1;
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
    }
}

- (UIBarButtonItem *) createNewItemWith {
    UIBarButtonItem *item;
    
//    item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(BarButtonItemAction:)];
    item = [[UIBarButtonItem alloc] initWithTitle:@"watsy0007" style:UIBarButtonItemStylePlain target:self action:@selector(BarButtonItemAction:)];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
    view.backgroundColor = [UIColor blueColor];
    
//    item = [[UIBarButtonItem alloc] initWithCustomView:view];
//    item.customView = view;
    item.target = self;
    item.action = @selector(BarButtonItemAction:);
    item.style = UIBarButtonItemStylePlain;
    item.tintColor = [UIColor colorWithRed:155.0 / 255.0 
                                     green:155.0 / 255.0 
                                      blue:155.0 / 255.0 
                                     alpha:0.7];
    
    [item setTitle:@"push"];

    //设置标题显示的位置
    [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setBackButtonTitlePositionAdjustment:UIOffsetMake(20, 5) forBarMetrics:UIBarMetricsDefault];
    
    [view release];
    
    return [item autorelease];
}

- (void) loadView {
    [super loadView];
    UILabel *label = [[UILabel alloc] initWithFrame:self.view.bounds];
    label.numberOfLines = 3;
    label.textAlignment = UITextAlignmentCenter;
    label.text = @"watsy0007 \n QQ:258841679\n群:125807534";
    [self.view addSubview:label];
    [label release];
    
    self.title = @"UIBarButtomItem Class Reference Demo";

    if (self.nDepth == 0) {
        self.navigationItem.rightBarButtonItem = [self createNewItemWith];
    } 
}


- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return YES;
}

- (void) viewDidLoad {
    [super viewDidLoad];
    
    
}

@end


@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIViewController *viewController;

@end

@implementation AppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;

- (void) dealloc {
    [_window release];
    [_viewController release];
    
    [super dealloc];
}

- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    
    self.viewController = [[ViewController alloc] init];
    
    UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:self.viewController]; 
    self.window.rootViewController = controller;
    [controller release];
    
    [self.window makeKeyAndVisible];
    return YES;
}

@end

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}


你可能感兴趣的:(1天学习1个类 UIBarButtomItem 类 示例)