Reputation: 333
I am new to the objective C programming and I am in a position where I need to create an iPhone App really quickly. I am using XCode 4.2
I have a problem transferring an NSString variable from one view to the other . the two views are in two different sets of .h and .m classes
in the first class in the .h i have something like this
@interface firstview : UIViewController {
NSString *test;
}
-(IBAction)testbutton
@end
in the .m of the firstview I have
-(IBAction)testbutton{
secondView *second;
[second setText:text]; //set text is a function that will take an NSString parameter
second= [[secondView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
}
in the .h of the secondView I wrote
@interface secondView : UIViewController{
-IB
}
Upvotes: 1
Views: 136
Reputation: 125007
You've got the right idea, but you're trying to call -setText:
before second
points to a valid object! Do this instead:
-(IBAction)testbutton{
secondView *second;
second = [[secondView alloc] initWithNibName:nil bundle:nil];
[second setText:text]; //set text is a function that will take an NSString parameter
[self presentModalViewController:second animated:YES];
}
Also, the interface you give for your secondView
class looks both incorrect and incomplete -- I'm not sure what you're trying to do with the -IB
part. And it'd help in the future if you follow the usual Objective-C naming convention and start class names with an uppercase character: SecondView
instead of secondView
. Finally, I'd advise against naming a view controller ending in "...View", since that makes it easy to confuse the view controller with a UIView. All together, it should look something like this:
@interface SecondViewController : UIViewController{
NSString *text;
}
@property (retain, nonatomic) NSString *text;
@end
Declaring text
as an instance variable is optional there -- if you don't do it, the compiler will create an ivar if you synthesize the accessors for your text
property.
Upvotes: 1
Reputation: 1307
You need to set the text after you allocate and initialize.
-(IBAction) testButton {
secondView *second = [[[secondView alloc] initWithNibName:nil bundle:nil] autorelease];
[second setText:text];
[self presentModalViewController:second animated:YES];
}
Upvotes: 0
Reputation: 75376
Change your code to this:
-(IBAction)testbutton{
secondView *second;
second = [[secondView alloc] initWithNibName:nil bundle:nil];
[second setText:text]; //set text is a function that will take an NSString parameter
[self presentModalViewController:second animated:YES];
}
In your original version, you were initializing (i.e. instantiating) your second view after calling setText:
on it. You need to initialize it and then set the text.
Upvotes: 0