我正在尝试制作一个非常简单的 iOS 应用程序,它有两个按钮:+1 和 -1,然后在单独的选项卡上,标签将显示总数。过去一小时我一直在阅读有关如何在视图控制器之间传递数据的信息,但这对我来说没有任何意义。我是完整的 iOS 新手。这是我的代码:
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
第二视图控制器.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
在 FirstViewController.m 中,XCode 告诉我“在‘SecondViewController’类型的对象上找不到属性‘计数器’”和“使用未声明的标识符‘标签’”的错误。
我感到困惑的部分原因是那里有很多信息似乎适用于旧版本的 iOS。例如,我对合成的东西很困惑。
谢谢你的帮助!