IOS5中:
-(void)setBackgroundImage:(UIImage)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics
IOS6中:
在上面的方法基础上,加上[UIApplication shareApplication].statusBarStyle = UIStatusBarStyleBlackOpaque;
基础控件UINavigationController中的传值,代理传值,正向传值,反向传值
#import <UIKit/UIKit.h> //声明一个协议 @protocol SendValue<NSObject> //定义一个方法 - (void)sendBtnTitle:(NSString *)title; @end @interface FirstViewController : UIViewController // 定义代理 @property (nonatomic, assign) id <SendValue>delegate; // 创建一个正向传值的属性 @property (nonatomic,copy) NSString *currentTitle; @end//正向传值利用的是属性传值
//反向传值利用代理传值
创建一个RootViewController类和FirstViewController
分别在AppDelegate.m中调用RootViewController.h 在RootViewController.m中调用 FirstViewController.h
在RootViewController.m
{
self.view.backgroundColor = [UIColor redColor];
self.navigationItem.title = @"Root";
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:@"push to VC2" forState:UIControlStateNormal];
[btn setFrame:CGRectMake(30, 100, 300, 50)];
[btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
FirstViewController.h
//协议代理实现反向传值
//声明一个协议
@protocol SendValue <NSObject>
//定义一个方法
-(void)SendTitle:(NSString *)title;
@end
@interface 下面写上
//定义代理
@property (nonatomic,assign) id <SendValue> delegate;
//创建一个正向传值的属性
@property (nonatomic,copy) NSString *currentTitle;
FirstViewController.m中
@implementation 下面写上
@synthesize delegate = _delegate;
@synthesize currentTitle = _currentTitle;
- (void)viewDidLoad
{//UINavigationController中的反向传值问题
self.view.backgroundColor = [UIColor yellowColor];
//把标题放在一个数组中 利用for循环设置
NSArray * btnTitles = [NSArray arrayWithObjects:@"标题1",@"标题2",@"标题3", nil];
for (int i=0; i<[btnTitles count]; i++) {
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//如果按钮的标题和属性中的_currentTitle 相同,即和根页面中的导航条的title一样
if ([_currentTitle isEqualToString:btn.currentTitle]) {
//开启选中状态
btn.selected = YES;
}
[btn setTitle:[btnTitles objectAtIndex:i] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(titleBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn setFrame:CGRectMake(10, 80+i*40, 300, 30)];
[self.view addSubview:btn];
}
//对titleBtnClicked:设置
-(void)titleBtnClicked:(UIButton *)btn
{
NSString * title = btn.currentTitle;
//判断代理中是否有SendTitle:这个函数
if([_delegate respondsToSelector:@selector(SendTitle:)]){
//代理执行自己的SendTitle 函数,传参是title
[_delegate SendTitle:title];
}
[self.navigationController popViewControllerAnimated:YES];
}
在RootViewController.h中
#import <UIKit/UIKit.h>
添加头文件 #import "FirstViewController.h"
//把协议挂过来
@interface RootViewController : UIViewController <SendValue>
@end
- (void)btnClicked:(UIButton *)btn
{
FirstViewController * first = [[FirstViewController alloc] init];
//将当前页面的navigaItem.title传递过去
//正向传值
first.currentTitle = self.navigationItem.title;
//将代理指定为当前rootViewController类的指针
first.delegate = self;
[self.navigationController pushViewController:first animated:YES];
}
//实现协议定义的方法
-(void)SendTitle:(NSString *)title
{
self.navigationItem.title = title;
}