首先.h檔跟storyboard(或xib檔)連結,h file長這樣:
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
@interface Page1 : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *in_volt;
@property (weak, nonatomic) IBOutlet UILabel *out_volt;
@property (weak, nonatomic) IBOutlet UILabel *load;
@property (weak, nonatomic) IBOutlet UILabel *bat_level;
@property (weak, nonatomic) IBOutlet UILabel *bat_backup_time;
@property (weak, nonatomic) IBOutlet UILabel *ups_status;
@property (weak, nonatomic) IBOutlet UILabel *bat_stauts;
@property (weak, nonatomic) IBOutlet UILabel *power_condition;
@end
step1:
第一步是要讓UILable可以顯示資料
所以很簡單,在要觸發的地方寫下self.(UILabel名稱).text = (nsstring型態);
好了,很顯然第一個問題就是,我們現在是object型態,該怎樣轉NSString呢?
step2:
因為要顯示的是數字,所以先把它轉為int
object to int :
[(NSString *)object integerValue]
這樣就變成int型態囉!
step3:
轉int還是不能顯示在UILabel上阿!
所以我們還需要再轉一次,這時候使用
[NSString stringWithFormat:@"%d", (int型態)]
但是呢!Xcode會跟你說warning,所以要改成
NSString stringWithFormat:@"%ld", (強制轉換型態成long,就是前面加個(long)這樣)]
完成就長這樣:
self.in_volt.text = [NSString stringWithFormat:@"%ld", (long)[(NSString *)你填入的object型態在這邊 integerValue]];
完成!
end