ios-頁面傳值(協議、單例)

來源:互聯網
上載者:User

ios-頁面傳值(協議、單例)

在第一個頁面定義一個協議,同時定義一個協議的方法,用來擷取第二個頁面返回到第一個頁面的返回值。

#import

#pragma mark 定義一個協議用於參數傳遞
@protocol edelegate
-(void) setReturnPwd:(NSString *) password;
@end

在第一個頁面的實現檔案中,實現協議的方法,同時在跳轉到第二個頁面的方法中設定代理。

#import "ViewController.h"
#import "mainViewController.h"

@interface ViewController ()
@end


@implementation ViewController

#pragma mark 實現代理方法
-(void)setReturnPwd:(NSString *)password{
self._pwd.text=password;
}

-(void)login:(id)sender{
mainViewController *mainView=[self.storyboard instantiateViewControllerWithIdentifier:@"second"];//這是第二個頁面,要跳轉的頁面
mainView.delegate=self; //設定代理

[self presentViewController:mainView animated:YES completion:nil];

}

在第二頁面的標頭檔中定義代理

#import
@protocol edelegate;


@interface mainViewController : UIViewController

#pragma mark 定義代理

@property(nonatomic,strong) id delegate;

@end

在第二頁面的實現檔案中的返回第一個頁面的方法中調用代理傳參。

#import "mainViewController.h"
#import "ViewController.h"

@interface mainViewController ()
@end

@implementation mainViewController


-(void)backv:(id)sender{
[self.delegate setReturnPwd:self._sname.text]; //調用代理傳參
[self dismissViewControllerAnimated:YES completion:nil];
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.