iphone - 从头开始启动 Xcode 项目并且无法显示任何内容

标签 iphone ios xcode

我正在关注一本书(Big Nerd Ranch IOS 编程),我使用的 xcode 版本比这本书使用的版本稍新。因此,我无法完全遵循书中的步骤,其中步骤之一是创建一个基于窗口的应用程序。

我所做的是:我创建了一个空项目,它为我提供了委托(delegate)和一些其他文件。我创建了自己的 MainWindow.xib。

我继续阅读,我已经到了需要编译的部分,我应该在模拟器上看到一些东西。然而,我什么也没看到。我知道我在这里遗漏了一些东西,因为我猜测我没有使用代码正确连接 MainWindow.xib (??)。请注意,这本书告诉我将 map View 、文本字段和事件指示器添加到主窗口。

我想知道从头开始项目的正确方法是什么。实际上我更喜欢这样做,这样我就可以了解启动 ios 项目的真相。

更新: 将 Info.plist“主 Nib 文件基本名称”设置为“MainWindow”后。我能够看到风景。但是,我无法单击模拟器上的文本字段并使键盘显示。我单击了主页按钮,当我尝试再次单击该图标时,它将显示白色屏幕,而不是带有文本字段的 map 。

WhereamiAppDelegate.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface WhereamiAppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>{
    CLLocationManager * locationManager;
    IBOutlet MKMapView *worldView;
    IBOutlet UIActivityIndicatorView *activityIndicator;
    IBOutlet UITextField *locationTitleField;
}

@property (strong, nonatomic) IBOutlet UIWindow *window;

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;

@end

WhereamiAppDelegate.m

#import "WhereamiAppDelegate.h"

@implementation WhereamiAppDelegate

@synthesize window = _window;
@synthesize managedObjectContext = __managedObjectContext;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    locationManager = [[CLLocationManager alloc] init];

    [locationManager setDelegate:self];
    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [locationManager startUpdatingLocation];

    [worldView setShowsUserLocation:YES];


    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    return YES;
}

- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    NSLog(@"%@", newLocation);
}

- (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSLog(@"Could not find location: %@", error);
}

- (void)applicationWillResignActive:(UIApplication *)application
{

}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    /*
     Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
     */
}

- (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
{
    // Saves changes in the application's managed object context before the application terminates.
    [self saveContext];
}

- (void)saveContext
{
    NSError *error = nil;
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    if (managedObjectContext != nil)
    {
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
        {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        } 
    }
}

#pragma mark - Core Data stack

- (NSManagedObjectContext *)managedObjectContext
{
    if (__managedObjectContext != nil)
    {
        return __managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil)
    {
        __managedObjectContext = [[NSManagedObjectContext alloc] init];
        [__managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return __managedObjectContext;
}

- (NSManagedObjectModel *)managedObjectModel
{
    if (__managedObjectModel != nil)
    {
        return __managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"whereami" withExtension:@"momd"];
    __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return __managedObjectModel;
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (__persistentStoreCoordinator != nil)
    {
        return __persistentStoreCoordinator;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"whereami.sqlite"];

    NSError *error = nil;
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
    {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }    

    return __persistentStoreCoordinator;
}

#pragma mark - Application's Documents directory

- (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

@end

ma​​in.m

#import <UIKit/UIKit.h>

#import "WhereamiAppDelegate.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([WhereamiAppDelegate class]));
    }
}

info.plist

MainWindow.xib

最佳答案

参见here (引自@WrightCS):

MainWindow.xib is the defined in your info.plist as the Main nib file base name. In your MainWindow.xib, you define the first controller that you want to load, in your case, RootViewController.

关于iphone - 从头开始启动 Xcode 项目并且无法显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8014558/

相关文章:

iphone - 如何让 UIWebView 加载网页,但 URL 在 UILabel 上?

ios - UICollectionViewCell 带有图像但没有自定义类

ios - 从 Xcode Master-Detail 项目中的 Detail View Controller 访问 Master View Controller

iphone - Xcode 配置文件问题

ios - iframe 比 iPhone 上应有的宽度宽

iOS 12.0 : Is there a way to set MFMailComposeViewController navigation bar title's text to white?

ios - 当我的角色设置为 App Manager 时如何创建配置文件

objective-c - 如何更改标签上的日期选择器选定值

ios - 在 Swift 4 的左/右 UITextField 中添加图标或图像

iphone - Objective-C 中的单例共享数据源