注册 登录
主题 : UITabBarController+UINavigationController源代码:回复glueklig
级别: 侠客
UID: 6862
精华: 1
发帖: 17
可可豆: 970 CB
威望: 970 点
在线时间: 244(时)
注册时间: 2009-07-07
最后登录: 2011-04-16
0 楼:  发表于: 2009-07-10 14:03    发自: Web Page

UITabBarController+UINavigationController源代码:回复glueklig   

       以前都是看别人发帖,不知道回复时为什么上传不了附件,后来发现是太大了,干脆单独发个贴好了。
      附件是偶自己写的代码,编着玩儿的,没有实际意义,练习TabBarController和NavController加一起而已。大体思路,先创建TabBarController,然后把NavController、viewController添加的TabBarController里头去,并且为每个viewController创建单独的.xib文件,设置好图像显示。仅供参考,我也是初学者,不正之处望大虾们指正。
     新建project名称tabBar。TabBarController有三个选项,第一个IntroductionFirstViewController是navigationController,其它的不是,所以我就不给出代码了,大家可以自己编写后再引用。该代码里面用到图片就不提供了,你可以添加自己的图片,改变图片文件名。偶的代码最后的效果是这样的:










//tabBarAppDelegate.h
复制代码
  1. #import <UIKit/UIKit.h>
  2. #import "IntroductionFirstViewController.h"
  3. #import "DisplayController.h"
  4. #import "OperationFirstViewController.h"
  5. @interface tabBarAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
  6.     UIWindow *window;
  7.     UITabBarController *tabBarController;
  8. }
  9. @property (nonatomic, retain) IBOutlet UIWindow *window;
  10. @end


//tabBarAppDelegate.m
复制代码
  1. #import "tabBarAppDelegate.h"
  2. @implementation tabBarAppDelegate
  3. @synthesize window;
  4. - (void)applicationDidFinishLaunching:(UIApplication *)application {
  5.    
  6.     // Add the tab bar controller's current view as a subview of the window
  7.     [application setStatusBarHidden:YES];   
  8.     //tabBarController
  9.     tabBarController = [[UITabBarController alloc] init];
  10.    
  11.     // Create a few view controllers and navigation controllers
  12.     IntroductionFirstViewController *introductionFirst =[[IntroductionFirstViewController alloc]initWithNibName:@"IntroductionFistView" bundle:nil];// first:navigationcontroller
  13.     UINavigationController *navIntroductionController=[[UINavigationController alloc] init];
  14.     [navIntroductionController pushViewController:introductionFirst animated:NO];
  15.     DisplayController *display=[[DisplayController alloc] initWithNibName:@"DisplayView" bundle:nil];//second:view controller
  16.     OperationFirstViewController*operationFirst=[[OperationFirstViewController alloc]initWithNibName:@"OperationFirstView" bundle:nil];//third:anothernavigation controller
  17.     UINavigationController *navOperationController=[[UINavigationController alloc] init];
  18.     [navOperationController pushViewController:operationFirst animated:NO];   
  19.     // Add them as children of the tab bar controller
  20.     NSArray *tabcontrollers = [NSArray arrayWithObjects:navIntroductionController, display, navOperationController, nil];
  21.     [tabBarController setViewControllers:tabcontrollers animated:YES];
  22.     // Don't forget memory management
  23.     [introductionFirst release];
  24.     [display release];
  25.     [operationFirst release];
  26.     [navIntroductionController release];
  27.     [navOperationController release];
  28.        
  29.     // Add the tab bar controller's view to the window
  30.     [window addSubview:tabBarController.view];
  31.     [window makeKeyAndVisible];
  32. }
  33. - (void)dealloc {
  34.     [tabBarController release];
  35.     [window release];
  36.     [super dealloc];
  37. }
  38. @end


//IntroductionFirstViewController.h
复制代码
  1. #import <UIKit/UIKit.h>
  2. #import "IntroductionSecondViewController.h"
  3. @interface IntroductionFirstViewController : UIViewController {
  4. }
  5. -(IBAction)buttonPressed:(id)sender;
  6. @end


//IntroductionFirstViewController.m

