Can't update UIlabel text - iphone

In my iPhone App I have ViewController1 that contains a UILabel called selectedBook and I added to its view in the storyboard and I connected the outlets.
I have another view controller called ViewController2 and I pass some data from ViewController2 to ViewController1 and the text of the UILabel is one of them.
In ViewController1 I wrote the following code:
in ViewController1.h
#interface ViewController1 : UIViewController{
IBOutlet UILabel *selectedBook;
NSString *BookName;
NSString *BookCover;
NSString *BookAbout;
}
#property (nonatomic, retain) NSString *BookName;
#property (nonatomic, retain) NSString *BookCover;
#property (nonatomic, retain) NSString *BookAbout;
-(void) setTheSelectedBook:(NSString *)bookName bookCover:(NSString *)bookCover bookAbout:(NSString *)bookAbout userID:(int)userID;
In ViewController1.m
-(void) setTheSelectedBook:(NSString *)bookName bookCover:(NSString *)bookCover bookAbout:(NSString *)bookAbout userID:(int)userID
{
BookName = bookName;
BookCover = bookCover;
BookAbout = bookAbout;
NSLog(#"in setTheSelectedBook, BookName: %# - BookCover: %# - BookAbout: %# - userID: %d", BookName, BookCover, BookAbout, userID);
selectedBook.text = bookName;
}
In viewController2 I wrote the following code:
ViewController1 *viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"Suggest"];
[viewController setTheSelectedBook:#"MybookName" bookCover:#"MyBookCover" bookAbout:#"MuBookAbout" userID:1];
My data pass properly and I see it in the output, but the problem is that the UILabel text is not updated, meaning that this line
selectedBook.text = bookName;
does not work.
I tried writing it as selectedBook.text = #"someText"; and this works.
I can't understand what's missing to make selectedBook.text = bookName; work? I hope someone help me in this issue.
Thanks in advance.

Your code is a bit confusing. Never begin your variables with a capital letter. BookName should be bookName or _bookName. I think that's one of the problems.
If you instead create a Book-class with the variables:
#property (copy) NSString *name;
#property (copy) NSString *cover;
#property (copy) NSString *about;
Then in your viewController1 create the book by instantiating your Book-class and set it's variables, you could then send your Book-object to your viewController2.
That would probably be easier for you.
If you just want to change your own code, you could try with:
In your h-file:
#property (copy) NSString *BookName;
#property (copy) NSString *BookCover;
#property (copy) NSString *BookAbout;
And in your m-file:
-(void) setTheSelectedBook:(NSString *)bookName bookCover:(NSString *)bookCover bookAbout:(NSString *)bookAbout userID:(int)userID
{
_BookName = bookName;
_BookCover = bookCover;
_BookAbout = bookAbout;
NSLog(#"in setTheSelectedBook, BookName: %# - BookCover: %# - BookAbout: %# - userID: %d", BookName, BookCover, BookAbout, userID);
selectedBook.text = _BookName;
}

The only explanation I see here is, that bookName is not an NSString.
Add the following line to that method:
NSLog(#"bookName class is %#", [bookName class]);
NSLog(#"selectedBook class is %#", [selectedBook class]);
What does XCode write to the log during runtime?

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

Simple NSString Error - Printing (null)

I'm having a problem with NSString. I feel like it's a pretty simple thing to figure out but I've been staring at it for a while and just can't seem to get anywhere :/ Your help is highly appreciated!
I defined a class called Painting.
Here is Painting.h:
#interface Painting : NSObject {
NSString *artist;
UIImage *image;
}
#property (nonatomic, copy) NSString *artist;
#property (nonatomic, copy) UIImage *image;
#end
Here is Painting.m:
#implementation Painting
#synthesize artist, image;
#end
In a .h file for a view controller, I create a "painting":
Painting *monet;
#property (nonatomic, retain) Painting *monet;
Now, in it's .m file, I'm trying to do a very very simple print and it won't work. I get (null) instead of "Monet".
monet.artist = #"Monet";
NSString *bob3 = monet.artist;
NSLog(#"Real artist: %#", bob3);
However, this does work (gives me "Monet" instead of (null)):
NSString *bob3 = #"Monet";
NSLog(#"Real artist: %#", bob3);
What am I missing??
You probably need before:
monet.artist = #"Monet";
this:
monet = [[Painting alloc] init];

How to set NSString* property from another class?

Please look to the following code.
FirstViewController.h:
#interface FirstViewController : UIViewController {
NSString* inputData;
}
#property (nonatomic, copy/retain) NSString* inputData;
FirstViewController.m:
#synthesize inputData;
SecondViewController.m:
- (IBAction)buttonSaveDown {
[firstViewController setInputData:#"Text"];
firstViewController.inputData = #"Text";
firstViewController.inputData = [[NSString stringWithString:#"Text"] retain];
}
Add breakpoints after every line with inputData. Check firstViewController.inputData - it's '(null)'! Why?
Printing description of inputData:
(null)
Also I try 'Edit value' in the debugger. After setting value the result is '(null)' too.
There's no any warnings on the build. Please help anybody. Thanks a lot!
To moderators: Sorry, but someone deleted the same previous question when I asked to delete all comments on it.
Your firstViewController variable is not set to anything. You need to set it to a valid instance of FirstViewController before you can do anything useful with it.
Just use
#property (nonatomic, retain) NSString* inputData;
Then these each should work:
[firstViewController setInputData:#"Text"];
firstViewController.inputData = #"Text";
firstViewController.inputData = [NSString stringWithString:#"Text"];

Retaining value from textbox in an NSString

Here's my .h file:
The IBOutlet I am having problems with is the textbox. I would like to retain the value in the textbox as an NSString:
#interface UploaderViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
IBOutlet UIImageView *imageView;
IBOutlet UITextField *textCaption;
NSString *caption;
}
- (IBAction)pushPick;
- (IBAction)pushUpload;
#property (nonatomic, retain) IBOutlet UITextField *textCaption;
#end
Where the value is stored in this NSString:
- (IBAction)pushUpload {
caption = textCaption.text;
}
I think you meant to do something like this:
- (IBAction)pushUpload {
[caption release];
caption = [textCaption.text copy];
}
In shouldChangeTextInRange call your pushUpload.

UIPopover Delegate - Unable to assign regardless of protocal/declarations

I'm having some difficulty figure out what I'm doing wrong when trying to assign my a delegate for my UIPopoverView. I tried to work around not even using one, but having it would be much more straightforward and clean. Here is the code that I think should cover it:
//.h of View where I call popover, this would be the delegate.
#import <UIKit/UIKit.h>
#import "ACTypePopoverViewController.h"
#interface NewRouteViewController : UIViewController<ACTypePickerDelegate>{
ACTypePopoverViewController *_acTypePicker;
UIPopoverController *_acTypePickerPopover;
}
#property (nonatomic, retain) ACTypePopoverViewController *acTypePicker;
#property (nonatomic, retain) UIPopoverController *acTypePickerPopover;
#end
//.m file for where I would like to use the popover, is the .m for the .h above
if (_acTypePickerPopover == nil)
{
ACTypePopoverViewController* content = [[ACTypePopoverViewController alloc] init];
UIPopoverController* aPopover = [[UIPopoverController alloc]
initWithContentViewController:content];
aPopover.delegate = self;
[content release];
// Store the popover in a custom property for later use.
self.acTypePickerPopover = aPopover;
}
[self.acTypePickerPopover presentPopoverFromRect:self.selectACTypeButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
//.h file for the actual popover, what I would be setting the delegate of
#protocol ACTypePickerDelegate
- (void)acTypeSelected:(NSString *)acType;
#end
#interface ACTypePopoverViewController : UITableViewController {
NSMutableArray *_acTypes;
NSString *selectedACType;
id<ACTypePickerDelegate> _delegate;
}
#property (nonatomic, retain) NSMutableArray *acTypes;
#property (nonatomic, retain) NSString *selectedACType;
#property (nonatomic, assign) id<ACTypePickerDelegate> delegate;
#end
I think thats all I need, but let me know if more code is needed!
Thanks!
I understood you correctly... what you need is:
content.delegate = self;
Right after this line you have:
ACTypePopoverViewController* content = [[ACTypePopoverViewController alloc] init];
Are you synthesizing your properties? Also you are assigning your delegate before initiating the popover...
#synthesize acTypePickerPopover;
self.acTypePickerPopover = [[[UIPopoverController alloc]
initWithContentViewController:_acTypePickerPopover] autorelease];
self.acTypePickerPopover.delegate = self;
`