gpt4 book ai didi

uitableview - NSDate 使我的应用程序崩溃,我不知道为什么

转载 作者:行者123 更新时间:2023-12-03 17:40:24 25 4
gpt4 key购买 nike

基本情况如下:

我需要用 1 个 UIDatePicker 控制 2 次(NSDate)。我在 XCode (v 4.4.1) 中创建了一个新的 Single View 项目,只是为了开始摆弄我需要的项目。我在创建项目时使用了 Storyboard,很容易。在 Storyboard 中,我摆脱了 View Controller 并拖出了一个导航 Controller 。删除了作为 Root View Controller 的 TableView ,并添加了一个常规 View Controller 作为 Root View Controller 。在那里我放置了一个按钮。我拖出另一个 View Controller 并使用“推送”将 Root View Controller 上的那个按钮连接到这个新的 View Controller 。非常简单。我测试了它,它有效,基本。

我为刚刚添加的新 View Controller 创建了一个类,并将 Storyboard 中的 View Controller 设置为该类型。然后,在 Storyboard 中,我添加了一个 UIDatePicker、一个 UIButton 和一个 UITableView(请记住,这只是我在尝试让事情正常工作并尝试学习如何使用所有东西,对此仍然很陌生)。

我的 ViewController 类的代码如下:

新 View Controller .h

#import <UIKit/UIKit.h>

@interface Reminders : UIViewController <UITableViewDataSource, UITableViewDelegate> {
NSInteger selectedRow;
}

@property (nonatomic, retain) NSDate *amTime;
@property (nonatomic, retain) NSDate *pmTime;

@property (nonatomic, retain) IBOutlet UITableView *tbl;
@property (nonatomic, retain) IBOutlet UIDatePicker *picker;

- (IBAction)timeChange:(id)sender;
- (IBAction)pickerChange:(id)sender;

- (NSString *)dateAsString: (NSDate *)date;

@end

NewViewController.m
#import "Reminders.h"

@interface Reminders ()

@end

@implementation Reminders

@synthesize tbl, picker;
@synthesize amTime, pmTime;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
NSLog(@"init");

}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(@"did load");

amTime = [NSDate date];
NSLog(@"AM time viewDidLoad: %@", [self dateAsString:amTime]);

pmTime = [NSDate date];
NSLog(@"PM time viewDidLoad: %@", [self dateAsString:pmTime]);

selectedRow = (NSInteger)1;
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}





-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}


-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
return 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//NSLog(@"cell for row");
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"MyIdentifier"];
}

if (indexPath.row == 0) {
cell.textLabel.text = @"Dose";
cell.detailTextLabel.text = @"120";
}
else if (indexPath.row == 1) {
cell.textLabel.text = @"AM";
//cell.detailTextLabel.text = @"time am";

cell.detailTextLabel.text = [self dateAsString:amTime];

}
else if (indexPath.row == 2) {
cell.textLabel.text = @"PM";
//cell.detailTextLabel.text = @"time pm";

cell.detailTextLabel.text = [self dateAsString:pmTime];
}

if (selectedRow == indexPath.row) {
[tbl selectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0] animated:NO scrollPosition:0];
}

return cell;
}


-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
selectedRow = 0;
}
else if (indexPath.row == 1) {
selectedRow = 1;
[picker setDate:amTime];
}
else if (indexPath.row == 2) {
selectedRow = 2;
[picker setDate:pmTime];
}
}


- (IBAction)timeChange:(id)sender {
if (selectedRow == 1) {
amTime = [NSDate date];
NSLog(@"time change: %@", [self dateAsString:amTime]);
}
else if (selectedRow == 2) {
pmTime = [NSDate date];
NSLog(@"time change: %@", [self dateAsString:pmTime]);
}
else {

}

[tbl reloadData];
}

- (IBAction)pickerChange:(id)sender {
if (selectedRow == 1) {
amTime = [picker date];
NSLog(@"PICKER time change: %@", [self dateAsString:amTime]);
}
else if (selectedRow == 2) {
pmTime = [picker date];
NSLog(@"PICKER time change: %@", [self dateAsString:pmTime]);
}
else {
NSLog(@"picker does NOTHING");
}

[tbl reloadData];
}

- (NSString *)dateAsString:(NSDate *)date {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSString *stringFromDate = [formatter stringFromDate:date];

return stringFromDate;

}

是的,我完全意识到按钮和日期选择器的操作都在改变时间以及按钮和相应操作存在的原因是因为当我无法让它按我想的方式工作时我开始添加东西它应该一直在工作。

反正。时间似乎开始得很好,但是从我通过检查可以看到的情况来看,有些点是无效的objective-c对象。当它试图将“amTime”放入单元格详细信息文本时,它会阻塞 cellForRowAtIndexPath 方法。

这是踢球者......我创建了相同的应用程序,使用我上面提到的相同步骤,相同的代码(复制粘贴),在另一台机器(具有正确的代码签名 key 的机器)上,并且该版本正常工作,即使我在我当前的机器上运行该项目。当我在当前机器上创建项目时,它崩溃了。

我为这篇文章的长度道歉,但我想尽量提供尽可能多的细节。

最佳答案

看来您的变量被释放得太早了。我会保存 amTimepmTime在实例变量中,并通过 self.amTime 在我的代码中访问它们和 self.pmTime :因为它们是属性,getters/setters 方法将适本地保留/释放。

raywenderlich.com 上有一个精彩的三部分教程,解释了 iOS 开发中内存管理的基础知识:必读。

http://www.raywenderlich.com/2657/memory-management-in-objective-c-tutorial

关于uitableview - NSDate 使我的应用程序崩溃,我不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11945719/

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