UI5_UINavigation传值
2024-09-19 19:12:44
//
// AppDelegate.m
// UI5_UINavigation传值
//
// Created by zhangxueming on 15/7/7.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "AppDelegate.h"
#import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
ViewController *rootViewController = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rootViewController];
self.window.rootViewController = nav; return YES;
}
//
// ViewController.h
// UI5_UINavigation传值
//
// Created by zhangxueming on 15/7/7.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <UIKit/UIKit.h>
#import "SubViewController.h" @interface ViewController : UIViewController <sendTitleReport> @end //
// ViewController.m
// UI5_UINavigation传值
//
// Created by zhangxueming on 15/7/7.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "ViewController.h"
#import "SubViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor cyanColor]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(100, 200, self.view.frame.size.width-200, 50);
[btn setTitle:@"push" forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont boldSystemFontOfSize:24];
[btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn]; self.navigationItem.title = @"按钮一"; } - (void)btnClicked
{
NSLog(@"-------");
SubViewController *svc = [[SubViewController alloc] init];
//正向传值: 最简单的做法, 在子视图控制器中定义属性实例变量接受值
svc.delegate = self;//设置代理
svc.currentTitle = self.navigationItem.title;
[self.navigationController pushViewController:svc animated:YES];
} - (void)sendButtonTitle:(NSString *)title
{
self.navigationItem.title = title;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
//
// SubViewController.h
// UI5_UINavigation传值
//
// Created by zhangxueming on 15/7/7.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <UIKit/UIKit.h> @protocol sendTitleReport <NSObject> - (void)sendButtonTitle:(NSString *)title; @end @interface SubViewController : UIViewController //记录当前被选中按钮的标题
@property (nonatomic, copy)NSString *currentTitle;
//防止相互引用
@property (assign, nonatomic) id <sendTitleReport> delegate; @end //
// SubViewController.m
// UI5_UINavigation传值
//
// Created by zhangxueming on 15/7/7.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "SubViewController.h" @interface SubViewController () @end @implementation SubViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
NSArray *titles = @[@"按钮一",@"按钮二",@"按钮三"];
for (int i= 0; i<3; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(100, 100+i*100, self.view.frame.size.width-200, 50);
[btn setTitle:titles[i] forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont boldSystemFontOfSize:24];
[btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
btn.tag = 200+i;
//设置选中状态
if ([self.currentTitle isEqualToString:btn.currentTitle]) {
btn.selected = YES;
}
[self.view addSubview:btn];
}
} - (void)btnClicked:(UIButton *)btn
{
if([self.delegate respondsToSelector:@selector(sendButtonTitle:)])
{
[self.delegate sendButtonTitle:btn.currentTitle];
}
[self.navigationController popViewControllerAnimated: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
最新文章
- Spring(三)__aop编程
- 如何使用花生壳 发布WCF服务 进行外网访问
- [BZOJ2792][Poi2012]Well
- ACCESS自动编号重新从1开始
- hdu1251(字典树)
- epoll_create, epoll_ctl和epoll_wait
- 在ModelSim波形图中以参数名显示变量
- [CSAPP笔记][第十二章并发编程]
- tomcat 产生heapdump文件配置
- A. Vasya and Football
- 总结过滤器,监听器,servlet的异同点,已经执行顺序。
- HIT2019春软件构造->;正则表达式语法
- Jdk_API——1.8和Jdk_API1.6下载分享
- 使用Mybatis Generator插件自动生成映射文件(cmd无法进入文件,dns服务器对区域没有权威等问题)遇到问题
- (二)Wireshark的实用表格
- datagridview的一些设置
- asp.net开发细节整理
- 透过摩拜和ofo,看产品从0到1时如何取舍需求(转)
- 【总结 】550,535,553 Mail from must equal authorized user— jenkins(hudson) email163邮箱和26邮箱成功配置总结
- Flutter - 左右侧滑菜单:drawer和endDrawer