i’ve been doing a thing here using objective-c, but i’m a beginner in this language, and that’s what i did:
there is a class, named “firstviewclass”, that is in the control of my first view, in this view there is a textfield that the user puts a number. the textfield is in the firstviewclass.h named “setNumber”. There is another class, named “secondviewclass” that is in control of the second view, in this view there is a label that is in the secondviewclass.h, and i want that this label recive the value that the user put in the textfield from the first view, but when i test it on the iOS simulator any number that i put in the textfield it appears in the label as 0… I really don’t know what to do!
My codes:
firstviewclass.h:
#import <UIKit/UIKit.h>
@interface firstviewclass : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *setNumber;
- (IBAction)gotonextview:(id)sender;
@end
secondviewclass.h:
#import <UIKit/UIKit.h>
#import "firstviewclass.h"
@interface secondviewclass : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *labelthatrecivesthetextfieldvalue;
@end
secondviewclass.m:
#import "secondviewclass.h"
#import "firstviewclass.h"
@implementation secondviewclass
@synthesize labelthatrecivesthetextfieldvalue;
-(void)viewDidLoad{
[super viewDidLoad];
firstviewclass *object = [[firstviewclass alloc] init];
NSString *string = [[NSString alloc] initWithFormat:@"%i", [object.setNumber.text intValue]];
labelthatrecivesthetextfieldvalue.text = string;
}
@end
First of all, I would strongly recommend not naming a property or instance variable
setSomething. It will cause headaches for anyone reading your code because it will always look like you’re trying to call a setter. Also, please do capitalize your class names.Your actual problem is that in
viewDidLoadyou’re creating an instance offirstviewclass, and then trying to get the value from setNumber. That is before the user had any chance to enter anything.Also, the
setNumberoutlet infirstviewclasswill probably be going nowhere anyway, since you’re instantiating that class yourself, instead of loading the NIB.Edit (ah, Storyboard, d’oh):
For Storyboard, you need to pass the setNumber text field’s value to the second view.
First of all, remove the
firstviewclass *object = [[firstviewclass alloc] init];line.Then, in your first view controller’s
prepareForSeguemethod, you can pass the value of thesetNumbertext field to a property in your your second view controller, and use it from there (e.g. inconfigureView).I recommend working through Apple’s Storyboard tutorial, it shows exactly what you need to do, step by step. The step you’re having issues with right now, passing data to your next view controller, is here.