码迷,mamicode.com
首页 > 移动开发 > 详细

ios两视图间委托(delegate)传值

时间:2014-10-08 18:07:24      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:视图   委托delegate   传值   

现有两个视图(ViewController,ViewController1),从ViewController中带参数跳转到ViewController1,在ViewController1选中数据后带有效数据后退到ViewController中,委托实现上述功能。

项目:点击下载

一、ViewController主要代码

// 按钮点击事件
-(IBAction)clickSearchBtn:(id)sender {
    
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    
    ViewController1 *_viewController = [[[ViewController1 alloc] init] autorelease];
    _viewController.delegate = self;
    _viewController.transText = self.textField.text;
    
    [self.navigationController pushViewController:_viewController animated:YES];
}

// ViewController1Delegate 的代理方法
-(void)selectData:(NSString*)text{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self.listArray removeAllObjects];
        for (int i=0; i<5; i++) {
            [self.listArray addObject:[NSString stringWithFormat:@"第%d行数据:%@_%d",i+1,text,arc4random_uniform(100)]];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.dataTableView reloadData];
        });
    });
}

二、ViewController1主要代码

// ViewController1.h
// delegate写在#import之前,否则可能会Delegate Cannot find protocol declaration
@protocol ViewController1Delegate;
@protocol ViewController1Delegate <NSObject>

@optional
-(void)selectData:(NSString*)text;
@end

// ViewController1.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.delegate respondsToSelector:@selector(selectData:)]) {
        
        //通过委托协议传值
        [self.delegate selectData:[self.listArray objectAtIndex:indexPath.row]];
        [self.navigationController popViewControllerAnimated:YES];
    }
}


三、效果图

bubuko.com,布布扣 bubuko.com,布布扣 bubuko.com,布布扣

ios两视图间委托(delegate)传值

标签:视图   委托delegate   传值   

原文地址:http://blog.csdn.net/fengshi_sh/article/details/39890955

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!