我正在尝试合并这两个项目。
此时我只想将它们保存在单独的视图控制器中,并使Thumbnail Picker的功能在其自己的控制器中工作。
我收到错误,正如标题中所述,“在'UIViewController *'类型的对象上找不到”属性'图像'
错误来自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项目:
答案 0 :(得分:3)
您在.h文件中声明的“images
”属性:
@property (strong, nonatomic) UIImageView *images;
不是您尝试通过以下行分配给它的NSMutableArray:
self.viewController.images = images;
如果你可以得到相同的类型(即单个UIImageView对象或包含许多UIImages的NSMutableArray),那么你应该更好地将事物分配给该属性。
答案 1 :(得分:0)
我解决了。问题是我正在使用Storyboard合并一个项目,并使用以编程方式生成的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
。那是我被绊倒的另一个地方。