我努力學習IOS編碼,並試圖使foodDiary應用..我用空的項目文件,然後添加的情節提要視圖控制器和表視圖控制器基本的iPhone應用程序,並突破點錯誤
當我運行我的程序時,tableView菜單顯示其中的元素,然後當我按+按鈕視圖控制器顯示其中有一個文本框和一個按鈕裏面..當我輸入的東西,然後按下我的程序壓碎,我得到斷點錯誤, 我得到錯誤的位置:
- (IBAction)addFoodButton:(id)sender {
NSString *newFood = [addFoodText text];
[foodTableViewController addFood:newFood];
[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];
}
我的部分編碼爲:
DAYfoodTableViewController.h
#import <UIKit/UIKit.h>
@interface DAYfoodTableViewController:的UITableViewController
@屬性(非原子,強)的NSMutableArray * foodArray;
- (void)addFood:(NSString *)newFood;
@end
DAYfoodTableViewController.m
#import "DAYfoodTableViewController.h"
#import "DAYaddFoodViewController.h"
@interface DAYfoodTableViewController()
@end
@implementation DAYfoodTableViewController
@synthesize foodArray;
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
DAYaddFoodViewController *addFoodViewController = [segue destinationViewController];
[addFoodViewController setFoodTableViewController:self];
}
-(void) addFood:(NSString *)newFood
{
[foodArray addObject:newFood];
[[self tableView] reloadData];
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
foodArray=[[NSMutableArray alloc] initWithObjects:@"Pizza",@"Chips",@"Sandwiches",@"Hot Dogs", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [foodArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"BasicCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
int rowNumber=[indexPath row];
NSString *food=[foodArray objectAtIndex:rowNumber];
[[cell textLabel] setText:food];
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[foodArray removeObjectAtIndex:[indexPath row] ];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade ];
/*
or (by asim)
if (editingStyle == UITableViewCellEditingStyleDelete) {
[foodArray removeObjectAtIndex:[indexPath row] ];
[[self tableView] reloadData];
}
*/
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#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
DAYaddFoodTableViewController.h
#import <UIKit/UIKit.h>
#import "DAYfoodTableViewController.h"
@interface DAYaddFoodViewController : UIViewController
@property (nonatomic, weak) DAYfoodTableViewController *foodTableViewController;
- (IBAction)addFoodButton:(id)sender;
@property (weak, nonatomic) IBOutlet UITextField *addFoodText;
@end
DAYaddFoodTableView.m
#import "DAYaddFoodViewController.h"
@interface DAYaddFoodViewController()
@end
@implementation DAYaddFoodViewController
@synthesize foodTableViewController;
@synthesize addFoodText;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)addFoodButton:(id)sender {
NSString *newFood = [addFoodText text];
[foodTableViewController addFood:newFood];
[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];
}
@end
奇怪的是,當程序崩潰時,我通過模擬器重新運行程序,它完美地工作.. 什麼可能會導致此問題?謝謝