我是iPhone App开发的新手,下面是ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self updateMyView];
}
- (IBAction)clickButtonResult:(id)sender
{
enteredText = [textField text]; // Or textField.text
NSLog(@"Number 1 : %i", number_1);
NSLog(@"Number 2 : %i", number_2);
NSLog(@"Entered Text is %@", enteredText);
int NUM_RESULT = number_1 + number_2;
verify_result = [NSString stringWithFormat:@"%i", NUM_RESULT];
NSLog(@"Verify Result : %@", verify_result);
NSString *final_result = [NSString stringWithFormat:@"%d", [enteredText isEqualToString:verify_result]];
int final_int_result = [final_result integerValue];
if (final_int_result) {
//result_label.text = @"Correct";
NSLog(@"Correct");
[self updateMyView];
} else {
//result_label.text = @"Wrong";
NSLog(@"Wrong");
}
}
- (int)getRandomNumberBetween:(int)min maxNumber:(int)max
{
return min + arc4random() % (max - min + 1);
}
- (void) updateMyView
{
number_1 = [self getRandomNumberBetween:10 maxNumber:99];
number_2 = [self getRandomNumberBetween:10 maxNumber:99];
num_1.text = [NSString stringWithFormat:@"%i", number_1];
num_2.text = [NSString stringWithFormat:@"%i", number_2];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
和ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
IBOutlet UILabel *num_1;
IBOutlet UILabel *num_2;
IBOutlet UILabel *result_label;
IBOutlet UITextField *textField;
int number_1;
int number_2;
NSString *verify_result;
NSString *enteredText;
BOOL display_result;
}
- (IBAction)clickButtonResult:(id)sender;
@end
输入正确的结果后,应该使用updateMyView函数更新UIView,但它没有发生。
任何人都可以帮忙吗?
答案 0 :(得分:1)
首先,开始使用Properties。
ViewController.h
@interface ViewController : UIViewController
@property (nonatomic, weak) IBOutlet UILabel *num_1;
@property (nonatomic, weak) IBOutlet UILabel *num_2;
@property (nonatomic, weak) IBOutlet UILabel *resultLabel;
@property (nonatomic, weak) IBOutlet UITextField *textField;
@property (nonatomic) NSInteger number_1;
@property (nonatomic) NSInteger number_2;
@property (nonatomic, strong) NSString *verifyResult;
@property (nonatomic, strong) NSString *enteredText;
@property (nonatomic) BOOL displayResult;
- (IBAction)clickButtonResult:(id)sender;
@end
在ViewController.m代码中,使用self。{name of property},例如text.Field属性的self.textField。
现在,转到“界面”构建器并将IBOutlet属性连接到正确的对象。 (单击文件所有者上的右键)
答案 1 :(得分:0)
尝试更改num_1.text
中的viewDidLoad
,以确保您可以通过UIViewController
访问该标签。
所以在viewDidLoad
中,只需添加类似
num1.text = @"Updated from viewDidLoad"
答案 2 :(得分:0)
确保您已正确连接num_1
和num_2
UILabels
。看来你没有连接这些。
答案 3 :(得分:0)
将[self updateMyView];
移到if
函数中的clickButtonResult:
语句之外
然后它会在点击按钮时更新视图。
if (final_int_result) {
//result_label.text = @"Correct";
NSLog(@"Correct");
} else {
//result_label.text = @"Wrong";
NSLog(@"Wrong");
}
[self updateMyView];