源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

iOS App开发中导航栏的创建及基本属性设置教程

  • 时间:2022-07-14 16:04 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:iOS App开发中导航栏的创建及基本属性设置教程
文件目录如下:基本导航顺序: root -> First -> Second -> Third。其中,FirstViewController作为 navigation堆栈的rootview [img]http://files.jb51.net/file_images/article/201602/201622690834116.jpg?20161269846[/img] [b]1、创建navigation[/b] 如果是想直接把navigation导航作为项目一开始的跟视图,把RootViewController.h文件里的nav属性放到AppDelegate.h里即可,再把RootViewController.m文件里的action的代码复制到 AppDelegate.m里的didFinishLaunchingWithOptions 方法里,最后把 self.window.rootViewController 设置 UINavigationController类型的属性nav即可 在RootViewController.h文件
[u]复制代码[/u] 代码如下:
#import <UIKit/UIKit.h> @class FirstViewController; @interface RootViewController : UIViewController @property (strong, nonatomic) UINavigationController *nav; - (IBAction)btnClick:(UIButton *)sender; @end
在RootViewController.m 文件里的随意一个自定义action里:
[u]复制代码[/u] 代码如下:
- (IBAction)btnClick:(UIButton *)sender {         //创建一个viewcontroller     FirstViewController *fristview =[[[FirstViewController alloc] init] autorelease];            //初始化UINavigationController(方式一)     self.nav = [[[UINavigationController alloc] initWithRootViewController:fristview] autorelease];             //初始化UINavigationController(方式二)   //  self.nav = [[[UINavigationController alloc] init] autorelease];   //  [self.nav pushViewController:fristview animated:YES];         //初始化UINavigationController(方式三,失败,xib文件加载失败,原因暂时不明)    // self.nav = [[[UINavigationController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];             //跳转到FirstView页面     [self presentViewController:self.nav animated:YES completion:nil];             //这种写法一般用于往view里添加一些小控件,如button  label textField之类的,不适宜用于页面跳转     // [self.view addSubview:self.nav.view];         }
[b]2.navigation的常用属性设置例子 [/b]我们的navigation就加载上去了以后,下面我们来设置navigation的属性:
[u]复制代码[/u] 代码如下:
- (void)viewDidLoad  {      [super viewDidLoad];      // Do any additional setup after loading the view.      [self.navigationController.navigationBar setTranslucent:NO];//设置navigationbar的半透明      self.title = @"navigationcontroller";//设置navigationbar上显示的标题      [self.navigationController.navigationBar setBarTintColor:[UIColor purpleColor]];//设置navigationbar的颜色      self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItemStyleDone target:self action:Nil];//设置navigationbar左边按钮      self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:Nil];//设置navigationbar右边按钮      [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];//设置navigationbar上左右按钮字体颜色  } 
效果图如下: [img]http://files.jb51.net/file_images/article/201602/201622690858466.png?2016126999[/img] 这里还有一个属性常用,就是:
[u]复制代码[/u] 代码如下:
NSArray *arr = [NSArray arrayWithObjects:@"1",@"2", nil nil];      UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:arr];      self.navigationItem.titleView = segment;//设置navigation上的titleview 
效果如下: [img]http://files.jb51.net/file_images/article/201602/201622690937588.png?20161269945[/img] 对,我们看到中间的字变成了两个可选的按钮,这就是navigation的另一个属性:navigationitem.titleview。 下面我们再建立一个视图,看一下两个视图之前是怎样通信的。 在第二个视图中,我添加了一个button来显示,并加了一个成员变量来接收从第一个视图中穿过来的值:  
[u]复制代码[/u] 代码如下:
@interface SecondViewController : UIViewController  @property (copy,nonatomic) NSString *str;  @end   
[u]复制代码[/u] 代码如下:
- (void)viewDidLoad  {      [super viewDidLoad];      // Do any additional setup after loading the view.      self.title = @"second";      UIButton *aBUTTON = [[UIButton alloc]initWithFrame:CGRectMake(30, 30, 50, 30)];      [aBUTTON setTitle:_str forState:UIControlStateNormal];      [aBUTTON addTarget:self action:@selector(clicked) forControlEvents:UIControlEventTouchUpInside];      [self.view addSubview:aBUTTON];  } 
然后我将第一个视图的右边按钮添加一个事件,点击按钮,就会推出第二个视图,并显示我们传过来的值:  
[u]复制代码[/u] 代码如下:
- (void)clicked{      SecondViewController *second = [[SecondViewController alloc]init];      [self.navigationController pushViewController:second animated:YES];      second.str = @"hello!!";      [second release];  } 
下面,我们来运行一下: [img]http://files.jb51.net/file_images/article/201602/201622691003542.png?201612691010[/img] 点进按钮以后,我们的第二个视图推出,button显示了传过来的值。 然后我们点击回button,还有navigation另外一个方法:
[u]复制代码[/u] 代码如下:
- (void)clicked{      [self.navigationController popViewControllerAnimated:YES];  } 
这样就可以回到第一个视图。  
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部