我正在关注几年前写的一本 Objective-C 书,我到了他们教如何使用界面构建器链接和设计 UI 的部分。现在,与之等效的似乎是“故事板”。我按照 IB 的说明将它们翻译成故事板,它似乎大部分都奏效了。但是,我没有让我的实例变量 UILabel 显示在屏幕上。所有按钮和标签都是在情节提要中创建的,而不是通过编程方式创建的。任何帮助,将不胜感激!
视图控制器.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
IBOutlet UILabel *randNumber; //instance variable
}
-(IBAction)seed:(id)sender;
-(IBAction)generate:(id)sender;
@property (retain,nonatomic) UILabel *randNumber; //getter and setter methods
@end
视图控制器.m
@interface ViewController ()
@end
@implementation ViewController
@synthesize randNumber;
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(IBAction)seed:(id)sender
{
srandom(time(NULL));
[randNumber setText:@"Generator seeded."];
}
-(IBAction)generate:(id)sender
{
//generate a random number between 0 and 100 inclusive
int generated;
generated = (random()%101);
[randNumber setText:[NSString stringWithFormat:@"%i",generated]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end