复制代码
  1. @implementation IntroductionFirstViewController;
  2. -(IBAction)buttonPressed:(id)sender
  3. {
  4.     IntroductionSecondViewController*secondIntroduction=[[IntroductionSecondViewController alloc]initWithNibName:@"IntroductionSecondView" bundle:nil];
  5.     [self.navigationController pushViewController:secondIntroduction animated:YES];
  6.     [secondIntroduction release];
  7. }
  8. // The designated initializer. Override to perform setup that is required before the view is loaded.
  9. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  10.     if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
  11.         // Custom initialization
  12.         self.navigationItem.title=@"Introduction";
  13.         self.tabBarItem.title=@"节目";
  14.         self.tabBarItem.image=[UIImage imageNamed:@"introduction.png"];
  15.     }
  16.     return self;
  17. }
  18. - (void)didReceiveMemoryWarning {
  19.     [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
  20.     // Release anything that's not essential, such as cached data
  21. }
  22. - (void)dealloc {
  23.     [super dealloc];
  24. }
  25. @end


//IntroductionSecondViewController.h
复制代码
  1. #import <UIKit/UIKit.h>
  2. #import "IntroductionThirdViewController.h"
  3. @interface IntroductionSecondViewController : UIViewController {
  4.     IBOutlet UIImageView *ingredients;
  5. }
  6. @property(retain) UIImageView *ingredients;
  7. @end


//IntroductionSecondViewController.m
复制代码
  1. #import "IntroductionSecondViewController.h"
  2. @implementation IntroductionSecondViewController
  3. @synthesize ingredients;
  4. // The designated initializer. Override to perform setup that is required before the view is loaded.
  5. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  6.     if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
  7.         // Custom initialization
  8.         self.navigationItem.title=@"Ingredients";
  9.             }
  10.     return self;
  11. }
  12. -(void)next
  13. {
  14.     IntroductionThirdViewController*thirdIntroduction=[[IntroductionThirdViewController alloc]initWithNibName:@"IntroductionThirdView" bundle:nil];
  15.     [self.navigationController pushViewController:thirdIntroduction animated:YES];   
  16. }
  17. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  18. - (void)viewDidLoad {
  19.     [super viewDidLoad];
  20.     UIBarButtonItem *nextButton = [[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStyleBordered target:selfaction:@selector(next)];
  21.     self.navigationItem.rightBarButtonItem = nextButton;
  22.     [nextButton release];
  23.     UIImageView *animation=[[UIImageView alloc] initWithFrame:ingredients.frame];
  24.     animation.animationImages=[NSArray arrayWithObjects:[UIImageimageNamed:@"noodle.png"],[UIImage imageNamed:@"meat.png"],[UIImageimageNamed:@"ingredients.jpg"],[UIImageimageNamed:@"water.png"],nil];  
  25.    //ingredients.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleTopMargin;
  26.     animation.animationDuration=1.75;
  27.     animation.animationRepeatCount=0;//注意0是指重复无穷次
  28.     [animation startAnimating];
  29.     [ingredients addSubview:animation];
  30.     [animation release];
  31. }
  32. - (void)dealloc {
  33.     [ingredients release];
  34.     [super dealloc];
  35. }
  36. @end


//IntroductionThirdViewController.h
复制代码
  1. #import <UIKit/UIKit.h>
  2. @interface IntroductionThirdViewController : UIViewController {
  3.     IBOutlet UIImageView *lefteye;
  4.     IBOutlet UIImageView *righteye;
  5.     IBOutlet UIImageView *heart;
  6.     IBOutlet UIImageView *noodle;
  7. }
  8. @property(retain) UIImageView *lefteye;
  9. @property(retain) UIImageView *righteye;
  10. @property(retain) UIImageView *heart;
  11. @property(retain) UIImageView *noodle;
  12. @end


//IntroductionThirdViewController.m

复制代码
  1. #import "IntroductionThirdViewController.h"
  2. @implementation IntroductionThirdViewController
  3. @synthesize heart;
  4. @synthesize lefteye;
  5. @synthesize righteye;
  6. // The designated initializer. Override to perform setup that is required before the view is loaded.
  7. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  8.     if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
  9.         // Custom initialization
  10.         self.navigationItem.title=@"Display";
  11.     }
  12.     return self;
  13. }
  14. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  15. - (void)viewDidLoad {
  16.     [super viewDidLoad];
  17.     CGRect frame=heart.frame;
  18.     [UIImageView beginAnimations:@"heartjumping" context:nil];
  19.     [UIImageView setAnimationRepeatAutoreverses:YES];
  20.     [UIImageView setAnimationRepeatCount:999];
  21.     frame.origin.y+=10;
  22.     heart.frame=frame;
  23.     [UIImageView setAnimationDuration:1];
  24.     frame.origin.y-=10;
  25.     heart.frame=frame;
  26.     [UIImageView setAnimationDuration:1];
  27.     [UIImageView commitAnimations];
  28.     UIImageView *left=[[UIImageView alloc] initWithFrame:lefteye.frame];   
  29.     UIImageView *right=[[UIImageView alloc] initWithFrame:righteye.frame];
  30.     left.animationImages=[NSArray arrayWithObjects:[UIImageimageNamed:@"left.png"],[UIImage imageNamed:@"leftdown.png"],[UIImageimageNamed:@"down.png"],
  31.     [UIImageimageNamed:@"rightdown.png"],[UIImage imageNamed:@"down.png"],[UIImageimageNamed:@"leftdown.png"],[UIImage imageNamed:@"left.png"],nil];
  32.     right.animationImages=[NSArray arrayWithObjects:[UIImageimageNamed:@"left.png"],[UIImage imageNamed:@"leftdown.png"],[UIImageimageNamed:@"down.png"],
  33.     [UIImageimageNamed:@"rightdown.png"],[UIImage imageNamed:@"down.png"],[UIImageimageNamed:@"leftdown.png"],[UIImage imageNamed:@"left.png"],nil];
  34.     left.animationDuration=2;
  35.     right.animationDuration=2;   
  36.     left.animationRepeatCount=0;//注意0是指重复无穷次
  37.     right.animationRepeatCount=0;//注意0是指重复无穷次
  38.     [left startAnimating];
  39.     [right startAnimating];   
  40.     [lefteye addSubview:left];     
  41.     [righteye addSubview:right];
  42.     [left release];
  43.     [right release];
  44.     CGRect frame2=noodle.frame;
  45.     [UIImageView beginAnimations:@"noodle" context:nil];
  46.     [UIImageView setAnimationCurve:UIViewAnimationCurveEaseIn];
  47.     [UIImageView setAnimationRepeatCount:999];
  48.     [UIImageView setAnimationBeginsFromCurrentState:YES];
  49.     [UIImageView setAnimationRepeatAutoreverses:YES];
  50.     [UIImageView setAnimationDuration:1];
  51.     frame2.origin.x+=200;
  52.     noodle.frame=frame2;
  53.     [UIImageView commitAnimations];
  54. }
  55. - (void)dealloc {
  56.     [heart release];
  57.     [lefteye release];
  58.     [righteye release];
  59.     [super dealloc];
  60. }
  61. @end
