您的位置:首页 > 产品设计 > UI/UE

UINavigationController及界面传值

2015-08-29 15:29 477 查看
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[_window release];

// 先创建一个viewController
MainViewController *mainVC = [[MainViewController alloc] init];
// 创建导航控制器,它可以认为是管理控制器的控制器,主要管理有层级关系的控制器
// 它继承于UIViewController 通过栈的方式管理所控制的视图控制器(控制器的切换:控制入栈和出栈来展示各个视图控制器),至少要有一个被管理的视图控制器,这个控制器我们称作导航控制器的根视图控制器
// 任何继承自UIViewController的类(多态)都可以作为根控制器
UINavigationController *naviC = [[UINavigationController alloc] initWithRootViewController:mainVC]; // 放入根视图控制器
//  UINavigationController其ContentView里始终显示栈顶控制器的view
//  它的viewControllers属性存储了栈中所有被管理的控制器
// navigationController属性,每个在栈中的控制器,都能通过此属性,获取自己所在的UINavigationController对象
// 入栈和出栈
//    pushViewController:animated //进⼊下一个视图控制器
//    popViewControllerAnimated: //返回上一个视图控制器
//    popToViewController:animated //返回到指定的视图控制器
//    popToRootViewControllerAnimated //返回到根视图控制器
// 常用属性
//    viewControllers //所有处于栈中的控制器
//    topViewController //位于栈顶的控制器
//    visibleViewController //当前正在显示的控制器
//    navigationBar //导航条
// UINavigationBar
//    navigationBar—导航条,iOS7之后默认是透明的,iOS7之前默认是不透明的。
//    navigationBar在透明情况,与contentView会重合一部分区域。
//    navigationBar在不透明情况,contentView跟在navigationBar的下面。
//    navigationBar竖屏下默认⾼度44,横屏下默认高度32.
// UINavigationBar属性
//    barTintColor // 设置导航条颜色
//    setBackgroundImage:forBarMetrics: // 导航条加背景图片
//    barStyle shadowImage titleTextAttributes tintColor translucent
// UINavigationBar除了能定义自身的样式外,它同样以栈的方式管理一组UINavigationItem,提供push和pop操作item
//    每个视图控制器都有一个navigationItem属性。navigationItem中设置的左按钮、右按钮、标题等,会随着控制器的显示,也显示到navigationBar上
//    UINavigationItem属于MVC中的M。封装了要显示在UINavigationBar上的数据
//    title titleView //标题视图 leftBarButtonItem rightBarButtonItem
// UIBarButtonItem
//    UIBarButtonItem属于MVC的M。定义了UINavigationItem上按钮的触发事件,外观等
//    -initWithBarButtonSystemItem:target:action:
//    -initWithTitle:style:target:action:
//    -initWithImage:style:target:action:
//    tintColor
self.window.rootViewController = naviC;
[naviC release];
[mainVC release];

return YES;
}




- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor magentaColor];
// 导航视图控制器高度是44,上面的状态栏高20,加在一起默认是64
// 对navigation 进行设置
// 标题设置
self.title = @"江诗丹顿";
// 内容方面的设置
self.navigationItem.title = @"百达翡丽";

// 外观进行设置
// 背景颜色的设置
// 不是所有的背景颜色都是backgroundColor
//    self.navigationController.navigationBar.barTintColor = [UIColor cyanColor];

// 创建一个视图
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
//    view.backgroundColor = [UIColor blackColor];
//    [self.view addSubview:view];
//    [view release];
// 为了防止坐标系被篡改,我们把bar从半透明设置成不透明,这样坐标系的原点会自动向下推64个格
self.navigationController.navigationBar.translucent = NO;

UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"信息",@"通话",@"邮箱"]];
seg.frame = CGRectMake(0, 0, 0, 30); // 这个宽度是怎么回事.设置多小都没事
// 宽度应该是有Items个数自适应的,高度可以自己设置
self.navigationItem.titleView = seg;

// 创建左右两边的按钮
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(leftButtonAction:)];

//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-tianchengzuo.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction:)];
// 如果想让按钮控件的图片保持原来的颜色 就需要像下面这么做
UIButton *buttonRight = [UIButton buttonWithType:UIButtonTypeCustom];
buttonRight.frame = CGRectMake(0, 0, 40, 40);
//    buttonRight.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"UIBarButtonItemStylePlain"]];
[buttonRight setImage:[UIImage imageNamed:@"iconfont-tianchengzuo.png"] forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonRight];

self.myTextField = [[UITextField alloc] initWithFrame:CGRectMake(90, 60, 200, 40)];
self.myTextField.backgroundColor = [UIColor lightGrayColor];
self.myTextField.layer.cornerRadius = 5;
self.myTextField.layer.borderWidth = 1;
//    self.myTextField.textAlignment =
[self.view addSubview:self.myTextField];
[self.myTextField release];

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(140, 100, 100, 40);
[button setTitle:@"下一页" forState:UIControlStateNormal];
button.layer.cornerRadius = 3;
button.layer.borderWidth = 1;
[self.view addSubview:button];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)rightButtonAction:(UIBarButtonItem *)item
{

}

- (void)leftButtonAction:(UIBarButtonItem *)item
{

}

