通知模式實現兩個textField傳值及模態視圖——iOS開發

來源:互聯網
上載者:User

標籤:ios開發   oc   傳值   textfield   模態視圖   

通知模式實現兩個textField傳值及模態視圖——iOS開發

利用通知模式,實現兩個不同介面的textField之間的傳值,在介面二輸入字元,傳值到前一介面的textField。

介面的切換,這裡暫時先用模態視圖實現。(重點在傳值,所以沒糾結設計介面排版,醜了點大家見諒)

大家不要看代碼看上去好像挺多,因為我沒使用storyboard/xib,是代碼實現布局,所以通知和模態視圖切換的代碼很少~

實現效果:

點擊下一頁按鈕,進入介面二:

在textField處輸入字串:

點擊返回按鈕,回到介面一,此時介面一的textField處也有字串

代碼部分:

AppDelegate.m

#import "AppDelegate.h"#import "ViewController1.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];    self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];    ViewController1 *vc1 = [[ViewController1 alloc] init];    self.window.rootViewController = vc1;    return YES;}

ViewController1.m

#import "ViewController1.h"#import "ViewController2.h"@interface ViewController1 (){    UITextField *text1;}@end@implementation ViewController1- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor greenColor];    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 50)];    label.text = @"介面一";    [self.view addSubview:label];    text1 = [[UITextField alloc] initWithFrame:CGRectMake(50, 150, 100, 50)];    text1.backgroundColor = [UIColor whiteColor];    [self.view addSubview:text1];    UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(50, 250, 100, 50)];    [button1 addTarget:self action:@selector(pushButtonAction:) forControlEvents:UIControlEventTouchUpInside];    [button1 setTitle:@"下一頁" forState:UIControlStateNormal];    button1.backgroundColor = [UIColor blackColor];    [self.view addSubview:button1];//    註冊觀察者    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(returnAction:) name:@"chuanzhi" object:nil];    // Do any additional setup after loading the view.}- (void)returnAction:(NSNotification *)text {    text1.text = text.userInfo[@"returnInfo"];}- (void)pushButtonAction:(UIButton *)btn {    ViewController2 *vc2 = [[ViewController2 alloc] init];    vc2.modalPresentationStyle = UIModalPresentationFullScreen;  //轉場效果    [self presentViewController:vc2 animated:YES completion:nil];}

ViewController2.m

#import "ViewController2.h"@interface ViewController2 (){    UITextField *text2;}@end@implementation ViewController2- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor blueColor];    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 50)];    label.text = @"介面二";    [self.view addSubview:label];    text2 = [[UITextField alloc] initWithFrame:CGRectMake(50, 150, 100, 50)];    text2.backgroundColor = [UIColor whiteColor];    [self.view addSubview:text2];    UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(50, 250, 100, 50)];    [button2 addTarget:self action:@selector(returnButtonAction:) forControlEvents:UIControlEventTouchUpInside];    [button2 setTitle:@"返回" forState:UIControlStateNormal];    button2.backgroundColor = [UIColor blackColor];    [self.view addSubview:button2];    // Do any additional setup after loading the view.}- (void)returnButtonAction:(UIButton *)btn {    NSDictionary *dic = @{@"returnInfo" : self->text2.text};    NSNotification *notification = [NSNotification notificationWithName:@"chuangzhi" object:nil userInfo:dic];    [[NSNotificationCenter defaultCenter] postNotification:notification];    [self dismissViewControllerAnimated:NO completion:nil];}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

通知模式實現兩個textField傳值及模態視圖——iOS開發

聯繫我們

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