好的,如果我继承了一个类,它继承了类所有的方法和属性,但想象我有一堆代码和属性,我希望它对两个不同的类是通用的,是否可以通过继承?
例如假设一个UIViewController和一个UITableViewController。
答案 0 :(得分:0)
如果我理解你的问题,答案是肯定的。您可以拥有从同一个类继承的任意数量的类,但是您不能从两个类继承类。可能对你有用的是创建一个单例类。我有一个Utilities类,我用它来进行常见的操作,比如找到屏幕的中点,从缓存中删除文件,复制文件等。它有20个左右的方法,我在几个不同的类中使用。我有另一个单例类,用于设置和获取全局变量,如系统版本,字体,文本大小,播放器名称等。
我调用这样的实用程序方法:
NSString *resultsFilePath = [Utilities cachedFilePath:@"Results"];
NSString *fullResultsFilePath = [Utilities cachedFilePath:@"FullResults"];
NSString *troublesomeWordsFilePath = [Utilities cachedFilePath:@"TroublesomeTargets"];
[Utilities copyCachedResultsToFile];
全局变量如下:
if ([Globals sharedInstance].currentClient) {
self.clientInput.text = [Globals sharedInstance].currentClient;
}
My Utilities类的开头如下:
//
// Utilities.m
//
// Created by John Scarry on 11/3/11.
// Copyright (c) 2011 Learning Fundamentals, Inc. All rights reserved.
//
#import "Utilities.h"
#import "mach/mach.h"
@implementation Utilities
+ (CGPoint)findMidpoint:(UIView *)view {
CGPoint midPoint;
midPoint.x = view.bounds.origin.x + view.bounds.size.width/2;
midPoint.y = view.bounds.origin.y + view.bounds.size.height/2;
return midPoint;
}
+ (NSString *)formattedDate {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *todaysDate = [dateFormatter stringFromDate:[NSDate date]];
return todaysDate;
}
+ (NSString *)formattedClientName {
NSString *client = [NSString stringWithFormat:@" "];
if( [Globals sharedInstance].currentClient ) client = [NSString stringWithFormat:@" %@ ",[Globals sharedInstance].currentClient];
return client;
}
我的Globals课程就是这样开始的:
//
// Globals.m
//
// Created by John Scarry on 11/3/11.
// Copyright (c) 2011 Learning Fundamentals, Inc. All rights reserved.
//
#import "Globals.h"
@implementation Globals
static Globals *singleton = nil;
+(Globals *) sharedInstance {
if (nil != singleton) return singleton;
static dispatch_once_t onceToken; // lock
dispatch_once(&onceToken, ^{ // this code is called at most once
singleton = [[Globals alloc] init];
});
return singleton;
}
// Lots of the properties use a default value from the .pch file
// Use lazy instantiation to overide the getter to make sure it is set.
- (NSInteger) systemVersionNumber {
return [[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."][0] intValue];
}
- (NSString *)scoringType {
if ( !_scoringType ) _scoringType = SCORING_TYPE;
return _scoringType;
}
- (NSString *)targetSoundDelayCode {
if ( !_targetSoundDelayCode ) _targetSoundDelayCode = TARGET_SOUND_DELAY;
return _targetSoundDelayCode;
}
- (BOOL)checkBoxes {
if ( !_checkBoxes ) _checkBoxes = FORCED_CHOICE_SCORING;
return _checkBoxes;
}
- (BOOL)showFavorites {
if ( ! _showFavorites ) _showFavorites = NO;
return _showFavorites;
}
编辑:我为每个为我的全局变量设置默认值的应用程序使用.pch。 e.g。
#define FORCED_CHOICE_SCORING NO
#define SCORING_TYPE @"CDI"
然后我编写一个自定义的getter,如果用户没有更改它,则使用默认值。
- (NSUInteger)targetSoundDelay {
if ( !_targetSoundDelay ) _targetSoundDelay = TARGET_SOUND_DELAY];
return _targetSoundDelay;
}
- (NSString *)scoringType {
if ( !_scoringType ) _scoringType = SCORING_TYPE;
return _scoringType;
}