完成下面输入值传递的功能: 创建一个根视图控制器,加载在window上。并且添加一个lable标签和button按钮。单击button时,弹出一个模态视图。模态视图中包含一个返回按钮和textField输入框。修改textField的值并点击返回按钮返回到根视图时,将textField的值显示到label标签上。
下面用代理实现:
首先要清楚谁需要委托,Modal想要去修改label的值,但是他自己做不了这件事情,所以Modal需要找一个具备这样能力的一个委托人。
先来创建根视图控制器和模态视图控制器并设置label、button、textField:
RootViewController:
- (void)viewDidLoad {
[super viewDidLoad];
//背景颜色
self.view.backgroundColor = [UIColor grayColor];
//设置Label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 150, 50)];
label.tag = 101;
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor orangeColor];
[self.view addSubview:[label autorelease]];
//设置Button
UIButton *goButton = [UIButton buttonWithType:UIButtonTypeCustom];
goButton.frame = CGRectMake(100, 200, 150, 50);
goButton.backgroundColor = [UIColor orangeColor];
[goButton setTitle:@"跳转" forState:UIControlStateNormal];
[goButton addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:goButton];
}
- (void)btnClick:(UIButton *)btn
{
ModalViewController *modalVC = [[ModalViewController alloc] init];
//弹出模态视图
[self presentViewController:modalVC animated:YES completion:nil];
[modalVC release];
}ModalViewController:
- (void)viewDidLoad {
[super viewDidLoad];
//背景颜色
self.view.backgroundColor = [UIColor orangeColor];
//设置TextField
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 150, 50)];
textField.tag = 101;
textField.placeholder = @"请输入内容";
textField.backgroundColor = [UIColor whiteColor];
[self.view addSubview:[textField autorelease]];
//设置Button
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(100, 200, 150, 50);
backButton.backgroundColor = [UIColor grayColor];
[backButton setTitle:@"返回" forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backButton];
}-(void) btnClick: (UIButton *) btn
{
//关闭模态视图
[self dismissViewControllerAnimated:YES completion:nil];
}AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
RootViewController *rootVC = [[RootViewController alloc] init];
self.window.rootViewController = [rootVC autorelease];
return YES;
}基本框架已经完成,开始设置代理模式
Modal要去传一个值修改label,自己不能做,找个代理人,并且代理人应该具备修改label的这样一个功能
在Modal中添加协议和代理人:
@protocol ModalViewControllerDelegate <NSObject>
-(void) changeLabelText: (NSString *) text;
@end
@interface ModalViewController : UIViewController
@property (nonatomic, assign) id<ModalViewControllerDelegate> delegate; //设置代理人
@end并在自己的实现文件中让代理去做这件事情:
点击返回时设置
-(void) btnClick: (UIButton *) btn
{
//获取textFiled
UITextField *textField = (UITextField *) [self.view viewWithTag:101];
//安全判断
if ( [self.delegate respondsToSelector:@selector(changeLabelText:)] ) {
//代理去更改labelText
[self.delegate changeLabelText:textField.text];
}
//关闭模态视图
[self dismissViewControllerAnimated:YES completion:nil];
}Root要做代理人就要遵守协议:
@interface RootViewController : UIViewController <ModalViewControllerDelegate>
@end遵守了协议当然要实现:
//实现协议的方法,功能是将text的内容显示到label上来
-(void) changeLabelText: (NSString *) text;
{
UILabel *label = (UILabel *)[self.view viewWithTag:101];
label.text = text;
}并且在点击跳转按钮时设置自己为Modal的代理:
- (void)btnClick:(UIButton *)btn
{
ModalViewController *modalVC = [[ModalViewController alloc] init];
modalVC.delegate = self; //设置代理
//弹出模态视图
[self presentViewController:modalVC animated:YES completion:nil];
[modalVC release];
}ok,大功告成,试试效果:
点击跳转,并在弹出界面输入框中输入文字:
代码资源: http://download.youkuaiyun.com/detail/shawn_chang/8431467
本文介绍如何在UI基础上通过代理实现页面跳转和输入值的传递。创建根视图控制器,添加label和button,点击button弹出模态视图,模态视图含返回按钮和textField。当在textField输入值并点击返回,代理会将值显示在label上。文章详细阐述了创建视图控制器、设置代理以及在模态视图中传递值的过程。
1万+

被折叠的 条评论
为什么被折叠?



