必须调用超类'UITableViewController'的指定初始值设定项

时间:2018-03-01 13:27:45

标签: objective-c swift uitableview

这是Objective-C中的标题代码:

@interface DocumentCatalogViewController : UITableViewController <UITableViewDelegate,UIAlertViewDelegate,UISearchBarDelegate,UIViewControllerPreviewingDelegate,UITableViewDataSource,UIDocumentInteractionControllerDelegate>

@property NSArray *filePathsArray;
@property NSArray *filePathsArrayCopy;

-(void) openTheFileInNativeViewer : (NSURL *) destinationURL;

-(void) openTheFileInOtherApps : (NSURL *) destinationURL;

@property UISearchBar *searchBar;

@property (strong, nonatomic) IBOutlet UIProgressView *progressView;

-(void)refreshDataAfterDownloading;

-(UIImage *) fileIconForTheCell : (NSString *) documentType;

-(UISearchBar *) setSearchBar;

-(void) openFile : (NSURL *) destinationURL doctype : (NSString *) documenttype;

+(id) sharedManager;

@end

这是Objective-C中的.m代码:

@interface DocumentCatalogViewController()

@property (nonatomic,strong) NSManagedObjectContext* managedObjectContext;
@property (strong) NSArray *files;
@property (strong) NSArray *SearchedFiles;

@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) id previewingContext;
@property (nonatomic ,strong) UIDocumentInteractionController *documentInteractionController;
@property (nonatomic) BOOL hasCompatibleApps;
@property (nonatomic, strong) DocumentDownloadHandler *SessionWrapper;

@end

@implementation DocumentCatalogViewController
@dynamic refreshControl;
@synthesize searchBar;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
        // Custom initialization
        self.title = NSLocalizedString(@"mdm.agent.common.documents", nil);
        self.progressView =[[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
        CGAffineTransform transform = CGAffineTransformMakeScale(1.0f, 5.0f);
        self.progressView.transform = transform;

        [self.progressView setFrame:CGRectMake(0,60, self.tableView.bounds.size.width, 10)];
        self.progressView.progressTintColor=[UIColor greenColor];

    }
    return self;
}

+ (id) sharedManager {

    static dispatch_once_t onceToken;
    static DocumentCatalogViewController *sharedCatalogManager;
    dispatch_once(&onceToken, ^{
        sharedCatalogManager = [[[self class] alloc] init];
    });
    return sharedCatalogManager;
}

- (instancetype)init {
    self = [super init];

    return self;
}

在将其转换为Swift时,我无法转换单例函数,因为dispatch_once已弃用。所以我创建了一个静态变量,如下所示。

swift中的代码段

class DocumentCatalogViewController: UITableViewController, UIViewControllerPreviewingDelegate, UISearchBarDelegate, UIDocumentInteractionControllerDelegate, UIAlertViewDelegate {
    var filePathsArray = [Any]()
    var filePathsArrayCopy = [Any]()
    var searchBar: UISearchBar?
    @IBOutlet var progressView: UIProgressView!

    var managedObjectContext: NSManagedObjectContext?
    var files = [File]()
    var searchedFiles = [File]()
    var button: UIButton?
    var cancelButton: UIButton?
    var previewingContext: Any?
    var documentInteractionController: UIDocumentInteractionController
    var hasCompatibleApps = false
    var sessionWrapper: DocumentDownloadHandler?
    //here
    static let shared = DocumentCatalogViewController()
    //but this shows a error **Cannot invoke initializer for type 'DocumentCatalogViewController' with no arguments**

    override init(style: UITableViewStyle) {
        super.init(style: .grouped)
        title = NSLocalizedString("mdm.agent.common.documents", comment: "")
        progressView = UIProgressView(progressViewStyle: .bar)
        let transform = CGAffineTransform(scaleX: 1.0, y: 5.0)
        progressView.transform = transform
        progressView.frame = CGRect(x: 0, y: 60, width: tableView.bounds.size.width, height: 10)
        progressView.progressTintColor = UIColor.green

    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        //fatalError("init(coder:) has not been implemented")
    }

因此,我创建了一个空的初始化函数init()

init(){
    super.init()
}

然后它显示错误说:

  

必须调用超类'UITableViewController'

的指定初始值设定项

1 个答案:

答案 0 :(得分:0)

您正在调用UITableViewController超类不支持的super.init(),将其更改为initWithStyle