UINavigationController及界面传值

本文详细介绍了iOS中UINavigationController的使用方法,包括如何创建导航控制器、配置导航栏样式、管理视图控制器栈以及如何通过导航项添加按钮等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

- (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;
}

UINavigationBar结构

- (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];
}
安装Docker安装插件,可以按照以下步骤进行操作: 1. 首先,安装Docker。可以按照官方文档提供的步骤进行安装,或者使用适合您操作系统的包管理器进行安装。 2. 安装Docker Compose插件。可以使用以下方法安装: 2.1 下载指定版本的docker-compose文件: curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose 2.2 赋予docker-compose文件执行权限: chmod +x /usr/local/bin/docker-compose 2.3 验证安装是否成功: docker-compose --version 3. 在安装插件之前,可以测试端口是否已被占用,以避免编排过程中出错。可以使用以下命令安装netstat并查看端口号是否被占用: yum -y install net-tools netstat -npl | grep 3306 现在,您已经安装Docker安装Docker Compose插件,可以继续进行其他操作,例如上传docker-compose.yml文件到服务器,并在服务器上安装MySQL容器。可以参考Docker的官方文档或其他资源来了解如何使用DockerDocker Compose进行容器的安装和配置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Docker安装docker-compose插件](https://blog.youkuaiyun.com/qq_50661854/article/details/124453329)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Docker安装MySQL docker安装mysql 完整详细教程](https://blog.youkuaiyun.com/qq_40739917/article/details/130891879)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值