ios兩視圖間委託(delegate)傳值

來源:互聯網
上載者:User

ios兩視圖間委託(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 @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];    }}


三、


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.