gpt4 book ai didi

ios - 将从核心数据中获取的数据分配给 UIPickerView

转载 作者:行者123 更新时间:2023-11-28 22:29:39 25 4
gpt4 key购买 nike

我搜索了所有堆栈溢出,但没有真正帮助我解决问题的答案,尽管这看起来很简单。

我有一个应用程序,其中包含一个存储字符串的实体。我可以很好地存储它们并在 UITableView 中获取它们,但在某些时候我需要获取它们并将它们放入 UIPickerView 中。这是我的类(class),其中有 UIPickerView。因为它是在模态视图中,所以有一个委托(delegate)可以带回父 View 中的数据,但这工作正常。头文件:

#import <UIKit/UIKit.h>


@protocol TextChoiceDelegate <NSObject>
- (void) takeBackViewController:(id)controller didFinishSelectingText:(NSString *)text;
@end

@interface ChooseTextViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource> {
UIPickerView *pickerView;
NSMutableArray *list;
}

@property (nonatomic, assign) id <TextChoiceDelegate> delegate;
@property (strong, nonatomic) IBOutlet UIPickerView *pickerView;
@property (strong, nonatomic) NSString *text;

- (IBAction)didFinishChoosingText:(id)sender;

@end

和 .m 文件:

#import "ChooseTextViewController.h"

@interface ChooseTextViewController ()
@property (strong) NSMutableArray *list;
@end


@implementation ChooseTextViewController

@synthesize delegate;

@synthesize pickerView;
@synthesize list;
@synthesize text;


#pragma mark - IBActions

- (IBAction)didFinishChoosingText:(id)sender
{
NSString *textToPassBack = text;
NSLog(@"returning: %@", textToPassBack);
[self.delegate takeBackViewController:self didFinishSelectingText:textToPassBack];

[self dismissViewControllerAnimated:YES completion:NULL];
}

#pragma mark - UIPickerViewDataSource methods

// Number of components in the pickerView
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}

// Number of elements in the pickerView component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
return [self.list count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [self.list objectAtIndex:row];
}


#pragma mark - PickeView delegate methods

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(@"Selection of element: %@", [self.list objectAtIndex:row]);
text = [self.list objectAtIndex:row];
}


#pragma mark - View Controller LifeCycle

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
/*
list = [[NSMutableArray alloc] init];
[list addObject:@"1"];
[list addObject:@"2"];
[list addObject:@"3"];
[list addObject:@"4"];
[list addObject:@"5"];
*/
}

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.list = [[NSMutableArray alloc] init];

NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"LolText"];

self.list = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
}


#pragma mark - Core Data Stack Methods

- (NSManagedObjectContext *)managedObjectContext
{
NSManagedObjectContext *context = nil;
id localDelegate = [[UIApplication sharedApplication] delegate];
if ([localDelegate performSelector:@selector(managedObjectContext)]) {
context = [localDelegate managedObjectContext];
}
return context;
}

@end

当我运行它时,pickerView 显示为空,当我单击它时应用程序崩溃并显示以下错误日志:2013-07-23 10:51:55.010 MemeGen[51428:c07] * -[UITableViewRowData rectForRow:inSection:] 断言失败,/SourceCache/UIKit_Sim/UIKit-2380.17/UITableViewRowData.m:16302013-07-23 10:51:55.012 MemeGen[51428:c07] 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“在无效索引路径处请求 rect ( 2 个索引 [0, 0])'** 首先抛出调用栈:(0x22bb012 0x16e5e7e 0x22bae78 0xe6b665 0x502f20 0x3c62de 0x756086 0x755f7a 0x37940d 0x37b9eb 0x5e485a 0x5e399b 0x5e50df 0x5e7 d2d 0x5e7cac 0x5dfa28 0x34c972 0x34ce53 0x32ad4a 0x31c698 0x264fdf9 0x264fad0 0x2230bf5 0x2230962 0x2261bb6 0x2260f44 0x2260e1b 0 x264e7e3 0x264e668 0x319ffc 0x27ad 0x26d5)libc++abi.dylib:终止调用抛出异常

出于测试目的,当我运行 viewDidLoad 方法的注释部分(注释所有关于 cora 数据)时,它工作正常。如果我将 viewDidAppear 中的代码放在 viewDidLoad 中,则一旦 viewController 出现在屏幕上就会崩溃

我觉得我离真相不远了,但我找不到出路。有人可以帮我吗?

最佳答案

这是因为您的 Pickerview 没有行,这就是系统崩溃的原因。

您可以检查您的列表是否提供数据?如果是那么应该没有问题。

检查是否没有数据则不要打开pickerview。

关于ios - 将从核心数据中获取的数据分配给 UIPickerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17806154/

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