自学iOS编程,并从this book开始。 我遇到错误“Property'MainViewController'找不到'AppDelegate *'类型的对象。
我已经检查过双倍和三倍,我正确地遵循了代码,甚至从头开始重新启动。我已经搜索了StackOverflow并尝试了一些解决方案,但没有一个工作,很少有正确匹配我的问题。有什么帮助吗?
AppDelegate.m(错误所在)
#import "AppDelegate.h"
#import "WeatherForecast.h"
#import "MainViewController.h"
@implementation AppDelegate
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
WeatherForecast *forecast = [[WeatherForecast alloc] init];
self.MainViewController.forecast = forecast;
// Override point for customization after application launch.
MainViewController *controller = (MainViewController *)self.window.rootViewController;
controller.managedObjectContext = self.managedObjectContext;
return YES;
}
MainViewController.h
#import "FlipsideViewController.h"
#import "WeatherForecast.h"
#import <CoreData/CoreData.h>
@interface MainViewController : UIViewController <FlipsideViewControllerDelegate>
- (IBAction)showInfo;
- (IBAction)refreshView:(id) sender;
- (void)updateView;
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (strong, nonatomic) WeatherForecast *forecast;
@end
答案 0 :(得分:1)
问题应该出在application:didFinishLaunchingWithOptions
的第二行。 self.MainViewController
期待AppDelegate中的属性。只需删除此行并在controller.forecast = forecast;
之前添加return YES.
此时您将获得对MainViewController的引用并可以安全地设置属性(假设MainViewController通过Storyboard或XIB设置为当前的rootViewController)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
WeatherForecast *forecast = [[WeatherForecast alloc] init];
// Override point for customization after application launch.
MainViewController *controller = (MainViewController *)self.window.rootViewController;
controller.managedObjectContext = self.managedObjectContext;
controller.forecast = forecast;
return YES;
}