私は iOS にかなり慣れていないので、学ぶべきことがたくさんあります。皆さんが私の過ちから私を導いてくれることを願っています。
私は最近、TableView から DetailView にデータを渡すことを学びました。ストップウォッチアプリも作り始めたのですが、ログ機能はとても便利だなと感じました。
ということで、現在、タイマー機能とハイスコアログ機能を備えたストップウォッチアプリを作成中です。ビュー(ストップウォッチ)からテーブルビュー(ログボード)に移動します。アプリの起動/終了時に情報が失われるため、情報を保持する一時ストレージとして NSMutableArray を使用しています。残念ながら、あちこちで変数をたどって変更することで、混乱して立ち往生してしまったようです。
皆さんが提供してくれた提案と助けに感謝し、ヒントをくれた@Abizernに感謝します。すべての問題を解決するために管理します。将来誰かがこれと同様のことを行う場合に備えて、ここにコードを残しておきます。
TimerViewController.h
#import <UIKit/UIKit.h>
#import "SampleData.h"
#import "SampleDataDAO.h"
#import "HighScoreTableViewController.h"
@interface TimerViewController : UIViewController
{
NSTimer *stopWatchTimer; // Store the timer that fires after a certain time
NSDate *startDate; // Stores the date of the click on the start button
}
@property(nonatomic, strong) SampleDataDAO *daoDS;
@property(nonatomic, strong) NSMutableArray *ds;
@property (retain, nonatomic) IBOutlet UILabel *stopWatchLabel;
@property (weak, nonatomic) IBOutlet UIButton *onStartPressed;
@property (weak, nonatomic) IBOutlet UIButton *onStopPressed;
@property (weak, nonatomic) IBOutlet UIButton *onLogPressed;
@property (weak, nonatomic) IBOutlet UIButton *onHighscorePressed;
- (IBAction)onStartPressed:(id)sender;
- (IBAction)onStopPressed:(id)sender;
- (IBAction)onLogPressed:(id)sender;
- (IBAction)onHighscorePressed:(id)sender;
@end
TimerViewController.m
#import "TimerViewController.h"
@interface TimerViewController ()
@end
@implementation TimerViewController
@synthesize stopWatchLabel;
@synthesize onStartPressed;
@synthesize onStopPressed;
@synthesize onLogPressed;
@synthesize onHighscorePressed;
@synthesize ds,daoDS;
- (void)viewDidLoad
{
[super viewDidLoad];
daoDS = [[SampleDataDAO alloc] init];
self.ds = daoDS.PopulateDataSource;
onStopPressed.enabled=false;
}
- (void)viewDidUnload
{
[self setStopWatchLabel:nil];
[self setOnStartPressed:nil];
[self setOnLogPressed:nil];
[self setOnStopPressed:nil];
[self setOnHighscorePressed:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:@"showDetail"]) {
HighScoreTableViewController *detailViewController = [segue destinationViewController];
detailViewController.arrayOfSampleData = self.ds;
}
}
- (void)updateTimer
{
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss.S"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
NSString *timeString=[dateFormatter stringFromDate:timerDate];
stopWatchLabel.text = timeString;
}
- (IBAction)onStartPressed:(id)sender {
startDate = [NSDate date];
// Create the stop watch timer that fires every 10 ms
stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
target:self
selector:@selector(updateTimer)
userInfo:nil
repeats:YES];
onStartPressed.enabled=false;
onStopPressed.enabled=true;
}
- (IBAction)onStopPressed:(id)sender {
[stopWatchTimer invalidate];
stopWatchTimer = nil;
[self updateTimer];
onStartPressed.enabled=true;
}
- (IBAction)onLogPressed:(id)sender {
NSString * timeCaptured = stopWatchLabel.text;
static NSInteger i = 1 ;
SampleData* mydata = [[SampleData alloc]init];
mydata.clueName=[NSString stringWithFormat:@"clue %d",i++ ];
mydata.timeLog = timeCaptured;
[self.ds addObject:mydata];
NSLog(@"%@",mydata.clueName);
NSLog(@"time %@", mydata.timeLog);
NSLog(@"%d",[self.ds count]);
mydata=nil;
}
- (IBAction)onHighscorePressed:(id)sender {
NSLog(@"Proceeding to HighScore");
}
@end
HighScoreTableView.h
#import <UIKit/UIKit.h>
#import "SampleData.h"
#import "SampleDataDAO.h"
#import "TimerViewController.h"
@interface HighScoreTableViewController : UITableViewController
@property (nonatomic, strong) NSMutableArray *arrayOfSampleData;
@property (nonatomic, strong) SampleData * highscoreData;
@end
HighScoreTableView.m
#import "HighScoreTableViewController.h"
@interface HighScoreTableViewController ()
@end
@implementation HighScoreTableViewController
@synthesize highscoreData;
@synthesize arrayOfSampleData;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
highscoreData = [[SampleData alloc]init];
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.arrayOfSampleData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"highscoreCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//highscoreData = [self.arrayOfSampleData objectAtIndex:indexPath.row];
highscoreData = (SampleData *)[self.arrayOfSampleData objectAtIndex:indexPath.row]; //if above line doesn't work, use this
cell.textLabel.text=[NSString stringWithFormat:@"%@ time %@",highscoreData.clueName, highscoreData.timeLog];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
@end
SampleData.h
#import <Foundation/Foundation.h>
@interface SampleData : NSObject
@property(nonatomic,strong) NSString * clueName;
@property(nonatomic,strong) NSString * timeLog;
@end
SampleData.m
#import "SampleData.h"
@implementation SampleData
@synthesize clueName,timeLog;
@end
SampleDataDAO.h
#import <Foundation/Foundation.h>
#import "SampleData.h"
@interface SampleDataDAO : NSObject
@property(nonatomic, strong) NSMutableArray * someDataArray;
-(NSMutableArray *)PopulateDataSource;
@end
SampleDataDAO.m (この DAO NSObject が必要かどうかは不明)
#import "SampleDataDAO.h"
@implementation SampleDataDAO
@synthesize someDataArray;
-(NSMutableArray *)PopulateDataSource
{
someDataArray = [[NSMutableArray alloc] init];
SampleData * mydata = [[SampleData alloc] init];
mydata = nil;
return someDataArray;
}
@end