暑期留校之iOS学习笔记之视图控制器(UI)

2015.8.18

<pre name="code" class="objc">//首先要导入头文件
#import "AppDelegate.h"
#import "RootViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //实例化一个导航控制器
    RootViewController *root =[[RootViewController alloc] init];
    
    //创建导航控制器
    UINavigationController *anvControl=[[UINavigationController alloc]initWithRootViewController:root];
    
    //添加到导航控制器
    self.window.rootViewController = anvControl;
    
    //设置控制器的背景颜色
    self.window.backgroundColor=[UIColor redColor];
    // Override point for customization after application launch.
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
#import "RootViewController.h"
#import "ViewController2.h"
@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
    
    //创建一个button
    //UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];蓝色感叹号
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

 //按钮标题为切换
    [btn setTitle:@"到2" forState:UIControlStateNormal];
    //设置按钮的位置
    [btn setFrame:CGRectMake(30, 80, 300, 50)];
    //为按钮添加事件 创建事件 方法
    [btn addTarget:self action:@selector(btnclicked:) forControlEvents:UIControlEventTouchUpInside];
    //设置背景颜色
    self.view.backgroundColor=[UIColor whiteColor];
    //设置按钮背景颜色
    btn.backgroundColor=[UIColor redColor];
    //添加的视图控制器中
    [self.view addSubview:btn];
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
//按钮所对应的方法
-(void)btnclicked:(UIButton*) btn

{
    //导航控制器对视图执行入栈操作(将视图控制器的实例加到导航控制器的栈中)
    ViewController2  * vc2=[[ViewController2 alloc] init];
//    取导航控制器的实例(avncontrol)
    //导航控制器对视图控制器执行入栈操作的方法,参数为视图控制器的实例 animated 表示动画
    [self.navigationController pushViewController:vc2 animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
#import "ViewController4.h"

@interface ViewController4 ()

@end

@implementation ViewController4

- (void)viewDidLoad {
    UIButton *pop=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [pop setTitle:@"到1" forState:UIControlStateNormal];
    [pop setFrame:CGRectMake(30, 80, 300, 50)];
    [pop addTarget:self action:@selector(btnpop:)forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:pop];
    pop.backgroundColor=[UIColor blackColor];
    self.view.backgroundColor=[UIColor yellowColor];
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
//跳转到任意视图控制器首先建立一个数组  视图控制器在数组中的顺序即为视图控制器在导航控制器栈中的顺序
//传递进去的视图控制器的实例必须是存在于视图控制器栈中的 事件方法
- (void)btnpop:(UIButton*)btnpop
{
    NSArray *array=self.navigationController.viewControllers;
[self.navigationController popToViewController:[array objectAtIndex:0] animated:YES
 ];
}
//跳转到根视图控制器点击事件触发 事件方法
- (void)popclicked:(UIButton*)pop
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end



UInnavigationbar的一些常用属性导航控制器自带的导航条常用的属性与方法

 

@property(nonatomic,assign)UIbarstyle barstyle;设置导航条的样式

 

@property(nonatomic)bool clipstobounds 设置导航条的自动裁减属性

 

-(void)setbackgroundimage:(UIimage*)backgroundsimage forbarmetrics

(UIbarmetrics)barmetrics //iOS5后 设置导航两条的背景图片和显示模式

 

导航控制器隐藏导航条的属性和方法

 

@property(nonatomic,getter=isnavigationbarhidden)bool navigationhidden 

隐藏导航天的属性

 

-(void)setnavifationbarhidden:(bool)hidden animated:(bool)animated;

隐藏导航条的方法

 

uinavigationitem

每个视图控制器都有一个navigationitem属性

每一个视图控制器都可以通过navigationitem属性来定制导航栏的显示

@property(nonatomic,copy)nsstring *title 设置标题 设置到标题栏的中间

@property(nonatomic,retain)uiview *titleview 设置标题视图 先是在导航栏的中间位置

@property(nonatomic,retain)uibarbuttonitem *leftbarbuttonitem 左侧按钮

@property(nonatomic,retain)uibarbuttonitem *lrightbarbuttonitem 右侧按钮

UIbuttonitem

uibarbuttonitem 的初始化方法

 

-(ID)initwithimage:(uiimage*)image style:(uibarbuttonitemstyle)styletarget:(id)target action:(sel)action;

 

-(id)initwithtitle:(nsstring*)title style:(uibarbuttonitemstyle)style target:(id)target action:(sel)action;

 

-(id)initwithbarbuttonsystemitem:(uibarbuttonsystemitem)system item target:(id)target action:(sel)action;

 

-(id)initwithcustomview:(uiview*)customview;

 

uitoolbar的使用

UInavigationcontrol 显示toolbar的方式 

@property(nonatomic, getter=istoolbarhidden) bool tool bar hidden 设置隐藏的显示属性

-(void)settoolhidden:(bool)hidden animated:(bool)animated 

是否动画效果

默认为yes

 

设置隐藏方法

 设置UItoolbar的背景图片

首先toolbar实在iOS5以后的方法position 是指toolbar处于屏幕的底部工具栏的现实

 

-(void)setbackgroundimage:(UIi image*)backgroundimage fortoolbarposition:(UItoolbarposition)toporbottom barmetrics:(UIbarmetrics) 

修改toolbar的大小 self.navigationcontrol.toolbar.frame=cgreactmake(0,020,34)

以上是到2015.8.22



posted @ 2015-08-23 10:29  Joe.Smith  阅读(164)  评论(0编辑  收藏  举报