Chủ đề: Cách ép kiểu trong Objective C?
-
28-05-2013, 09:41 AM #1Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Thì ép kiểu thành cái bạn muốn rồi cộng vào thôi.
[txtOperand1.text intValue];
Để tối về làm thử nhưng đại khái là dùng intValue để lấy giá trị int từ trong một id (Object trong C Object) [IMG]images/smilies/smile.png[/IMG]View more random threads:
- Viết ứng dụng mạng xã hội cần những gì để có thể làm được?
- IQ Widgets has a Gantt chart. You would need to generate bindings in order to use it in MonoTouch.
- Cách lấy giá trị trong Mảng ứng với time hiện tại?
- Giải thích đối truyền vào hàm trong Objective C
- Các bác giúp em với
- Có cách nào vừa download 1 file nhạc (mp3) trên server vừa play file nhạc?
- Làm sao hẹn giờ chơi nhạc
- Cài đặt của cơ chế quản lý bộ nhớ với retain/release/autorelease
- Sự khác biệt giữa các cách khai báo biến
- Mọi người định hướng giúp cái bài hướng đối tượng này với !!!
-
28-05-2013, 06:21 PM #2Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Cách ép kiểu trong Objective C?
Mã:#import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (strong, nonatomic) IBOutlet UITextField *txtOperand1; @property (strong, nonatomic) IBOutlet UITextField *txtOperand2; @property (strong, nonatomic) IBOutlet UILabel *lblResult; - (IBAction)resultAdd; - (IBAction)resultSubtract; - (IBAction)resultMultiply; - (IBAction)resultDivide; @end
Mã:#import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize txtOperand1, txtOperand2, lblResult; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)resultAdd { } - (IBAction)resultSubtract { } - (IBAction)resultMultiply { } - (IBAction)resultDivide { } @end
số được lấy ra từ txtOperand1 và txtOperand2 để làm phép tính rồi in kết quả ra label, mình ko biết convert kiểu gì. Mọi người chỉ giúp mình với. Thanks
-
06-06-2013, 10:01 AM #3Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
- Đầu tiên bạn cần lấy giá trị từ 2 textfield ra và chuyển sang kiểu integer:
Mã:NSInteger value1 = [self.txtOperand1.text integerValue]; NSInteger value2 = [self.txtOperand2.text integerValue];
Mã:NSInteger tong = value1 + value2; NSInteger hieu = value1 - value2; NSInteger tich = value1*value2; CGFloat thuong = (CGFloat)value1/(CGFloat)value2;
- Cuối cùng bạn gán giá trị vào label:
Mã:self.lblResult.text = [NSString stringWithFormat:@"Tong:%d", tong]; self.lblResult.text = [NSString stringWithFormat:@"Hieu:%d", hieu]; self.lblResult.text = [NSString stringWithFormat:@"Tich:%d", tich]; self.lblResult.text = [NSString stringWithFormat:@"Thuong:%.2f", thuong];
Chúc bạn thành công [IMG]images/smilies/smile.png[/IMG]
Ngọc Bích xanh bản chất thuộc dòng đá đá hoa (jade). Và cẩm thạch là tên gọi chung của ngọc bích. Vì thực chất chúng thuộc dòng đá đa khoáng được hình thành từ chất Silicat dưới dạng dioxy. Ngọc bích...
Chia sẻ Vòng tay ngọc bích xanh là...