如何解决错误'UIViewController'没有可见的@interface声明选择器''

Posted

技术标签:

【中文标题】如何解决错误\'UIViewController\'没有可见的@interface声明选择器\'\'【英文标题】:How to solve the error 'No visible @interface for 'UIViewController' declares the selector ''如何解决错误'UIViewController'没有可见的@interface声明选择器'' 【发布时间】:2014-10-14 13:18:22 【问题描述】:

我正在使用 Xcode 5

这是我的 AppDelegate.h 文件

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) UIViewController *viewController;

@end

这是我的 AppDelegate.m 文件

#import "AppDelegate.h"

#import "ViewController.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;


- (void)applicationWillResignActive:(UIApplication *)application

     // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.


- (void)applicationDidEnterBackground:(UIApplication *)application

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
[self.viewController saveData];
     

    - (void)applicationWillEnterForeground:(UIApplication *)application
    
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    [self.viewController loadData];
    

- (void)applicationDidBecomeActive:(UIApplication *)application

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.


- (void)applicationWillTerminate:(UIApplication *)application

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.


@end

这是我的 ViewController.h 文件

#import <UIKit/UIKit.h>

#import <AVFoundation/AVFoundation.h>

//some variables declared here

@interface ViewController : UIViewController <UIActionSheetDelegate, UINavigationBarDelegate, UIImagePickerControllerDelegate>
    //some outlets declared here


//some actions and - (void) declared here

- (NSString *) getFilePath;

- (void) saveData;

- (void) loadData;

@end

在代码[self.viewController saveData][self.viewController loadData] 旁边有一条错误消息:'No visible @interface for 'UIViewController' declares the selector 'saveData''No visible @interface for 'UIViewController' declares the selector 'loadData'。 我该怎么办?

【问题讨论】:

只需在您的 AppDelegate.h 文件中更新,从 UIViewController 到 ViewController。你的实际班级名称是哪个。 【参考方案1】:

您应该在 AppDelegate.h 文件中指定 UIViewController 的类:

@property (strong, nonatomic) ViewController *viewController;

并在此文件中添加导入语句

#import "ViewController.h"

【讨论】:

我按照您的解决方案,这两条错误消息消失了,但出现了 Apple Mach-O Linker Error。 @홍승욱 你有什么错误信息?检查,您没有在 ViewController.h / ViewController.m 中导入 AppDelegate.h【参考方案2】:

您的viewController 是 UIViewController 类的子类,因此没有 saveData/LoadData 方法。 这两种方法存在于您的自定义类 - ViewController 中。

解决方案是替换 AppDelegate.h 中的行:

@property (strong, nonatomic) UIViewController *viewController;

到:

@property (strong, nonatomic) ViewController *viewController;

或每次调用 saveData/loadData 时将 viewController 转换为 ViewController

【讨论】:

【参考方案3】:

如果应用程序委托中的视图控制器确实是ViewController 对象,则将属性更改为: @property (strong, nonatomic) ViewController *viewController;

您还需要匹配的 @class (.h) 和 #import (.m) 语句。

【讨论】:

以上是关于如何解决错误'UIViewController'没有可见的@interface声明选择器''的主要内容,如果未能解决你的问题,请参考以下文章

错误 UIViewController 没有成员

记录项目中所有 UIViewController 的类名

如何以编程方式将 UIViewController 的方向更改为横向?

如何比较 Swift 3 中的 UIViewController?

如何在通用 UIViewController 之后设置自定义 UIViewController?

如何通过 SwiftUI 按钮在 UIViewController 中执行功能?