[__NSCFNumber length]: unrecognized selector sent to instance - iphone

I am trying to pass data from a table view controller to a detail view controller. Every entry of my table works just as it should, except for one.
Here is the prepareForSegue method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:#"showDetails"]) {
NSIndexPath *indexPath = [[self tableView] indexPathForSelectedRow];
NSDictionary *notification = [[self notifications] objectAtIndex:[indexPath row]];
NRDetailViewController *destViewController = [segue destinationViewController];
[destViewController setDate:[notification objectForKey:#"date"]];
[destViewController setFrom:[notification objectForKey:#"from"]];
[destViewController setIden:[notification objectForKey:#"identifier"]];
[destViewController setPriority:[notification objectForKey:#"priority"]];
[destViewController setSubject:[notification objectForKey:#"subject"]];
[destViewController setMessage:[notification objectForKey:#"text"]];
}
}
Here is my interface for the detail view:
#import <UIKit/UIKit.h>
#interface NRDetailViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *dateField;
#property (weak, nonatomic) IBOutlet UITextField *fromField;
#property (weak, nonatomic) IBOutlet UITextField *idenField;
#property (weak, nonatomic) IBOutlet UITextField *prioField;
#property (weak, nonatomic) IBOutlet UITextField *subjectField;
#property (weak, nonatomic) IBOutlet UITextView *messageView;
#property (copy, nonatomic) NSString *date;
#property (copy, nonatomic) NSString *from;
#property (copy, nonatomic) NSString *iden;
#property (copy, nonatomic) NSString *priority;
#property (copy, nonatomic) NSString *subject;
#property (copy, nonatomic) NSString *message;
#end
Here is the detail view's implementation file:
#import "NRDetailViewController.h"
#interface NRDetailViewController ()
#end
#implementation NRDetailViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[self dateField] setText:[self date]];
[[self fromField] setText:[self from]];
[[self idenField] setText:[self iden]];
[[self prioField] setText:[self priority]];
[[self subjectField] setText:[self subject]];
[[self messageView] setText:[self message]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I have problem setting the text of idenField to the text of iden. I have added an exception breakpoint, and got that the exception indeed occurs at this line:
[[self idenField] setText:[self iden]];
At this point, I have printed the value of [self iden] and it even contains the content I want to pass, so I have absolutely no idea what the problem is, since as I said, all the other fields are working as they should.
The exception being thrown is:
2013-08-07 22:28:20.539 NotificationReader[1214:11303] -[__NSCFNumber length]: unrecognized selector sent to instance 0x7587a00
Any help would be appreciated greatly.

It looks like:
[destViewController setIden:[notification objectForKey:#"identifier"]];
is returning an NSNumber and not an NSString. You could try replacing that line with:
[destViewController setIden:[(NSNumber*)[notification objectForKey:#"identifier"] stringValue]];

Somewhere you are passing an NSNumber instead of a NSString.

To set the text, iden needs to be a NSString. When you are getting the object from the NSDictionary, it is a NSNumber.
Try this:
[destViewController setIden:(NSString *)[notification objectForKey:#"identifier"]];
or change the iden property to be a NSNumber and then
[[self idenField] setText:[NSString stringWithFormat:#"%d", [self iden]]];

Related

string set to null when pushing detail view

I am pushing a detail view from the master of a Master-Detail Application template.
I want to load a single view with different information depending on which row was clicked. I plan to load the data from a plist file.
Here is the code I used in the MasterViewController, when I added didSelectRowAtIndexPath:
DetailViewController *dvc = [[DetailViewController alloc]init];
dvc.rowItem = [_objects objectAtIndex:indexPath.row];;
NSLog(#"row is %i", indexPath.row);
NSLog(#"master %#", dvc.rowItem);
where rowItem is an NSString belonging to the DetailViewController. Inside the viewDidLoad of the DetailViewController I put:
[super viewDidLoad];
NSLog(#" thingy %#", rowItem);
[self setup:rowItem];
and setup looks like this:
-(void) setup: (NSString *)eval {
filePath = [NSString stringWithFormat:#"%#.plist",eval];
NSLog(#"%#", filePath);
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
NSString *subName;
NSString *yesNo;
NSString *theseChanges;
subName = [plistDict objectForKey:#"subContractAddress"];
yesNo = [plistDict objectForKey:#"yesOrno"];
theseChanges = [plistDict objectForKey:#"Changes"];
}
Here is my interface:
#import <UIKit/UIKit.h>
#interface DetailViewController : UIViewController
#property (strong, nonatomic) id detailItem;
#property (strong, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
#property (strong, nonatomic) IBOutlet UIButton *finishedButton;
#property (strong, nonatomic) IBOutlet UITextView *changes;
#property (strong, nonatomic) IBOutlet UIButton *saveButton;
#property (strong, nonatomic) IBOutlet UISwitch *test;
#property (strong, nonatomic) IBOutlet UILabel *finishedLabel;
#property (strong, nonatomic) IBOutlet UITextField *subContractName;
#property BOOL needsChanges;
#property (strong, nonatomic) NSString *rowItem;
-(IBAction)change:(id)sender;
-(IBAction)save:(id)sender;
#end
when I build and run I get the console output of:
thingy (null)
(null).plist
row is 0
master Excavations
when I press the first row, and
thingy (null)
(null).plist
row is 1
master Flooring
so somehow, rowItem is getting set to null in the crossover.
Can someone see what I am doing wrong here?
Try changing your two log statements as follows:
NSLog(#"master %#, detail %#", dvc.rowItem, dvc);
NSLog(#" thingy %# in detail %#", rowItem, self);
I suspect that the detail you create as dvc is not the same object that is in the view controller hierarchy. Showing the object addresses should verify whether that's the answer.
Do your setup in "viewWillAppear" and I'll bet it starts working.
Please check with this,
dvc.rowItem = [[NString alloc]initWithString:#"%#",[_objects objectAtIndex:indexPath.row]];

NSInvalidArgumentException error [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am trying to pass an object from one view controller to another but it gives an error which is
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MapViewController setTMode:]: unrecognized selector sent to instance 0x866ed70'
*** First throw call stack:
(0x250b022 0x2709cd6 0x250ccbd 0x2471ed0 0x2471cb2 0x28044 0x250ce99 0x155c14e 0x155c0e6 0x1602ade 0x1602fa7 0x1602266 0x15813c0 0x15815e6 0x1567dc4 0x155b634 0x2c0bef5 0x24df195 0x2443ff2 0x24428da 0x2441d84 0x2441c9b 0x2c0a7d8 0x2c0a88a 0x1559626 0x261d 0x2595)
terminate called throwing an exception
my method for the .h file is
#interface TransportViewController : UIViewController<UITextFieldDelegate>
{
MapViewController *modeData;
MapViewController *walkDistData;
MapViewController *routeOptData;
IBOutlet UITextField *tbMode;
IBOutlet UITextField *tbWalkDist;
IBOutlet UITextField *tbRouteOpt;
IBOutlet UIPickerView *pickerView;
NSMutableArray *modeArray;
UITextField *activeField;
IBOutlet UIButton *back;
}
#property (retain, nonatomic) MapViewController *modeData;
#property (retain, nonatomic) MapViewController *walkDistData;
#property (retain, nonatomic) MapViewController *routeOptData;
#property (retain, nonatomic) IBOutlet UITextField *tbMode;
#property (retain, nonatomic) IBOutlet UITextField *tbWalkDist;
#property (retain, nonatomic) IBOutlet UITextField *tbRouteOpt;
#property (retain, nonatomic) IBOutlet UIButton *back;
- (IBAction) backAction:(id)sender;
#end
and this is the method i have for my .m file
- (void)viewDidLoad
{
[super viewDidLoad];
tbMode.delegate = self;
tbWalkDist.delegate = self;
tbRouteOpt.delegate = self;
}
- (IBAction) backAction:(id)sender
{
MapViewController *view=[[MapViewController alloc] initWithNibName:nil bundle:nil];
self.modeData = view;
self.walkDistData = view;
self.routeOptData = view;
// NSLog(#"%#", modeData); // this is where i test all the NSLog at
modeData.tMode = tbMode.text;
NSString *str = [tbWalkDist text];
walkDistData.tWalkDist = [str intValue];
routeOptData.tRouteOpt = tbRouteOpt.text;
[self presentModalViewController:view animated:NO];
}
the error is caught in these lines
modeData.tMode = tbMode.text;
NSString *str = [tbWalkDist text];
walkDistData.tWalkDist = [str intValue];
routeOptData.tRouteOpt = tbRouteOpt.text;
I put a NSLog(#"%#", modeData); to check the values, it returns <MapViewController: 0x84627a0>
and NSLog(#"%#", tbMode.text); it return me the values A which is what I need.
*the error is with modeData.tMode & walkDistData.tWalkDist & routeOptData.tRouteOpt
What did I do wrong ? Please help . I can't seem to figure this one out
My second view controller .h
#interface MapViewController : UIViewController
{
NSString *tMode;
NSString *tRouteOpt;
int tWalkDist;
}
#property(nonatomic, retain)NSString *tMode;
#property(nonatomic, retain)NSString *tRouteOpt;
#property(nonatomic)int tWalkDist;
You need to synthesize the tMode property in MapViewController.m:
#implementation MapViewController
#synthesize tMode;
...

numberOfRowsInSection not being called for UITableView

I am having some issues where my UITableView is not reloading, I have redone the linking of the outlet to make sure that was not the issue. I have also tried using [self table] reloadData] but that does not seem to work either. I have debugged the issues, but it simply skips reloadData and the app keeps running like the code is not even there.
Top part of my .m file, nothing is done in ViewDidLoad
#import "SettingsCourses.h"
#implementation SettingsCourses
#synthesize settingsCourses;
#synthesize Delegate;
#synthesize BackButton;
#synthesize DeleteButton;
#synthesize table;
#synthesize courses;
#synthesize dataHandler;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
dataHandler = [[DataHandler alloc] init];
dataHandler.coursesDelegate = self;
courses = [dataHandler fetchCourses];
NSLog(#"Debug Count: %i", [courses count]);
}
[table reloadData];
return self;
}
- (void) viewWillAppear:(BOOL)animated {
[table reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (courses) {
NSLog(#"Count: %i", [courses count]);
return [courses count];
}
else {
NSLog(#"Count: 0");
return 0;
}
}
My .h file
#import <UIKit/UIKit.h>
#import "dataHandler.h"
#interface SettingsCourses : UIViewController
#property (strong, nonatomic) DataHandler *dataHandler;
#property (strong, nonatomic) NSMutableArray *courses;
#property (strong, nonatomic) IBOutlet UIView *settingsCourses;
#property (nonatomic, strong) id Delegate;
#property (weak, nonatomic) IBOutlet UIButton *BackButton;
#property (weak, nonatomic) IBOutlet UIButton *DeleteButton;
#property (weak, nonatomic) IBOutlet UITableView *table;
- (IBAction)Delete:(id)sender;
- (IBAction)Back:(id)sender;
Thanks for any help!
add this lines:
- (void)viewDidLoad {
[super viewDidLoad];
self.table.dataSource = self;
self.table.delegate = self;
}
but much easier would be to set the datasource in interface-build aka the XIB file.
Try this
#interface SettingsCourses : UIViewController <UITableViewDelegate,UITableViewDataSource>
You do not appear to be setting the table delegate or datasource. To do this just add the following code when in your unit method
table.dataSource = self or wherever your data source is;
table.delegate = self:
YOu might want to try to disconnect and reconnect datasource and delegate in the interface builder. IB sometimes "forgets" connections.
I had setup a UITableViewController in IB, but its class name didn't match the UITableViewController file I had. Rookie!
The error for me was that I was returning 0 in
func numberOfSections(in tableView: UITableView) -> Int

type of property txtname (uiTextView *) does not match of ivar txtname(uiTextView)

// ViewController.h
#interface ViewController : UIViewController{
IBOutlet UIButton *btnname;
IBOutlet UITextView *txtname;
IBOutlet UILabel *lblname;
}
#property (retain, nonatomic) IBOutlet UITextField *txtname;
#property (retain, nonatomic) IBOutlet UILabel *lblname;
- (IBAction)Btntikla:(id)sender;
#end
//ViewController.m
#implementation ViewController
#synthesize txtname;//here it says -type of property txtname (uiTextView *) does not match of ivar txtname(uiTextView)- this mistakes
#synthesize lblname;
- (void)dealloc {
[lblname release];
[txtname release];
[super dealloc];
}
- (IBAction)Btntikla:(id)sender {
NSString *name=[txtname text];
lblname.text=name;
}
#end
//my whole program is this.it gives this error?type of property txtname (uiTextView *) does not match of ivar txtname(uiTextView)..Can you help me pls?? what is my mistake ?
That's beacause you have
#property (retain, nonatomic) IBOutlet UITextField *txtname;
and
IBOutlet UITextView *txtname;
And UITextField and UITextView are differents controls

saving data in textfields and creating delegates

Trying to get a textfield to save data automatically and store it, I want to be able to go back to the view and the data remain until its edited again.
so far I have this...
#interface TableDatabaseViewController : UIViewController <UITextFieldDelegate>
{
Tables*table;
NSArray * tables;
IBOutlet UITextField * surname;
IBOutlet UITextField * seatNo;
IBOutlet UISegmentedControl * occupied;
IBOutlet UITextField * numberLabel;
int tapCount;
id <TableInfoDelgate> modTable;
}
#property (nonatomic, retain) Tables*table;
#property (nonatomic, retain) NSArray * tables;
#property (nonatomic, retain) IBOutlet UITextField * surname;
#property (nonatomic, retain) IBOutlet UITextField * seatNo;
#property (nonatomic, retain) IBOutlet UISegmentedControl* occupied;
#property (nonatomic, retain) id <TableInfoDelgate> modTable;
#property (nonatomic, retain) IBOutlet UITextField * numberLabel;
#property int tapCount;
-(id)initWithTables:(Tables*)t;
//-(void)myFunction;
#end
#synthesize table, numberLabel,tables,tapCount, modTable, seatNo,surname, occupied;
- (void)dealloc
{
self.numberLabel =nil;
self.surname = nil;
self.table = nil;
self.seatNo = nil;
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
-(void)myFunction
{
AddTableViewController * createController = [[AddTableViewController alloc] initWithNibName:#"AddTableViewController" bundle:nil];
[self.navigationController pushViewController:createController animated:YES];
[createController release];
}
- (void)viewDidLoad
{
UIBarButtonItem * theButton = [[UIBarButtonItem alloc] initWithTitle:#"Order" style:UIBarButtonItemStyleBordered target:self action:#selector(myFunction)];
self.navigationItem.rightBarButtonItem = theButton;
[theButton release];
self.tables = [TableListBuilder getTables];
self.numberLabel.text = [NSString stringWithFormat:#"%#", self.table.number];
self.seatNo.text = self.table.seats;
self.surname.text = self.table.surname;
// self.occupied.text = self.table.occupied;
[super viewDidLoad];
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:NO];
[self.modTable modifyTableatIndex: nil andsurname:self.surname.text andseatNo:self.seatNo.text andoccupied: self.occupied];
}
I also created a delegate which is:
#protocol TableInfoDelgate <NSObject>
-(void) modifyTableatIndex: (int) index andsurname: (NSString*) surname andseatNo: (NSString*) seatNo andoccupied: (BOOL) occupied;
#end
data in the textfield is not saving. Thanks
What does this function do?
-(void) modifyTableatIndex: (int) index andsurname: (NSString*) surname andseatNo: (NSString*) seatNo andoccupied: (BOOL) occupied;
For storing simple values look into NSUserDefaults
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[textField setText:[[NSUserDefaults standardUserDefaults] objectForKey:#"storedTextValue"];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSUserDefaults standardUserDefaults] setObject:textField.text forKey:#"storedTextValue"];
[[NSUserDefaults standardUserDefaults] synchronize];
}