- (void)click:(UIButton *)button
{
// 用模态跳转到下一页
//    SecondViewController *secondVC =[[SecondViewController alloc] init];
//    [secondVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
//    [self presentViewController:secondVC animated:YES completion:^{
//        NSLog(@"haha");
//    }];
//    [secondVC release];
// 通过导航视图控制器进行跳转
// 先创建下一页对象
SecondViewController *secVC = [[SecondViewController alloc] init];
// 属性传值的第二步 即完成视图间传递数值,点击按钮切换界面后可在第二视图中使用值为100的self.number
//    secVC.number = 100;
//    self.myTextField.text = [NSString stringWithFormat:@"%ld", secVC.number];
//    self.myTextField.text = [self.myTextField.text stringByAppendingFormat:@"%ld", secVC.number];
secVC.str = self.myTextField.text;
secVC.arr = @[@"杨林",@"刘山山"];

// 推出下一页
[self.navigationController pushViewController:secVC animated:YES];
[secVC release];
}


@interface SecondViewController : UIViewController

//UINavigationController以栈的方式管理视图控制器。通过push和pop控制跳转
//UINavigationBar管理一组UINavigationItem,UINavigationItem包含了UIBarButtonItem。
//使用属性传值解决从前往后传值的问题
//使用delegate解决从后往前传值的问题

// 属性传值第一步,在第二个页面写一条属性
@property(nonatomic, assign)NSInteger number;
// 针对字符串写两条属性
@property(nonatomic, copy)NSString *str;
@property(nonatomic, retain)NSArray *arr;

@end


- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(140, 100, 100, 40);
[button setTitle:@"下一页" forState:UIControlStateNormal];
button.layer.cornerRadius = 3;
button.layer.borderWidth = 1;
[self.view addSubview:button];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
NSLog(@"%ld", self.number);

self.label = [[UILabel alloc] initWithFrame:CGRectMake(90, 150, 200, 50)];
self.label.backgroundColor = [UIColor brownColor];
self.label.layer.cornerRadius = 5;
self.label.layer.borderWidth = 1;
[self.view addSubview:self.label];
[self.label release];

self.label.text = self.str;

NSLog(@"%@", self.arr);
}


#import "MainViewController.h"
#import "SecondViewController.h"
// 4.(头文件已引完)签订协议
@interface MainViewController ()<SecondViewControllerDelegate>

@property(nonatomic, retain)UIButton *button;
@property(nonatomic, retain)UILabel *label;

@end

@implementation MainViewController

- (void)dealloc
{
[_button release];
[_label release];
[super dealloc];
}

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];

// 把导航控制器的半透明变为不透明
self.navigationController.navigationBar.translucent = NO;
self.title = @"劳力士";
self.navigationController.navigationBar.tintColor = [UIColor lightGrayColor];

self.button = [UIButton buttonWithType:UIButtonTypeSystem];
self.button.frame = CGRectMake(140, 100, 100, 40);
self.button.backgroundColor = [UIColor blueColor];
[self.button setTitle:@"下一页" forState:UIControlStateNormal];
self.button.layer.borderWidth = 1;
self.button.layer.cornerRadius = 5;
[self.view addSubview:self.button];
[self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

self.label = [[UILabel alloc] initWithFrame:CGRectMake(140, 200, 100, 40)];
self.label.backgroundColor = [UIColor cyanColor];
self.label.layer.borderWidth = 1;
self.label.layer.cornerRadius = 3;
[self.view addSubview:self.label];
[self.label release];
}

- (void)click:(UIButton *)button
{
SecondViewController *secVC = [[SecondViewController alloc] init];
// 5.设置代理人
secVC.delegate = self;
[self.navigationController pushViewController:secVC animated:YES];
[secVC release];
}

// 6.实现协议方法
- (void)changeValue:(NSString *)value
{
self.label.text = value;
NSLog(@"%@", value);
}


#import <UIKit/UIKit.h>
// 协议传值的第一步
// 1.声明一份协议
@protocol SecondViewControllerDelegate <NSObject>
// 协议方法
- (void)changeValue:(NSString *)value;

@end

@interface SecondViewController : UIViewController
// 2.设置代理人的属性
@property(nonatomic, assign)id<SecondViewControllerDelegate>delegate;

@end


- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];

self.button = [UIButton buttonWithType:UIButtonTypeSystem];
self.button.frame = CGRectMake(140, 100, 100, 40);
self.button.backgroundColor = [UIColor purpleColor];
[self.button setTitle:@"返回" forState:UIControlStateNormal];
self.button.layer.borderWidth = 1;
self.button.layer.cornerRadius = 5;
[self.view addSubview:self.button];
[self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

self.textField = [[UITextField alloc] initWithFrame:CGRectMake(140, 200, 100, 40)];
self.textField.backgroundColor = [UIColor magentaColor];
self.textField.layer.borderWidth = 1;
self.textField.layer.cornerRadius = 5;
[self.view addSubview:self.textField];
[self.textField release];

}

// 协议触发的条件是点击按钮,所以在这里面进行协议传值的第三步
- (void)click:(UIButton *)button
{
// 3.设置代理人执行的协议方法
[self.delegate changeValue:self.textField.text];
[self.navigationController popViewControllerAnimated:YES];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: