Chào cả nhà.
Em mới bắt đầu tìm hiểu về Objective C
đang làm một ứng dụng đơn giản về Delegate
em tạo một ứng dụng iOS đơn giản. có 2 ViewController: ViewController + NextViewController
Ở ViewController có chứa label + button1 (Click Me)
Ở NextViewController có cửa TextField + button2 (OK)
Khi bắt đầu chạy chương trình, thì ViewController 1 hiện lên, lúc này label = rỗng,
Em click button1 trên ViewController thì chuyển sang NextViewController
tại đây. ta có thể nhập String vào TextField
Sau khi nhập xong, click button2, thì ứng dụng trở về ViewController và Label sẽ nhận giá trị vừa nhập vào TextField ở NextViewController
Trên là ý định em làm một program đơn giản như vậy
Nhưng hiện tại, program của em vẫn chưa hoạt động chính xác.
Sau khi nhập giá trị vào TextField ở ViewController2, mà Label trên ViewController1 vẫn chưa nhận dc giá trị
Nhờ cả nhà xem em bị sai chỗ nào với ạ?
NextViewController.h
Objective C Code:
#import <UIKit/UIKit.h> NS_ASSUME_NONNULL_BEGIN @protocol SendData -(void)showString:(NSString *)string; @end @interface NextViewController : UIViewController @property (weak, nonatomic) IBOutlet UITextField *txtFullName; @property (weak, nonatomic) IBOutlet UIButton *btnOK; @property(weak,nonatomic)id<SendData>delegate; @end NS_ASSUME_NONNULL_END
NextViewController.m
Objective C Code:
#import "NextViewController.h" @implementation NextViewController - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction)btnOKMethod:(id)sender { [self.delegate showString:self.txtFullName.text]; [self dismissViewControllerAnimated:YES completion:nil]; } @end
ViewController.h
Objective C Code:
#import <UIKit/UIKit.h> #import "NextViewController.h" @interface ViewController : UIViewController<SendData> @property (weak, nonatomic) IBOutlet UILabel *lblResult; @end
ViewController.m
Objective C Code:
#import "ViewController.h" #import "NextViewController.h" @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.lblResult.text = @""; NextViewController *nextViewController = [[NextViewController alloc]init]; nextViewController.delegate = self; } - (IBAction)Click:(id)sender { } - (void) showString:(NSString *)string{ self.lblResult.text = string; } @end
Mong nhận được chỉ hướng dẫn của mọi người ạ !