ITeye博客

新风作浪博客学习(十六)Navigation + Tab Bar 常用组合框架 .

android_zhang · 2013-06-07 08:50 · ios · 移动开发 · iOS · Markdown

看到很多项目中都采用的是Navigation加Tab Bar组合到一起,完成视图切换操作,在导航栏上添加基本按钮,给予响应事件,让应用给用户更好的体验,所以本菜鸟写了这个这样一个Demo,仅供学习

所创建工程模板是最后一个 Empty Application

先看运行效果: 第一个视图,点击按钮切换视图,点击导航栏上按钮可以切换回去

第二个视图设置了背景颜色和透明度 第三个视图添加了背景图片

第四个视图,在导航栏上添加了两个按钮,左边按钮属于自定义标题,右边按钮是系统的图标,点击左按钮弹出一个警告,右边按钮没有添加响应事件,点击后没反应

Tab Bar上添加的都是自定义图片

框架组合的主要代码,在AppDelegate.m中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[uiwindow alloc] initWithFrame:[[uiscreen mainscreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [uicolor whitecolor];
    
    _tabBarController = [[uitabbarcontroller alloc] init];
    _tabBarController.delegate = self;
    

    FirstViewController *firstViewController = [[firstviewcontroller alloc] initWithNibName:@"FirstViewController" bundle:nil];
    
    SecondViewController *secondViewController = [[secondviewcontroller alloc] initWithNibName:@"secondViewController" bundle:nil];
    
    ThirdViewController *thirdViewController = [[thirdviewcontroller alloc] initWithNibName:@"ThirdViewController" bundle:nil];
    
    FourViewController *fourViewController = [[fourviewcontroller alloc] initWithNibName:@"FourViewController" bundle:nil];
    
    UINavigationController *navFirst = [[uinavigationcontroller alloc] initWithRootViewController:firstViewController];
//    在加载图片是把标题都覆盖了,所以运行效果并没有显示这些文字
    navFirst.title = @"第一个视图";
    
    UINavigationController *navSecond = [[uinavigationcontroller alloc] initWithRootViewController:secondViewController];
    navSecond.title = @"第二个视图";
    
    UINavigationController *navThird = [[uinavigationcontroller alloc] initWithRootViewController:thirdViewController];
    navThird.title = @"第三个视图";
    
    UINavigationController *navFour = [[uinavigationcontroller alloc] initWithRootViewController:fourViewController];
    navFour.title = @"第四个视图";
    
    
    _tabBarController.viewControllers = [nsarray arraywithobjects:navfirst,navsecond,navthird,navfour, nil];
    _window.rootViewController = _tabBarController;
    
    [self.window addsubview:firstviewcontroller.view];
    [self.window makekeyandvisible];
    return YES;
}

第一个视图切换按钮响应事件

- (IBAction)switchView:(id)sender {
    
    FirstViewController *firstController = [[firstviewcontroller alloc] init];
    [self.navigationcontroller pushviewcontroller:firstcontroller animated:yes];
    firstController.title = @"第一个视图另一个视图";
}

第四个视图添加两个按钮方法,在最后一个控制机的.m文件中的-(void)viewDidLoad方法中

- (void)viewDidLoad
{
    [super viewdidload];
	// Do any additional setup after loading the view.
    UIBarButtonItem *leftButton = [[uibarbuttonitem alloc] initWithTitle:@"测试"
                                                                   style:UIBarButtonItemStylePlain target:self
                                                                  action:@selector(pullWarn)];
    self.navigationItem.leftBarButtonItem = leftButton;
    
    UIBarButtonItem *rightButton = [[uibarbuttonitem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];
    self.navigationItem.rightBarButtonItem = rightButton;
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。