I am following an Apple.developer.com tutorial. The goal is to create an easy to-do list app in iOS. Everything works fine, until I hit the doneButton to add an item. Than my mac switches back to xcode from the simulator, and I cannot really understand the error. Here`s my app: http://screencast.com/t/y0vJ1LboF9Wv As you can see, everything is works great, until I hit the doneButton WITH text in the textfield. I think the error is in the
XYZAddToDoItemViewController.m:
#import "XYZAddToDoItemViewController.h"
@interface XYZAddToDoItemViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *doneButton;
@end
@implementation XYZAddToDoItemViewController
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if (sender != self.doneButton) return;
if (self.textField.text.length > 0) {
self.toDoItem = [[XYZToDoItem alloc] init];
self.toDoItem.itemName = self.textField.text;
self.toDoItem.completed = NO;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)unwindToList:(UIStoryboardSegue *)segue
{
XYZAddToDoItemViewController *source = [segue sourceViewController];
XYZToDoItem *item = source.toDoItem;
if (item != nil) {
[self.toDoItems addObject:item];
[self.tableView reloadData];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#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 [self.toDoItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"ListPrototypeCell" forIndexPath:indexPath];
XYZToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
cell.textLabel.text = toDoItem.itemName;
if (toDoItem.completed) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
# pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
XYZToDoItem *tappedItem = [self.toDoItems objectAtIndex:indexPath.row];
tappedItem.completed = !tappedItem.completed;
[tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationNone];
}
@end
It has something to do with the prepareForSegue methode. The explaining of the error is:
2014-11-24 16:43:01.745 TooDooList[8174:138535] -[XYZAddToDoItemViewController textField:]: unrecognized selector sent to instance 0x7faf7a5d6660
2014-11-24 16:43:01.759 TooDooList[8174:138535] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[XYZAddToDoItemViewController textField:]: unrecognized selector sent to instance 0x7faf7a5d6660'
*** First throw call stack:
(
0 CoreFoundation 0x0000000102d47f35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001029e0bb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000102d4f04d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x0000000102ca727c ___forwarding___ + 988
4 CoreFoundation 0x0000000102ca6e18 _CF_forwarding_prep_0 + 120
5 UIKit 0x00000001031358be -[UIApplication sendAction:to:from:forEvent:] + 75
6 UIKit 0x000000010323c410 -[UIControl _sendActionsForEvents:withEvent:] + 467
7 UIKit 0x00000001038684cb -[UITextField _resignFirstResponder] + 256
8 UIKit 0x00000001032ab47b -[UIResponder resignFirstResponder] + 114
9 UIKit 0x00000001038682d4 -[UITextField resignFirstResponder] + 114
10 UIKit 0x000000010319ef9f -[UIView(Hierarchy) _removeFirstResponderFromSubtree] + 161
11 UIKit 0x000000010319f45a __UIViewWillBeRemovedFromSuperview + 76
12 UIKit 0x000000010319f1c9 -[UIView(Hierarchy) removeFromSuperview] + 91
13 UIKit 0x0000000103239599 __71-[UIPresentationController _initViewHierarchyForPresentationSuperview:]_block_invoke485 + 222
14 UIKit 0x0000000103235b75 -[UIPresentationController transitionDidFinish:] + 87
15 UIKit 0x00000001037fb298 -[_UIFullscreenPresentationController transitionDidFinish:] + 71
16 UIKit 0x0000000103238301 __56-[UIPresentationController runTransitionForCurrentState]_block_invoke_2 + 133
17 UIKit 0x000000010388aac3 -[_UIViewControllerTransitionContext completeTransition:] + 110
18 UIKit 0x000000010323326f -[UITransitionView _didCompleteTransition:] + 1120
19 UIKit 0x000000010318de6a -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 209
20 UIKit 0x000000010318e1a0 -[UIViewAnimationState animationDidStop:finished:] + 76
21 QuartzCore 0x0000000106a1e7ee _ZN2CA5Layer23run_animation_callbacksEPv + 308
22 libdispatch.dylib 0x00000001052a27f4 _dispatch_client_callout + 8
23 libdispatch.dylib 0x000000010528b8fb _dispatch_main_queue_callback_4CF + 949
24 CoreFoundation 0x0000000102caffe9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
25 CoreFoundation 0x0000000102c72eeb __CFRunLoopRun + 2043
26 CoreFoundation 0x0000000102c72486 CFRunLoopRunSpecific + 470
27 GraphicsServices 0x00000001063169f0 GSEventRunModal + 161
28 UIKit 0x0000000103134420 UIApplicationMain + 1282
29 TooDooList 0x00000001024b1383 main + 115
30 libdyld.dylib 0x00000001052d7145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
I have try to find a solution trough both googling it and read tutorials. If someone could help, I would be so glad. I am a newbie, so it could be a silly mistake. Thanks a lot! To help you out, here are some of my other codes:
XYZToDoListTableViewController.h:
#import <UIKit/UIKit.h>
#import "XYZToDoItem.h"
@interface XYZToDoListTableViewController : UITableViewController
- (IBAction)unwindToList:(UIStoryboardSegue *)segue;
@property XYZToDoItem *toDoItem;
@end
XYZAddToDoItemViewController.h:
#import <UIKit/UIKit.h>
#import "XYZToDoItem.h"
@interface XYZAddToDoItemViewController : UIViewController
@property XYZToDoItem *toDoItem;
@end
XYZToDoListTableViewController.m:
#import "XYZToDoListTableViewController.h"
#import "XYZToDoItem.h"
#import "XYZAddToDoItemViewController.h"
@interface XYZToDoListTableViewController ()
@property NSMutableArray *toDoItems;
@end
@implementation XYZToDoListTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.toDoItems = [[NSMutableArray alloc] init];
[self loadInitialData];
}
- (void)loadInitialData {
XYZToDoItem *item1 = [[XYZToDoItem alloc] init];
item1.itemName = @"Buy butter";
[self.toDoItems addObject:item1];
XYZToDoItem *item2 = [[XYZToDoItem alloc] init];
item2.itemName = @"Bake Millie a cake";
[self.toDoItems addObject:item2];
XYZToDoItem *item3 = [[XYZToDoItem alloc] init];
item3.itemName = @"Order pizza";
[self.toDoItems addObject:item3];
}
- (IBAction)unwindToList:(UIStoryboardSegue *)segue;
{
XYZAddToDoItemViewController *source = [segue sourceViewController];
XYZToDoItem *item = source.toDoItem;
if (item != nil) {
[self.toDoItems addObject:item];
[self.tableView reloadData];
}
}
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#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 [self.toDoItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"ListPrototypeCell" forIndexPath:indexPath];
XYZToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
cell.textLabel.text = toDoItem.itemName;
if (toDoItem.completed) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
# pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
XYZToDoItem *tappedItem = [self.toDoItems objectAtIndex:indexPath.row];
tappedItem.completed = !tappedItem.completed;
[tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationNone];
}
@end
If you need more code, let me know. Thanks.
Aucun commentaire:
Enregistrer un commentaire