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


1 0