gpt4 book ai didi

iphone - 选项卡栏不显示任何项目

转载 作者:行者123 更新时间:2023-11-29 04:40:08 24 4
gpt4 key购买 nike

我将 CoreData 添加到了我的选项卡栏应用程序中,并且 CoreData 功能正在运行(即我可以将信息从它输出到控制台)。问题是我的标签栏不起作用。当我运行我的应用程序时,它看起来像这样:

enter image description here

看起来标签栏本身已经显示,但没有任何项目,并且没有显示表格 View 。我的 Storyboard如下所示:

enter image description here

这是我的 AppDelegate 的代码:

AppDelegate.h

#import <UIKit/UIKit.h>

#import "JobsViewController.h"
#import "Job.h"
#import "Shift.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) 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

AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

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

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSManagedObjectContext *context = [self managedObjectContext];
Job *job = [NSEntityDescription insertNewObjectForEntityForName:@"Job" inManagedObjectContext:context];
job.employer = @"Calder Centre";
job.jobTitle = @"Addictions Counsellor";
job.regularRate = 25.9f;
job.overtimeRate = 30.5f;
job.deduction1 = 0.1f;
job.deduction2 = 0.2f;
job.deduction3 = 0.3f;
job.deduction4 = 0.4f;
job.deduction1Name = @"CPP";
job.deduction2Name = @"IT";
job.deduction3Name = @"Union Dues";
job.deduction4Name = @"Other";

Shift *shift = [NSEntityDescription insertNewObjectForEntityForName:@"Shift" inManagedObjectContext:context];
shift.startDate = [NSDate date];
shift.endDate = [NSDate date];

NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}

// Test listing all FailedBankInfos from the store
NSFetchRequest *jobFetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *jobEntity = [NSEntityDescription entityForName:@"Job" inManagedObjectContext:context];
[jobFetchRequest setEntity:jobEntity];
NSArray *fetchedJobs = [context executeFetchRequest:jobFetchRequest error:&error];
for (Job *job in fetchedJobs) {
NSLog(@"Employer: %@", job.employer);
NSLog(@"Title: %@", job.jobTitle);
}

NSFetchRequest *shiftFetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *shiftEntity = [NSEntityDescription entityForName:@"Shift" inManagedObjectContext:context];
[shiftFetchRequest setEntity:shiftEntity];
NSArray *fetchedShifts = [context executeFetchRequest:shiftFetchRequest error:&error];
for (Shift *shift in fetchedShifts) {
NSLog(@"Start Date: %@", shift.startDate);
NSLog(@"End Date: %@", shift.endDate);
}

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.

UITabBarController *tabBarController = [[UITabBarController alloc]init];
self.window.rootViewController = tabBarController;

self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

return YES;
}

这是 View Controller :

JobsViewController.h

#import <UIKit/UIKit.h>
#import "Job.h"
#import "Shift.h"

@interface JobsViewController : UITableViewController

@property (nonatomic, strong) NSManagedObjectContext* managedObjectContext;
@property (nonatomic, strong) NSArray *listOfJobs;

@end

JobsViewController.m

#import "JobsViewController.h"

@interface JobsViewController ()

@end

@implementation JobsViewController

@synthesize managedObjectContext, listOfJobs;

- (void)viewDidLoad
{
[super viewDidLoad];

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.rightBarButtonItem = self.editButtonItem;

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Job" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSError *error;
self.listOfJobs = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
self.title = @"Jobs";
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [listOfJobs count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"job";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

// Configure the cell...
Job *job = [listOfJobs objectAtIndex:indexPath.row];
cell.textLabel.text = job.jobTitle;
cell.detailTextLabel.text = job.employer;

return cell;
}

最佳答案

这与核心数据无关。

您正在将应用程序委托(delegate)模板中的代码用于 Non-Storyboard 项目,但您正在使用 Storyboard。 didFinishLaunching 方法中的代码正在创建一个新窗口和空选项卡栏 Controller ,该 Controller 将覆盖 Storyboard中的任何内容。

删除self.window =之后的所有内容,除了return YES;。这是基于 xib 的应用程序中使用的代码,如果您有 Storyboard,则不需要。

关于iphone - 选项卡栏不显示任何项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10510132/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com