I am trying to make a very simple iOS app that has two buttons: a +1 and a -1, and then on a separate tab, a label will display the total. I have been reading for the last hour about how to pass data between view controllers, but it doesn't make any sense to me. I am complete iOS newbie. Here is my code:
FirstViewController.h:
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
- (IBAction)takeOne:(id)sender;
- (IBAction)addOne:(id)sender;
@end
FirstviewController.m:
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)takeOne:(id)sender {
SecondViewController.counter--;
[label setText:[NSString stringWithFormat:@"%d", counter]];
}
- (IBAction)addOne:(id)sender {
SecondViewController.counter++;
[label setText:[NSString stringWithFormat:@"%d", counter]];
}
@end
SecondViewController.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@property (nonatomic) int counter;
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
SecondViewController.m
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
In FirstViewController.m, XCode is telling me errors that "Property 'counter' not found on object of type 'SecondViewController'" and that "Use of undeclared identifier 'label'".
Part of the reason I am confused is that there is a lot of information out there that seems to be for older versions of iOS. For example, I am very confused by the synthesize stuff.
Thanks for your help!
viewDidAppear
for the second view controller retrieves the current model data, and that's all you need. – Rob