How to set NSString* property from another class? - iphone

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

Related

Can't update UIlabel text

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?

Unrecognized selector sent to instance?

I have seen that a few others have had this problem as well.
I'm trying to follow a tutorial online that shows how to create animated pins on a MapView.
I have implemented the code as shown in the tutorial and the project builds fine except I receive this exception:
-[MKPointAnnotation iconN]: unrecognized selector sent to instance
I have a subclass of 'MKPinAnnotationView' and in the .m file I create this method:
- (void)setAnnotation:(id<MKAnnotation>)annotation {
[super setAnnotation:annotation];
//Place *place = [[Place alloc] init];
Place *place = (Place *)annotation;
//The following line is where the program sends "SIGABRT"
icon = [UIImage imageNamed:[NSString stringWithFormat:#"pin_%d.png", [place.iconN intValue]]];
[iconView setImage:icon];
}
Here are a few parts from my "model" which is called Place.h/.m.
Here is where I create the property for 'iconN'.
#property (retain, nonatomic) NSNumber *iconN;
And here I synthesize it:
#synthesize iconN = _iconN;
Any help is greatly appreciated.
EDIT:
Here is the Place.h and Place.m
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface Place : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
}
#property (retain, nonatomic) NSNumber *iconN;
#property (nonatomic, copy) NSString *title;
#property (nonatomic) CLLocationCoordinate2D coordinate;
- (id)initWithLong:(CGFloat)lon Lat:(CGFloat)lat iconNumber:(NSNumber *)iconNumber;
#end
And the Place.m
#import "Place.h"
#implementation Place
#synthesize coordinate;
#synthesize iconN = _iconN;
#synthesize title;
- (id)initWithLong:(CGFloat)lon Lat:(CGFloat)lat iconNumber:(NSNumber *)iconNumber {
self = [super init];
if (self) {
coordinate = CLLocationCoordinate2DMake(lat, lon);
self.iconN = iconNumber;
}
return self;
}
- (NSString *)title {
return [NSString stringWithFormat:#"Bus: %d", [self.iconN intValue]];
}
- (NSString *)subtitle {
return [NSString stringWithFormat:#"bus[%d] from database.", [self.iconN intValue] - 1];
}
#end
You cannot convert a MKAnnotation to a Place just by casting it. This line is wrong.
Place *place = (Place *)annotation;
You should post your Place.h and Place.m files if you're still stuck. You need to either set the iconN property on a new Place object, or create an init method in the Place class that accepts the MKAnnotation object as a parameter and sets it own internal values accordingly.
In the line
Place *place = (Place *)annotation;
has the variable place of annotation variable class (MKPointAnnotation), you are not able to bring the master class variable to a subclass in this way. Instead you'll have to make a constructor for Place from MKPointAnnotation and perform a check in the setAnnotation method that annotation is of MKPointAnnotation.
You are sending the message to the annotation but you seem to have subclasses the annotation view.
Posting as an answer what was originally just a comment:
I'm not familiar with the MapKit, but the thing that sticks out for me in this: -[MKPointAnnotation iconN]: unrecognized selector sent to instance is that the class is MKPointAnnotation. So the annotation you're receiving isn't actually a Place object, it's an MKPointAnnotation object - you can't just cast to Place. I suspect the root of your problem is where you create your annotation object in the first place.

Copy of string from one view to another view in iphone

hi i am new to iphone development.
I'm trying with sample where I need to copy a string from the textfield of viewController and display it on the next view with a Label.
On the first view there is button bellow the textfield.1
I am not able to fix some issues showing BAD ACESS can anyone help me in solving this.
Let me know what i'm doing Wrong.
Thank you.
//copystring.h
#interface CopystringViewController : UIViewController{
UITextField *myTextField;
NSString *somestring;
}
#property(nonatomic,retain)IBOutlet UITextField *myTextfield;
#property(nonatomic,retain)NSString *somestring;
-(IBAction)next:(id)sender;
//.m file
#synthesize myTextfield,somestring;
-(IBAction)next:(id)sender;
{
NextView * next = [[NextView alloc] initWithNewString: myTextField.text];
[self.navigationController presentModalViewController: next animated: YES];
}
NextView.h
#interface NextView : UIViewController{
UILabel *string2;
}
#property(nonatomic,retain)IBOutlet UILabel *string2;
- (id)initWithNewString:(NSString*)someString;
//.m file
#synthesize string2;
-(id)initWithNewString:(NSString*)someString {
string2 = someString;
return self;
}
Just replace method.it may run.
-(IBAction)next:(id)sender; {
NextView * next = [[NextView alloc] initWithNibName:#"NextView" bundle:nil];
next.string2=myTextField.text;
[self.navigationController presentModalViewController:next animated: YES];
}
hey string2 is a label so try string2.text = somestring;
-(id)initWithNewString:(NSString*)someString {
string2.text = someString;
return self;
}
It looks like the problem is that you're assigning an NSString (someString) to a UILabel (string2).
AppleVijay's answer should do the trick.
If you don't like dot-notation in ObjC you can also write it like this:
[string2 setText:someString]
U can declare ur somestring in to the delagate h/m file that way it wil be the global string. u can use it with appdaligateobj.stringname.
U dont evn need init u can directly add to viewdidload of next view.
string2.text = AppDalegateobj.stringName.
Delegation is the usual way to move data around like this. I wrote a very simple example project to show this in action.
You need init you view and retain you string:
-(id)initWithNewString:(NSString*)someString {
self = [super init];
if (self) {
string2.text = someString;
}
return self;
}
/
/AppDelegate.h
NSString *String1;
#property (nonatomic, retain) NSString * String1;
//AppDelegate.m
#synthesize String1
//ViewController1.h
AppDelegate * objAppDelegate;
UILable *lblstring;
#property (nonatomic, retain) UILable *lblstring;
//ViewController1.m
#synthesize lblstring
//On View Did Load.
objAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
lblstring.text = objAppDelegate.String1;

CFString isNaturallyRTL - message sent to deallocated instance

I already googled for "CFString isNaturallyRTL" with 0 results.
these are my classes:
//in .H
#interface myViewController : UIViewController {
UITextField *from;
UITextField *to;
NSString *fromText;
NSString *toText;
}
#property (nonatomic, retain) NSString* fromText;
#property (nonatomic, retain) NSString* toText;
#property (nonatomic, retain) UITextField *from;
#property (nonatomic, retain) UITextField *to;
//in .m
#synthesize from, to;
#synthesize fromText, toText;
viewDidLoad(...) {
fromText = #"Roma";
toText = #"Lecce";
}
- (void) drawRoute {
if ( ([[from text] length] > 2) && ([[to text] length] > 2) )
{
fromText = from.text;
toText = to.text;
[...]
}
}
Now, i have a view that open on button touch tha contains two text boxes and a button. Like this.
- (void) drawRouteTextboxes {
from = [[UITextField alloc] initWithFrame: [...] ];
from.text = fromText;
from.delegate = self;
[ctr.view addSubview:from];
[from release];
to = [[UITextField alloc] initWithFrame: [...] ];
[...]
[searchButton addTarget:self action:#selector(drawRoute) forControlEvents: UIControlEventTouchUpInside];
}
It's all correct, compile and run.
First time that i click drawRouteTextboxes, it opens my view with default text setted ("Roma" and "lecce").
Second time, i open the view, edit textfield and call drawRoute. It's ok.
The third time that i call drawRouteTextboxes it return me this runtime error:
*** -[CFString _isNaturallyRTL]: message sent to deallocated instance 0x3a8d140
I don't know where is the problem...
Someone know a solution?
It's the first time that i see this error!
thanks,
Alberto.
It's all correct, compile and run.
If it was all correct, it would run without error. ;)
This looks suspect:
fromText = from.text;
toText = to.text;
If from.text and to.text are returning either autoreleased objects or objects that are later released, then the above doesn't retain the strings and could easily lead to an over-release problem as you are seeing.
Use self.fromText = from.text; instead.
Note that NSString* properties should almost always be copy and not retain.

Problem with sharing variables between views - missing something?

I know im missing something but my friend and I can figure out what.
Firstly.. I have two .hs and .ms that I'd like to share data between - two view controllers
In the first .h i have this - that makes the variables and properties them
//top half of .h
//Passing to Submit Page
NSMutableString *messageString;
NSInteger theirTime;
}
#property (nonatomic, readwrite) NSInteger theirTime;
#property (nonatomic, retain, readwrite) NSMutableString *messageString;
/actions
#end
Then in the respective .m - sythesize them
#synthesize messageString, theirTime;
then from the new .h and .h i need to acces them.. so In view did load i do this
- (void)viewDidLoad {
messageString = [[NSMutableString alloc] init];
MemoryViewController *controller = [[MemoryViewController alloc] init];
timeInSeconds = controller.theirTime;
NSLog(#"Time = %d", timeInSeconds);
messageString = controller.messageString;
NSLog(#"Message - %#", messageString);
[controller release];
NSUserDefaults *HighScore = [NSUserDefaults standardUserDefaults];
bestTime.text= [NSString stringWithFormat:#"Best Time:%d", [HighScore integerForKey:#"integerKey"]];
currentTime.text = [NSString stringWithFormat:#"Current Time:%d", timeInSeconds];
[super viewDidLoad];
}
and at the top
#import "MemoryViewController.h"
and now the .h to show you all what the variables are
IBOutlet UILabel *bestTime;
IBOutlet UILabel *currentTime;
int timeInSeconds;
NSMutableString *messageString;
So. In short - I made variables made properties, and synthesized them, then in the view i make an instance of the other VC, then try use them to do things
Log out put
2010-04-15 20:53:09.105 Memory[3538:207] Time = 0
2010-04-15 20:53:09.107 Memory[3538:207] Message - (null)
Any ideas guys would be great... if you need more code/ less code just say.. ive tried other blogs but they all do it with app delegates.. and i dont like global variables.
Cheers
Sam
You initialised a new MemoryViewController instance in your -viewDidLoad, so of course all of its instance variables are 0 or nil. If you already have a MemoryViewController that you need to get the properties from, you need to reference that instance instead of creating a new one.