[ 此帖被lolfriend在2009-07-10 14:44重新编辑 ]

每天学些没接触过的东西让我对生活充满激情!
级别: 精灵王

UID: 2236
精华: 5
发帖: 214
可可豆: 34631 CB
威望: 34633 点
在线时间: 2353(时)
注册时间: 2009-02-17
最后登录: 2016-08-17
1 楼:  发表于: 2009-07-10 14:33    发自: Web Page
LZ的图片显示不出来了,不过代码对我来说太有用了,谢谢
级别: 圣骑士
UID: 3366
精华: 0
发帖: 57
可可豆: 6786 CB
威望: 6786 点
在线时间: 2156(时)
注册时间: 2009-03-20
最后登录: 2017-05-09
2 楼:  发表于: 2009-07-10 15:20    发自: Web Page
谢谢, 楼主这是我要的
级别: 圣骑士
状态: 连续签到 - [171天]
UID: 7132
精华: 0
发帖: 100
可可豆: 8521 CB
威望: 8150 点
在线时间: 942(时)
注册时间: 2009-07-19
最后登录: 2017-05-30
3 楼:  发表于: 2009-07-27 15:52    发自: Web Page
请问可不可以在IB里面的一个子视图上使用导航,如何使用!
有人说人生最大的幸福是:睡觉睡到自然醒,数钱数到手抽筋。其实我认为最大的幸福是:无论几点睡觉最后一眼看到的是心爱的人,无论几点醒来第一眼看到的是心爱的人。无论赚多少钱,都是和心爱的人一起赚,如论花多少钱,都是和心爱的人一起花。
级别: 圣骑士

