【问题标题】:Property 'images' not found on object of type 'UIViewController *'在“UIViewController *”类型的对象上找不到属性“图像”
【发布时间】:2013-11-12 05:07:50
【问题描述】:

我正在尝试合并这两个项目。

Bearded - An iPhone photo app

Thumbnail Picker View

此时我只是试图将它们保存在单独的视图控制器中,并让缩略图选择器的功能在它自己的控制器中工作。

我收到了错误,如标题中所述,“在 'UIViewController *' 类型的对象上找不到属性 'images'”

错误来自 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]];
self.viewController = [[UIViewController alloc] init];

NSArray *paths = [[NSBundle mainBundle] pathsForResourcesOfType:@"jpg" inDirectory:nil];
NSMutableArray *images = [NSMutableArray arrayWithCapacity:paths.count];
for (NSString *path in paths) {
    [images addObject:[UIImage imageWithContentsOfFile:path]];
}

self.viewController.images = images;
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];

return YES;
}


@end

这里是 AppDelegate.h:

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIViewController *viewController;

@end

我尝试过像这样声明一个属性:

@property (strong, nonatomic) UIImageView *images;

即使我煞费苦心地合并了这两个代码库,我似乎也无法解决这个问题。

这是我目前所处的 Xcode 项目:

Xcode project

【问题讨论】:

  • 我不知道为什么这被否决了。我认为合并两个代码库(例如本例中来自 GitHub 的)对于 iOS 开发人员来说非常重要。

标签: ios iphone objective-c uiviewcontroller


【解决方案1】:

您在 .h 文件中声明的“images”属性:

@property (strong, nonatomic) UIImageView *images;

不是您尝试通过此行分配给它的 NSMutableArray:

self.viewController.images = images;

如果您可以使类型相同(即单个 UIImageView 对象或包含许多 UIImages 的 NSMutableArray),那么您应该更好地为该属性分配东西。

【讨论】:

  • 是的,但是,该属性行是尝试修复错误的尝试。它不在我链接到的原始“缩略图选择器视图”项目中。当代码相同时,我不明白为什么它在那个项目中运行而不是在我的项目中运行。我唯一做的就是改变:@property (strong, nonatomic) ViewController *viewController;@property (strong, nonatomic) UIViewController *viewController;因为我在那里也遇到了错误。
  • 我建议您更改两者的名称以避免混淆。
  • 您使用通用 UIViewController 更改自定义(继承) ViewController 的对象。在项目中应该有一个 ViewController.m 和 .h 将被实现和当前文件使用它的对象......希望它有帮助
  • @K'am 我确实尝试将这一行:self.viewController = [[ViewController alloc] init]; 更改为 self.viewController = [[UIViewController alloc] init];
  • @K'am 另外,该项目中的 ViewController 文件已重命名为 ElfViewController。由于我已合并这两个项目并使用 Storyboard,因此我不得不重命名这些控制器。
【解决方案2】:

我解决了。问题是我正在合并一个使用 Storyboards 的项目和一个使用以编程方式生成的 ViewController 的项目。好吧,问题的一部分。

因为,我合并了我不得不给视图控制器和 ThumbnailView 另一个名称的项目。我将其命名为 ElfViewController。

这是工作代码:

AppDelegate.h

#import <UIKit/UIKit.h>

@class ViewController;
@class ElfViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ElfViewController *viewController; // Changed here to ElfViewController
@property (strong, nonatomic) ViewController *firstViewController;

@end

AppDelegate.m

#import "AppDelegate.h"
#import "ViewController.h"
#import "ElfViewController.h"

@implementation AppDelegate

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

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

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ElfViewController alloc] init]; //Changed here to ElfViewController
self.firstViewController = [[ViewController alloc] init];

NSArray *paths = [[NSBundle mainBundle] pathsForResourcesOfType:@"jpg" inDirectory:nil];
NSMutableArray *images = [NSMutableArray arrayWithCapacity:paths.count];
for (NSString *path in paths) {
    [images addObject:[UIImage imageWithContentsOfFile:path]];
}

self.viewController.images = images;
self.window.rootViewController = self.firstViewController;
[self.window makeKeyAndVisible];

}

@end

我还是 Objective-C 的新手,只学习了如何在 Storyboard 中创建 ViewControllers 而不是以编程方式。

另外我不知道你可以为特定的视图控制器声明一个属性,而不仅仅是一个通用的UIViewContoller。那是我被绊倒的另一个地方。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-27
    • 2012-03-04
    • 1970-01-01
    • 2023-03-22
    • 2019-06-25
    • 2021-06-30
    • 2012-07-31
    • 1970-01-01
    相关资源
    最近更新 更多