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:
- Xử lí video và phát real-time
- Viết ứng dụng mạng xã hội cần những gì để có thể làm được?
- Làm sao load 100 cái hình trong IOS
- Làm sao cập nhập được thay đổi từ webservice trong UITableView
- Cài đặt của cơ chế quản lý bộ nhớ với retain/release/autorelease
- Trong Objective C có Abstract class và Reflection hay không ?
- So sánh NSString objective C.
- các pro chỉ em cách chèn file flash đuôi .swf và xcode với!
- Giải thích đối truyền vào hàm trong Objective C
- Làm thế nào để bắt sự kiện hiển thị thay đổi trong app iOS?
-
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]
Với sự đa dạng và phong phú về mẫu mã và giá cả, choxeonline.vn hứa hẹn sẽ là địa chỉ tin cậy cho những ai đang tìm kiếm chiếc xe ô tô ưng ý. Từ những mẫu xe hạng sang đến những chiếc xe gia đình...
Khám Phá Đa Dạng Sản Phẩm Xe Ô Tô...