函數的調用方式有三種:
第一種:通過self來調用本對象的成員函數
- (IBAction)captureOrderButtonPressed:(UIButton *)sender
{
[self checkUserStateWithSender:sender];
}
第二種:通過類名調用靜态全局函數
MBProgressHUD.h
+(void)hudShowWithStatus :(id)viewcontroller : (NSString *)string;//顯示1.8消失的提示框
MBProgressHUD.m
+(void)hudShowWithStatus :(id)viewcontroller :(NSString *)string
{
[MBProgressHUD hudShowWithStatus:viewcontroller :string intervl:1.8];
}
調用處:
[MBProgressHUD hudShowWithStatus:self :[error localizedDescription]];
第三種:通過單例對象來調用單例的函數。
API.h
+ (instancetype)shareAPI;
- (void)updateLocationParams:(NSDictionary *)params block:(void(^)(NSError *error))block;//更新地理位置
API.m
- (void)updateLocationParams:(NSDictionary )params block:(void (^)(NSError ))block
{
NSMutableDictionary *muParams = [NSMutableDictionary dictionaryWithDictionary:params];
[muParams setObject:g_updateLocationCmd forKey:@”cmdCode”];
[self GET:@”resetLngLatJsonPhone.htm” params:muParams success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (responseObject) {
FLDDLogDebug(@”success”);
if (block) {
block(nil);
}
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
FLDDLogInfo(@”error”);
if (block) {
block(error);
}
}];
}
調用:
[[API shareAPI] updateLocationParams:muParams block:^(NSError *error) {
g_updatingLocation = NO;
if (!error) {
FLDDLogDebug(@”updata location success”);
g_loginStat = LOGIN_STATE_LOGIN_SUCESS;
if (self.segmentedControl.selectedSegmentIndex == 0)
{
[self setupRefresh];
}
else
{
[self getOrders];
}
// [self getOrders];
[self showNoticeView];
// [self setupRefresh];
}
else
{
g_loginStat = LOGIN_STATE_UNLOCATION_LOGIN;
}
[self showNoticeView];
}];
+ (instancetype)shareAPI
{
static API *shareAPI = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
#ifdef DEBUG
NSString *baseUrl = [AppManager valueForKey:@"RootUrl"];
if (baseUrl.length == 0) {
baseUrl = @"http://test.zuixiandao.cn/fhl/phone/psy/";
[AppManager setUserDefaultsValue:baseUrl key:@"RootUrl"];
}
shareAPI = [[API alloc] initWithBaseURL:[NSURL URLWithString:baseUrl]];
#else
shareAPI = [[API alloc] initWithBaseURL:[NSURL URLWithString:BaseURL]];
#endif
});
shareAPI.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];//設定相應内容類型
shareAPI.securityPolicy.allowInvalidCertificates = NO;
[shareAPI.requestSerializer setTimeoutInterval:g_requeRespondTime];
return
單例又稱單子,保證系統的在調用中隻生成一個對象,是以若把全局變量存在單例裡面就能在通過這個單例通路這些全局變量了。是以IOS的APP幾乎都用全局變量,也可以實作部分全局函數代替部分靜态全局函數。
通過以下幾行代碼就能實作單例,簡單吧:
+(Singleton *) sharedInstance
{
static Singleton *sharedInstace = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstace = [[self alloc] init];
});
return sharedInstace;