iOS开发代理的实现
来源:互联网 发布:南京网络教育本科 编辑:程序博客网 时间:2024/04/28 14:47
代理的使用:
代理的实现是这样的:如果首页想要下一个页面的数据,就是在首页点击跳转的页面数据,可以使用代理方法也可以使用别的
方法kvo。首页做跳转页面的代理。不知道你们明白否。
下面就是代理的怎么声明。方法怎么声明?好的方法在跳转的页面声明,声明的东西都是在这里面,我做你的代理我只管调用你的方法。剩下的我不管了。你弄好吧。如果不明白想想tableView是怎样使用的代理。
下面就是代码了:
//
// ViewController.h
// helloworld
//
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//
// ViewController.m
// helloworld
//
//
#import "ViewController.h"
#import "MyViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UpdateAlter>
{
UITableView *myTableView;
NSArray *tableArray;
NSMutableArray *tableArrayDetail;
MyViewController *myViewCotroller;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 30,320,400)];
myTableView.delegate = self;
myTableView.dataSource = self;
[self initWithData];
[self.view addSubview:myTableView];
}
-(void)initWithData
{
tableArray = [[NSArray alloc]initWithObjects:@"性别",@"年龄",@"性格取向",@"婚姻",@"地址",nil];
tableArrayDetail = [[NSMutableArray alloc]initWithObjects:@"男",@"20",@"保守",@"否",@"河南省",nil];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *string = @"Cell";
UITableViewCell *cell = [myTableView dequeueReusableCellWithIdentifier:string];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:string];
}
cell.textLabel.text = tableArray[indexPath.row];
cell.detailTextLabel.text = tableArrayDetail[indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.row)
{
case 0:
NSLog(@"1111111111111");
myViewCotroller = [[MyViewController alloc]init];
myViewCotroller.delegate = self;
// [self presentViewController:myViewCotroller animated:YES completion:^{}];
[self.navigationController pushViewController:myViewCotroller animated:YES];
break;
case 1:
NSLog(@"222222222222222");
break;
case 2:
NSLog(@"3333333333333");
break;
case 3:
NSLog(@"44444444444444");
break;
case 4:
NSLog(@"55555555555");
break;
default:
break;
}
}
-(void)update:(NSString *)text
{
NSLog(@"%@",text);
tableArrayDetail[0] = text;
[myTableView reloadData];
}
@end
//
// AppDelegate.m
// helloworld
//
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
ViewController *viewC = [[ViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:viewC];
self.window.rootViewController = nav;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
@end
//
// MyViewController.h
// helloworld
//
#import <UIKit/UIKit.h>
@protocol UpdateAlter <NSObject>
-(void)update:(NSString *)text;
@end
@interface MyViewController : UIViewController
@property(nonatomic,weak) id<UpdateAlter>delegate;
@end
//
// MyViewController.m
// helloworld
//
//
#import "MyViewController.h"
@interface MyViewController()
{
UITextField *_textFile;
}
@end
@implementation MyViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self initwithMyView];
}
-(void)initwithMyView
{
UITextField *textFile = [[UITextField alloc]initWithFrame:CGRectMake(30,100,100,30)];
textFile.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:textFile];
_textFile = textFile;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(140, 100, 60, 30)];
[button setTitle:@"Click" forState:0];
[button setTitleColor:[UIColor redColor] forState:0];
[button addTarget:self action:@selector(saveText:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)saveText:(UIButton *)sender
{
if([self.delegate respondsToSelector:@selector(update:)])
{
[self.delegate update:_textFile.text];
[self.navigationController popToRootViewControllerAnimated:YES];
}
}
@end
代理的实现是这样的:如果首页想要下一个页面的数据,就是在首页点击跳转的页面数据,可以使用代理方法也可以使用别的
方法kvo。首页做跳转页面的代理。不知道你们明白否。
下面就是代理的怎么声明。方法怎么声明?好的方法在跳转的页面声明,声明的东西都是在这里面,我做你的代理我只管调用你的方法。剩下的我不管了。你弄好吧。如果不明白想想tableView是怎样使用的代理。
下面就是代码了:
//
// ViewController.h
// helloworld
//
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//
// ViewController.m
// helloworld
//
//
#import "ViewController.h"
#import "MyViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UpdateAlter>
{
UITableView *myTableView;
NSArray *tableArray;
NSMutableArray *tableArrayDetail;
MyViewController *myViewCotroller;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 30,320,400)];
myTableView.delegate = self;
myTableView.dataSource = self;
[self initWithData];
[self.view addSubview:myTableView];
}
-(void)initWithData
{
tableArray = [[NSArray alloc]initWithObjects:@"性别",@"年龄",@"性格取向",@"婚姻",@"地址",nil];
tableArrayDetail = [[NSMutableArray alloc]initWithObjects:@"男",@"20",@"保守",@"否",@"河南省",nil];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *string = @"Cell";
UITableViewCell *cell = [myTableView dequeueReusableCellWithIdentifier:string];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:string];
}
cell.textLabel.text = tableArray[indexPath.row];
cell.detailTextLabel.text = tableArrayDetail[indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.row)
{
case 0:
NSLog(@"1111111111111");
myViewCotroller = [[MyViewController alloc]init];
myViewCotroller.delegate = self;
// [self presentViewController:myViewCotroller animated:YES completion:^{}];
[self.navigationController pushViewController:myViewCotroller animated:YES];
break;
case 1:
NSLog(@"222222222222222");
break;
case 2:
NSLog(@"3333333333333");
break;
case 3:
NSLog(@"44444444444444");
break;
case 4:
NSLog(@"55555555555");
break;
default:
break;
}
}
-(void)update:(NSString *)text
{
NSLog(@"%@",text);
tableArrayDetail[0] = text;
[myTableView reloadData];
}
@end
//
// AppDelegate.m
// helloworld
//
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
ViewController *viewC = [[ViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:viewC];
self.window.rootViewController = nav;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
@end
//
// MyViewController.h
// helloworld
//
#import <UIKit/UIKit.h>
@protocol UpdateAlter <NSObject>
-(void)update:(NSString *)text;
@end
@interface MyViewController : UIViewController
@property(nonatomic,weak) id<UpdateAlter>delegate;
@end
//
// MyViewController.m
// helloworld
//
//
#import "MyViewController.h"
@interface MyViewController()
{
UITextField *_textFile;
}
@end
@implementation MyViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self initwithMyView];
}
-(void)initwithMyView
{
UITextField *textFile = [[UITextField alloc]initWithFrame:CGRectMake(30,100,100,30)];
textFile.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:textFile];
_textFile = textFile;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(140, 100, 60, 30)];
[button setTitle:@"Click" forState:0];
[button setTitleColor:[UIColor redColor] forState:0];
[button addTarget:self action:@selector(saveText:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)saveText:(UIButton *)sender
{
if([self.delegate respondsToSelector:@selector(update:)])
{
[self.delegate update:_textFile.text];
[self.navigationController popToRootViewControllerAnimated:YES];
}
}
@end
1 0
- iOS开发代理的实现
- iOS开发,代理实现方法
- 【深入浅出IOS开发】彩票-代理实现子Controller的切换
- 【深入浅出IOS开发】彩票-代理实现子Controller的切换
- iOS开发之UitableViewCell中UISwitch的使用,代理实现
- ios软件开发委托代理实现介绍
- IOS开发-基于WebDriverAgent代理服务,实现iOS手机app自动化测试的框架搭建
- iOS开发中关于UIApplication的代理
- iOS开发代理协议的使用
- iOS开发中代理的简单理解
- iOS的动态代理模式的实现
- iOS开发之用block来实现UIAlertView点击事件的代理方法
- iOS代理方法的实现步骤
- ios 开发 代理模式
- iOS开发 UI高级之代理实现传值
- IOS Delegate代理实现
- 【iOS开发-54】案例学习:通过UIScrollView的缩放图片功能练习代理模式的具体实现
- 【iOS开发-26】利用协议代理实现导航控制器UINavigationController视图之间的正向传值和反向传值
- MATLAB入门
- C#文件操作
- Linux下的串口继续 转自http://www.cnblogs.com/jason-lu/articles/3173988.html
- 将java web项目转为maven项目
- 关于在eclipse中ADT的安装
- iOS开发代理的实现
- Stackoverflow上人气最旺的10个Java问题
- Centos6.5搭建LDAP服务器及用户添加删除
- 程序员如何坚持写作?
- mysql存储过程详解
- 利用端口转发访问VirtualBox虚拟机中的MySQL
- Hybrid APP架构设计思路
- (转)STDAPI释义
- java Integer和int之间==的比较问题 equal