string set to null when pushing detail view - iphone

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]];

Related

[__NSCFNumber length]: unrecognized selector sent to instance

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]]];

Property not found on ViewController

I am beginning in iOS development. I've been searching a solution for days and I can't get it. I got the following code:
#interface SAProfileViewController : UITableViewController {
#public NSNumber *userId;
}
#property (weak, nonatomic) IBOutlet UIView *awesomeProfileHeader;
#property (nonatomic, assign) NSNumber *userId;
#end
As you can see I am declaring my property, and after that I do this :
#implementation SAProfileViewController
#synthesize userId = _userId;
...
On an other ViewController I import SAProfileViewController.
#import "SAProfileViewController.h"
...
-(void)goToUserProfile :(id) sender
{
UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
SAProfileViewController *controller = [[SAProfileViewController alloc] init];
controller.userId = gesture.view.tag; // << HERE THE ERROR APPEARS
[self.navigationController pushViewController:controller animated:YES];
}
That's it, this is my code and I get the following error:
"Property 'userId' not found on object of type 'SAProfileViewController *'
Thanks in advance.
You have done a mistake with the property type.
Change
#property (nonatomic, assign) NSNumber *userId;
to
#property (nonatomic, retain) NSNumber *userId;
Than you have to create a NSNumber instance of gesture.view.tag like this
controller.userId = [NSNumber numberWithInteger:gesture.view.tag];
OR you can change your userId to NSInteger instead of NSNumber*
#property (nonatomic, assign) NSInteger userId;
(you have to do this for the object attribute userId, too)

unable to set values taken from text fields from a UIViewController to another class for calculation

I am having problems unable to set values taken from UItextfields from a Viewcontroller to another class which does the calculations from those values.
#interface InitialViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *phValue;
#property (weak, nonatomic) IBOutlet UITextField *pco2Value;
#property (weak, nonatomic) IBOutlet UITextField *hco3Value;
#property (weak, nonatomic) IBOutlet UITextField *sodiumValue;
#property (weak, nonatomic) IBOutlet UITextField *chlorideValue;
#property (nonatomic,retain)NSDecimalNumber* ph;
#property (nonatomic,retain) NSDecimalNumber* pco2;
#property (nonatomic,retain)NSDecimalNumber* hco3;
#property (nonatomic,retain)NSDecimalNumber* sodium;
#property (nonatomic,retain)NSDecimalNumber* chloride;
- (IBAction)analyseValues:(UIButton *)sender;
#end
in the implementation
-(void)values{
[self setPh:[[NSDecimalNumber alloc] initWithFloat:phValue.text.floatValue ]];
[self setPco2:[[NSDecimalNumber alloc] initWithFloat:pco2Value.text.floatValue]];
[self setHco3:[[NSDecimalNumber alloc ] initWithFloat:hco3Value.text.floatValue]];
[self setSodium:[[NSDecimalNumber alloc] initWithFloat:sodiumValue.text.floatValue] ];
[self setChloride:[[NSDecimalNumber alloc] initWithFloat:chlorideValue.text.floatValue] ];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self values];}
Then I have another class which takes these values ,but on running the program it runs but does assign values taken from the initialViewController.
#import <Foundation/Foundation.h>
#import "InitialViewController.h"
#interface AcidBaseCalculations : NSObject
#property (nonatomic) float ph;
#property (nonatomic) float pco2;
#property (nonatomic) float chloride;
#property (nonatomic) float sodium;
#property (nonatomic) float hco3;
#implementation AcidBaseCalculations
#synthesize ph,pco2,chloride,hco3,sodium;
-(void)calculations{
InitialViewController *bvc = [[InitialViewController alloc] init];
ph = bvc.ph.floatValue ;
pco2 = bvc.pco2.floatValue;
// these values are not being set although the program runs successfully
hco3 = bvc.hco3.floatValue;
sodium = bvc.sodium.floatValue;
chloride = bvc.chloride.floatValue;
I want to use these new values here and perform some logical operations in this class.
You are creating a new InitialViewController with this line:
InitialViewController *bvc = [[InitialViewController alloc] init];
It is a diffrent instance than your other/original InitialViewController
You either have to make a property
#property(nonatomic,strong)InitialViewController myInitialVC;
And set this property when you are alloc/initing your AcidBaseCalculations.
Our you have to make property for all variables you would like to calculate and set them while alloc/initing to the
AcidBaseCalculations

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;
...

Cant set row selected for RSS app in iphone

OK, i'm making an app that pulls down rss feeds, the initial table view is the headlines, the second view shows the detail of that headline the user selected.
This is the code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[[self appDelegate] setCurrentlySelectedBlogItem:[[[self rssParser]rssItems]objectAtIndex:indexPath.row]];
NewsDetailViewController *newsDetail = [[NewsDetailViewController alloc] initWithNibName:#"NewsDetailViewController" bundle:nil];
//NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];
// ...
// Pass the selected object to the new view controller.
newsDetail.titleTextView.text = [[[self appDelegate] currentlySelectedBlogItem]title];
newsDetail.descriptionTextView.text = [[[self appDelegate] currentlySelectedBlogItem]description];
[self.navigationController pushViewController:newsDetail animated:YES];
[newsDetail release];
}
setCurrentlySelectedBlogItem is a pointer to blogrss class which is reference in the AppDelegate. When i'm clicking on the row of the headline and its taking me to the next view, the content is empty. In the last two lines above i'm attempting to set the UILabel's as the current headline's details...
Anyone hazard a guess to why this is happening?
Also this is my AppDelegate.h file:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#class RssFunViewController;
#class NewsDetailViewController;
#class RootViewController;
#class BlogRss;
#class BlogRssParser;
#interface ExampleAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
RootViewController *viewController;
NewsDetailViewController * newsDetailController;
BlogRss * currentlySelectedBlogItem;
#private
NSManagedObjectContext *managedObjectContext_;
NSManagedObjectModel *managedObjectModel_;
NSPersistentStoreCoordinator *persistentStoreCoordinator_;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet RootViewController *viewController;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (nonatomic, retain) IBOutlet NewsDetailViewController * newsDetailController;
#property (readwrite,retain) BlogRss * currentlySelectedBlogItem;
#property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
#property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (NSString *)applicationDocumentsDirectory;
- (void)saveContext;
#end
The UILabels in the view controller are probably still nil at that point and not accessible. Instead of setting the controls directly, create two NSString properties in the NewsDetailViewController and set those before you push it. Then, in the NewsDetailViewController, set the labels to the property values in the viewDidLoad method (at which point the labels should be instantiated and accessible).