UID: 2228
精华: 4
发帖: 73
可可豆: 7020 CB
威望: 7020 点
在线时间: 501(时)
注册时间: 2009-02-16
最后登录: 2014-10-30
4 楼:  发表于: 2009-07-27 17:28    发自: Web Page
不用IB,用code实现的代码例子在这~嘿嘿
http://www.cocoachina.com/bbs/read.php?tid-5869.html
级别: 圣骑士
状态: 连续签到 - [171天]
UID: 7132
精华: 0
发帖: 100
可可豆: 8521 CB
威望: 8150 点
在线时间: 942(时)
注册时间: 2009-07-19
最后登录: 2017-05-30
5 楼:  发表于: 2009-07-27 17:53    发自: Web Page
收到,那我想请问一下可不可以用ib实现呢?或者说在一个不是主视图的子视图里可不可以再重新定义导航呢?
有人说人生最大的幸福是:睡觉睡到自然醒,数钱数到手抽筋。其实我认为最大的幸福是:无论几点睡觉最后一眼看到的是心爱的人,无论几点醒来第一眼看到的是心爱的人。无论赚多少钱,都是和心爱的人一起赚,如论花多少钱,都是和心爱的人一起花。
级别: 精灵王

UID: 4379
精华: 0
发帖: 323
可可豆: 12279 CB
威望: 12270 点
在线时间: 2131(时)
注册时间: 2009-04-09
最后登录: 2016-12-05
6 楼:  发表于: 2009-08-02 15:25    发自: Web Page
用IB总是不成功,干脆用代码好了
级别: 新手上路
UID: 1068
精华: 0
发帖: 18
可可豆: 171 CB
威望: 171 点
在线时间: 8(时)
注册时间: 2008-10-17
最后登录: 2012-06-11
7 楼:  发表于: 2009-08-21 16:54    发自: Web Page
谢谢楼主分享
级别: 骑士
UID: 18380
精华: 0
发帖: 171
可可豆: 1701 CB
威望: 1701 点
在线时间: 277(时)
注册时间: 2010-04-25
最后登录: 2013-04-26
8 楼:  发表于: 2010-07-17 14:24    发自: Web Page
MARK
学习~~

级别: 版主

状态: 连续签到 - [144天]
UID: 491
精华: 6
发帖: 1766
可可豆: 112205 CB
威望: 112852 点
在线时间: 8537(时)
注册时间: 2008-08-19
最后登录: 2019-02-28
9 楼:  发表于: 2010-07-17 19:14    发自: Web Page
lz。。。。悄悄的来,悄悄的走阿,都不给和尚我打个招呼。。。
 Developer
------------------------------------------------------------
Η αγάπη ποτέ δεν αποτυγχάνει.
愛是永不止息。
Love never fails.

    --《圣经.新约》哥林多前书第13章

CocoaChina社区转载内容已尽可能注明出处,如未能核实来源或转发内容图片有权利瑕疵的,请及时联系社区进行修改或删除【联系方式QQ : 3442093904 邮箱:support@cocoachina.com】文章内容为作者独立观点,不代表CocoaChina社区立场。版权归原作者所有,如申请授权请联系作者,因文章侵权CocoaChina社区不承担任何法律及连带责任。

描述
快速回复

关注本帖(如果有新回复会站内信通知您)

发帖、回帖都会得到可观的积分奖励。查看论坛积分规则

按"Ctrl+Enter"直接提交
    顶部