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:
- Objective C: alloc và Release ngay khi nó vẫn được sử dụng không bị lỗi, nghĩa là thế nào?
- [Core Data]Cách lấy data cho vào Cell của UITableView?
- Sử dụng file .PNG làm giao diện phần mềm
- Code trong objective c có chạy được trong c++ không
- Từ UIViewController gọi đến UINavigationController thì làm thế nào?
- Hiển thị ảnh lên UIScrollview lỗi: Terminating app due to uncaught exception ... was mutated while being enumerated?
- [Thắc mắc] Cách sử dụng thư viện dựng sẵn igraph.h dành cho C++ trên VS 2013
- Lập trình Objectice C - Cùng trao đổi kinh nghiệm về lập trình IOS
- Mở view 2 từ view 1 trong lập trình Objective-C như thế nào?
- Cố định cho Lanscapes cho màn hình
-
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]
Sự đa dạng trong các mẫu mã và kiểu dáng của sản phẩm Đồ Thờ Gỗ cũng là điểm thu hút đặc biệt. Từ những tượng phật nhỏ xinh, đến các bức thư pháp hoặc các bát hương gỗ được điêu khắc tỉ mỉ, đều mang...
Tinh Thần Truyền Bá: Skyhome.vn và...